Compare commits
3 Commits
0aed1528e2
...
e10cc4257d
| Author | SHA1 | Date | |
|---|---|---|---|
| e10cc4257d | |||
| c67841fe07 | |||
| 14d05011d5 |
@@ -1 +1,5 @@
|
||||
((org-mode . ((org-todo-keywords . ((sequence "TODO" | "DONE" "TOO_EASY"))))))
|
||||
((org-mode . ((org-todo-keywords . ((sequence "TODO" | "DONE" "TOO_EASY")))
|
||||
(eval . (let ((lc-el (expand-file-name "lc-org.el" (dir-locals-find-file default-directory))))
|
||||
(when (and lc-el (file-exists-p lc-el))
|
||||
(load-file lc-el)
|
||||
(lc-org-mode 1)))))))
|
||||
|
||||
@@ -14,7 +14,12 @@ An Anki-exportable study deck for NeetCode DSA problems.
|
||||
- Python and C++ solution stubs
|
||||
- A `NEETCODE` property linking back to the roadmap
|
||||
|
||||
3. **Study flow** — open roadmap.org, pick a topic, pick a problem,
|
||||
3. **Toolkit** (`toolkit/`) — your reference library:
|
||||
- `tricks.org` — flashcards for patterns & tricks (exported to Anki)
|
||||
- `notes.org` — deep implementation notes with explanations (exported to Anki)
|
||||
- `suggestions.org` — pattern reference for quick lookup (not exported)
|
||||
|
||||
4. **Study flow** — open roadmap.org, pick a topic, pick a problem,
|
||||
follow the Notes link, solve it, mark it DONE in both places.
|
||||
|
||||
## File Layout
|
||||
@@ -24,7 +29,10 @@ org/study_deck_02/
|
||||
├── AGENTS.md ← you are here
|
||||
├── roadmap.org ← generated by leetcode/extract.mjs
|
||||
├── toolkit/
|
||||
│ └── tricks.org ← common patterns & templates
|
||||
│ ├── tricks.org ← flashcards for patterns & tricks
|
||||
│ ├── notes.org ← deep implementation notes
|
||||
│ ├── suggestions.org ← pattern reference (not exported)
|
||||
│ └── images/ ← diagrams, screenshots
|
||||
└── dsa/
|
||||
├── arrays-hashing/
|
||||
│ ├── 0217-contains-duplicate.org
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Add :lc-problem <number> to org source blocks in leetcode problem files."""
|
||||
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def process_file(path: Path, dry_run: bool = False) -> bool:
|
||||
"""Add :lc-problem to src blocks that don't already have it. Returns True if modified."""
|
||||
m = re.match(r"(\d{4})-", path.stem)
|
||||
if not m:
|
||||
return False
|
||||
problem_num = str(int(m.group(1))) # strip leading zeros: "0015" -> "15"
|
||||
|
||||
text = path.read_text()
|
||||
original = text
|
||||
|
||||
# Match #+begin_src <lang> where lang is python or cpp
|
||||
def fix_src_block(match: re.Match) -> str:
|
||||
full = match.group(0)
|
||||
lang = match.group(1)
|
||||
suffix = match.group(2) or ""
|
||||
|
||||
# If already has :lc-problem, fix leading zeros
|
||||
if ":lc-problem" in suffix:
|
||||
suffix = re.sub(r":lc-problem 0+(\d)", rf":lc-problem \1", suffix)
|
||||
return f"#+begin_src {lang}{suffix}"
|
||||
|
||||
# Add :lc-problem
|
||||
extra = f" :lc-problem {problem_num}"
|
||||
if lang == "python":
|
||||
extra += " :lc-lang python3"
|
||||
return f"#+begin_src {lang}{extra}{suffix}"
|
||||
|
||||
text = re.sub(
|
||||
r"#\+begin_src (python|cpp)([^\n]*)",
|
||||
fix_src_block,
|
||||
text,
|
||||
)
|
||||
|
||||
if text != original:
|
||||
if dry_run:
|
||||
print(f" would modify: {path.name}")
|
||||
else:
|
||||
path.write_text(text)
|
||||
print(f" modified: {path.name}")
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
dry_run = "--dry-run" in sys.argv
|
||||
base = Path(__file__).parent / "dsa"
|
||||
if not base.exists():
|
||||
print(f"dsa/ directory not found at {base}")
|
||||
sys.exit(1)
|
||||
|
||||
org_files = sorted(base.rglob("*.org"))
|
||||
print(f"Found {len(org_files)} org files in dsa/")
|
||||
|
||||
modified = 0
|
||||
skipped = 0
|
||||
for f in org_files:
|
||||
if process_file(f, dry_run):
|
||||
modified += 1
|
||||
else:
|
||||
skipped += 1
|
||||
|
||||
print(f"\nDone: {modified} modified, {skipped} unchanged")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+2
-2
@@ -35,13 +35,13 @@ Output: "bb"
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 5 :lc-lang python3
|
||||
class Solution:
|
||||
def longestPalindrome(self, s: str) -> str:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 5
|
||||
class Solution {
|
||||
public:
|
||||
string longestPalindrome(string s) {
|
||||
|
||||
@@ -41,13 +41,13 @@ Explanation: There are three ways to climb to the top.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 70 :lc-lang python3
|
||||
class Solution:
|
||||
def climbStairs(self, n: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 70
|
||||
class Solution {
|
||||
public:
|
||||
int climbStairs(int n) {
|
||||
|
||||
@@ -72,13 +72,13 @@ The test cases are generated so that the answer fits in a *32-bit* integer.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 91 :lc-lang python3
|
||||
class Solution:
|
||||
def numDecodings(self, s: str) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 91
|
||||
class Solution {
|
||||
public:
|
||||
int numDecodings(string s) {
|
||||
|
||||
@@ -54,13 +54,13 @@ Output: false
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 139 :lc-lang python3
|
||||
class Solution:
|
||||
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 139
|
||||
class Solution {
|
||||
public:
|
||||
bool wordBreak(string s, vector<string>& wordDict) {
|
||||
|
||||
@@ -42,13 +42,13 @@ Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 152 :lc-lang python3
|
||||
class Solution:
|
||||
def maxProduct(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 152
|
||||
class Solution {
|
||||
public:
|
||||
int maxProduct(vector<int>& nums) {
|
||||
|
||||
@@ -40,13 +40,13 @@ Total amount you can rob = 2 + 9 + 1 = 12.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 198 :lc-lang python3
|
||||
class Solution:
|
||||
def rob(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 198
|
||||
class Solution {
|
||||
public:
|
||||
int rob(vector<int>& nums) {
|
||||
|
||||
@@ -48,13 +48,13 @@ Output: 3
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 213 :lc-lang python3
|
||||
class Solution:
|
||||
def rob(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 213
|
||||
class Solution {
|
||||
public:
|
||||
int rob(vector<int>& nums) {
|
||||
|
||||
+2
-2
@@ -46,13 +46,13 @@ Follow up: Can you come up with an algorithm that runs in ~O(n log(n))~ time com
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 300 :lc-lang python3
|
||||
class Solution:
|
||||
def lengthOfLIS(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 300
|
||||
class Solution {
|
||||
public:
|
||||
int lengthOfLIS(vector<int>& nums) {
|
||||
|
||||
@@ -50,13 +50,13 @@ Output: 0
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 322 :lc-lang python3
|
||||
class Solution:
|
||||
def coinChange(self, coins: List[int], amount: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 322
|
||||
class Solution {
|
||||
public:
|
||||
int coinChange(vector<int>& coins, int amount) {
|
||||
|
||||
@@ -36,13 +36,13 @@ Explanation: The array cannot be partitioned into equal sum subsets.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 416 :lc-lang python3
|
||||
class Solution:
|
||||
def canPartition(self, nums: List[int]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 416
|
||||
class Solution {
|
||||
public:
|
||||
bool canPartition(vector<int>& nums) {
|
||||
|
||||
@@ -40,13 +40,13 @@ Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 647 :lc-lang python3
|
||||
class Solution:
|
||||
def countSubstrings(self, s: str) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 647
|
||||
class Solution {
|
||||
public:
|
||||
int countSubstrings(string s) {
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 656 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 656
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -49,13 +49,13 @@ The total cost is 6.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 746 :lc-lang python3
|
||||
class Solution:
|
||||
def minCostClimbingStairs(self, cost: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 746
|
||||
class Solution {
|
||||
public:
|
||||
int minCostClimbingStairs(vector<int>& cost) {
|
||||
|
||||
@@ -58,13 +58,13 @@ Explanation: ".*" means "zero or more (*) of any character (.)".
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 10 :lc-lang python3
|
||||
class Solution:
|
||||
def isMatch(self, s: str, p: str) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 10
|
||||
class Solution {
|
||||
public:
|
||||
bool isMatch(string s, string p) {
|
||||
|
||||
@@ -40,13 +40,13 @@ Explanation: From the top-left corner, there are a total of 3 ways to reach the
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 62 :lc-lang python3
|
||||
class Solution:
|
||||
def uniquePaths(self, m: int, n: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 62
|
||||
class Solution {
|
||||
public:
|
||||
int uniquePaths(int m, int n) {
|
||||
|
||||
@@ -52,13 +52,13 @@ exection -> execution (insert 'u')
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 72 :lc-lang python3
|
||||
class Solution:
|
||||
def minDistance(self, word1: str, word2: str) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 72
|
||||
class Solution {
|
||||
public:
|
||||
int minDistance(string word1, string word2) {
|
||||
|
||||
@@ -64,13 +64,13 @@ Output: true
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 97 :lc-lang python3
|
||||
class Solution:
|
||||
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 97
|
||||
class Solution {
|
||||
public:
|
||||
bool isInterleave(string s1, string s2, string s3) {
|
||||
|
||||
@@ -48,13 +48,13 @@ babgbag
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 115 :lc-lang python3
|
||||
class Solution:
|
||||
def numDistinct(self, s: str, t: str) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 115
|
||||
class Solution {
|
||||
public:
|
||||
int numDistinct(string s, string t) {
|
||||
|
||||
+2
-2
@@ -41,13 +41,13 @@ Output: 0
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 309 :lc-lang python3
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 309
|
||||
class Solution {
|
||||
public:
|
||||
int maxProfit(vector<int>& prices) {
|
||||
|
||||
@@ -43,13 +43,13 @@ Output: 10
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 312 :lc-lang python3
|
||||
class Solution:
|
||||
def maxCoins(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 312
|
||||
class Solution {
|
||||
public:
|
||||
int maxCoins(vector<int>& nums) {
|
||||
|
||||
+2
-2
@@ -51,13 +51,13 @@ Output: 1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 329 :lc-lang python3
|
||||
class Solution:
|
||||
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 329
|
||||
class Solution {
|
||||
public:
|
||||
int longestIncreasingPath(vector<vector<int>>& matrix) {
|
||||
|
||||
@@ -50,13 +50,13 @@ Output: 1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 494 :lc-lang python3
|
||||
class Solution:
|
||||
def findTargetSumWays(self, nums: List[int], target: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 494
|
||||
class Solution {
|
||||
public:
|
||||
int findTargetSumWays(vector<int>& nums, int target) {
|
||||
|
||||
@@ -59,13 +59,13 @@ Output: 1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 518 :lc-lang python3
|
||||
class Solution:
|
||||
def change(self, amount: int, coins: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 518
|
||||
class Solution {
|
||||
public:
|
||||
int change(int amount, vector<int>& coins) {
|
||||
|
||||
@@ -52,13 +52,13 @@ Explanation: There is no such common subsequence, so the result is 0.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1143 :lc-lang python3
|
||||
class Solution:
|
||||
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1143
|
||||
class Solution {
|
||||
public:
|
||||
int longestCommonSubsequence(string text1, string text2) {
|
||||
|
||||
@@ -57,13 +57,13 @@ Output: 68
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1220 :lc-lang python3
|
||||
class Solution:
|
||||
def countVowelPermutation(self, n: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1220
|
||||
class Solution {
|
||||
public:
|
||||
int countVowelPermutation(int n) {
|
||||
|
||||
+2
-2
@@ -52,13 +52,13 @@ Explanation: It is optimal to choose the subsequence [6,1,5] with alternating su
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1911 :lc-lang python3
|
||||
class Solution:
|
||||
def maxAlternatingSum(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1911
|
||||
class Solution {
|
||||
public:
|
||||
long long maxAlternatingSum(vector<int>& nums) {
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 269 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 269
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -49,13 +49,13 @@ Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 332 :lc-lang python3
|
||||
class Solution:
|
||||
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 332
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> findItinerary(vector<vector<string>>& tickets) {
|
||||
|
||||
@@ -55,13 +55,13 @@ Output: -1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 743 :lc-lang python3
|
||||
class Solution:
|
||||
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 743
|
||||
class Solution {
|
||||
public:
|
||||
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
|
||||
|
||||
@@ -53,13 +53,13 @@ We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 778 :lc-lang python3
|
||||
class Solution:
|
||||
def swimInWater(self, grid: List[List[int]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 778
|
||||
class Solution {
|
||||
public:
|
||||
int swimInWater(vector<vector<int>>& grid) {
|
||||
|
||||
@@ -69,13 +69,13 @@ The optimal path with no stops from city 0 to 2 is marked in red and has cost 50
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 787 :lc-lang python3
|
||||
class Solution:
|
||||
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 787
|
||||
class Solution {
|
||||
public:
|
||||
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
|
||||
|
||||
@@ -44,13 +44,13 @@ Output: 18
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1584 :lc-lang python3
|
||||
class Solution:
|
||||
def minCostConnectPoints(self, points: List[List[int]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1584
|
||||
class Solution {
|
||||
public:
|
||||
int minCostConnectPoints(vector<vector<int>>& points) {
|
||||
|
||||
+2
-2
@@ -61,13 +61,13 @@ It can be shown that no grouping is possible.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 2493 :lc-lang python3
|
||||
class Solution:
|
||||
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 2493
|
||||
class Solution {
|
||||
public:
|
||||
int magnificentSets(int n, vector<vector<int>>& edges) {
|
||||
|
||||
@@ -69,13 +69,13 @@ It can be shown that there are no other paths with a higher safeness factor.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 2812 :lc-lang python3
|
||||
class Solution:
|
||||
def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 2812
|
||||
class Solution {
|
||||
public:
|
||||
int maximumSafenessFactor(vector<vector<int>>& grid) {
|
||||
|
||||
@@ -54,17 +54,40 @@ Output: [0,1]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1 :lc-lang python3
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
sb = {}
|
||||
for xi, x in enumerate(nums):
|
||||
want = target - x;
|
||||
if want in sb:
|
||||
return sb[want], xi
|
||||
sb[x] = xi
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1
|
||||
#include <algorithm>
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
std::sort(nums.begin(), nums.end());
|
||||
for (int i=0; i< nums.size(); i++) {
|
||||
int x = nums[i];
|
||||
if (x > target) {
|
||||
return {};
|
||||
}
|
||||
// TODO: c++ binary search, i forgot how to do this
|
||||
int want = target - x
|
||||
auto it = std::binary_learch(nums.begin() + i + 1, nums.end(), want);
|
||||
if (it != nums.end() && *it == want) {
|
||||
int wi = std::distance(nums.begin(), it)
|
||||
return {i, wi}
|
||||
}
|
||||
}
|
||||
reuturn {};
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
|
||||
@@ -67,13 +67,13 @@ Explanation: Same as Example 1, except with the 5 in the top left corner being m
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 36 :lc-lang python3
|
||||
class Solution:
|
||||
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 36
|
||||
class Solution {
|
||||
public:
|
||||
bool isValidSudoku(vector<vector<char>>& board) {
|
||||
|
||||
@@ -44,13 +44,13 @@ Given an array of strings ~strs~, group the anagrams together. You can return th
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 49 :lc-lang python3
|
||||
class Solution:
|
||||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 49
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<string>> groupAnagrams(vector<string>& strs) {
|
||||
|
||||
@@ -46,13 +46,13 @@ Output: 3
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 128 :lc-lang python3
|
||||
class Solution:
|
||||
def longestConsecutive(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 128
|
||||
class Solution {
|
||||
public:
|
||||
int longestConsecutive(vector<int>& nums) {
|
||||
|
||||
@@ -42,7 +42,7 @@ All elements are distinct.
|
||||
Write your approach here.
|
||||
|
||||
** DONE Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 217 :lc-lang python3
|
||||
class Solution:
|
||||
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||
s = set()
|
||||
@@ -54,7 +54,7 @@ class Solution:
|
||||
#+end_src
|
||||
|
||||
** DONE C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 217
|
||||
#include <vector>
|
||||
#include <set>
|
||||
class Solution {
|
||||
|
||||
@@ -40,13 +40,13 @@ Output: [0,0,9,0,0]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 238 :lc-lang python3
|
||||
class Solution:
|
||||
def productExceptSelf(self, nums: List[int]) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 238
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> productExceptSelf(vector<int>& nums) {
|
||||
|
||||
@@ -30,13 +30,13 @@ Given two strings ~s~ and ~t~, return ~true~ if ~t~ is an anagram of ~s~, and ~f
|
||||
Write your approach here.
|
||||
|
||||
** DONE Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 242 :lc-lang python3
|
||||
class Solution:
|
||||
def isAnagram(self, s: str, t: str) -> bool:
|
||||
#+end_src
|
||||
|
||||
** DONE C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 242
|
||||
#include <map>
|
||||
#include <string>
|
||||
class Solution {
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 271 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 271
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -40,13 +40,15 @@ Given an integer array ~nums~ and an integer ~k~, return /the/ ~k~ /most frequen
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 347 :lc-lang python3
|
||||
class Solution:
|
||||
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
|
||||
from collections import Counter
|
||||
return [n for n, _ in Counter(nums).most_common(k)]
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 347
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> topKFrequent(vector<int>& nums, int k) {
|
||||
|
||||
@@ -51,13 +51,43 @@ Explanation: No string of words is substring of another string.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1408 :lc-lang python3
|
||||
from collections import defaultdict as dd
|
||||
class Solution:
|
||||
def stringMatching(self, words: List[str]) -> List[str]:
|
||||
words = reversed(sorted(words))
|
||||
def squash(word, trie):
|
||||
lst = []
|
||||
for c in word:
|
||||
lst.append([trie, ''])
|
||||
for i in range(len(lst)):
|
||||
lst[i][0] = lst[i][0][c]
|
||||
lst[i][1] += c
|
||||
if not lst[i][0]['ads']:
|
||||
lst[i][0]['ads'] = {lst[i][1]: [word]}
|
||||
else:
|
||||
lst[i][0]['ads'][lst[i][1]].append(word)
|
||||
def in_trie(word, trie):
|
||||
for c in word:
|
||||
if c not in trie:
|
||||
return []
|
||||
trie = trie[c]
|
||||
return [(word, bw) for bw in trie['ads'][word]]
|
||||
trie_maker = lambda: dd(trie_maker)
|
||||
trie = trie_maker()
|
||||
ans = []
|
||||
for s in words:
|
||||
ans += in_trie(s, trie)
|
||||
squash(s, trie)
|
||||
return ans
|
||||
|
||||
|
||||
#+end_src
|
||||
|
||||
**
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1408
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> stringMatching(vector<string>& words) {
|
||||
|
||||
+2
-2
@@ -46,13 +46,13 @@ Output: [11,8,5,4,3,4]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1769 :lc-lang python3
|
||||
class Solution:
|
||||
def minOperations(self, boxes: str) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1769
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> minOperations(string boxes) {
|
||||
|
||||
@@ -52,13 +52,13 @@ Explanation: None of the passengers are older than 60.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 2678 :lc-lang python3
|
||||
class Solution:
|
||||
def countSeniors(self, details: List[str]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 2678
|
||||
class Solution {
|
||||
public:
|
||||
int countSeniors(vector<string>& details) {
|
||||
|
||||
@@ -36,13 +36,13 @@ Output: ["a","b","c"]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 17 :lc-lang python3
|
||||
class Solution:
|
||||
def letterCombinations(self, digits: str) -> List[str]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 17
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> letterCombinations(string digits) {
|
||||
|
||||
@@ -30,13 +30,13 @@ Output: ["()"]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 22 :lc-lang python3
|
||||
class Solution:
|
||||
def generateParenthesis(self, n: int) -> List[str]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 22
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> generateParenthesis(int n) {
|
||||
|
||||
@@ -55,13 +55,13 @@ Output: []
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 39 :lc-lang python3
|
||||
class Solution:
|
||||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 39
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
|
||||
|
||||
@@ -50,13 +50,13 @@ Output:
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 40 :lc-lang python3
|
||||
class Solution:
|
||||
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 40
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
|
||||
|
||||
@@ -41,13 +41,13 @@ Output: [[1]]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 46 :lc-lang python3
|
||||
class Solution:
|
||||
def permute(self, nums: List[int]) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 46
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> permute(vector<int>& nums) {
|
||||
|
||||
@@ -37,13 +37,13 @@ Output: [["Q"]]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 51 :lc-lang python3
|
||||
class Solution:
|
||||
def solveNQueens(self, n: int) -> List[List[str]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 51
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<string>> solveNQueens(int n) {
|
||||
|
||||
@@ -35,13 +35,13 @@ Output: 1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 52 :lc-lang python3
|
||||
class Solution:
|
||||
def totalNQueens(self, n: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 52
|
||||
class Solution {
|
||||
public:
|
||||
int totalNQueens(int n) {
|
||||
|
||||
@@ -39,13 +39,13 @@ Explanation: There is 1 choose 1 = 1 total combination.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 77 :lc-lang python3
|
||||
class Solution:
|
||||
def combine(self, n: int, k: int) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 77
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> combine(int n, int k) {
|
||||
|
||||
@@ -38,13 +38,13 @@ Output: [[],[0]]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 78 :lc-lang python3
|
||||
class Solution:
|
||||
def subsets(self, nums: List[int]) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 78
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> subsets(vector<int>& nums) {
|
||||
|
||||
@@ -53,13 +53,13 @@ Output: false
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 79 :lc-lang python3
|
||||
class Solution:
|
||||
def exist(self, board: List[List[str]], word: str) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 79
|
||||
class Solution {
|
||||
public:
|
||||
bool exist(vector<vector<char>>& board, string word) {
|
||||
|
||||
@@ -34,13 +34,13 @@ Output: [[],[0]]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 90 :lc-lang python3
|
||||
class Solution:
|
||||
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 90
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
|
||||
|
||||
@@ -32,13 +32,13 @@ Output: [["a"]]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 131 :lc-lang python3
|
||||
class Solution:
|
||||
def partition(self, s: str) -> List[List[str]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 131
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<string>> partition(string s) {
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 351 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 351
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -46,13 +46,13 @@ Output: 1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1079 :lc-lang python3
|
||||
class Solution:
|
||||
def numTilePossibilities(self, tiles: str) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1079
|
||||
class Solution {
|
||||
public:
|
||||
int numTilePossibilities(string tiles) {
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 1863 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 1863
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -46,13 +46,13 @@ Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 4 :lc-lang python3
|
||||
class Solution:
|
||||
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 4
|
||||
class Solution {
|
||||
public:
|
||||
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
|
||||
|
||||
@@ -51,13 +51,13 @@ Output: -1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 33 :lc-lang python3
|
||||
class Solution:
|
||||
def search(self, nums: List[int], target: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 33
|
||||
class Solution {
|
||||
public:
|
||||
int search(vector<int>& nums, int target) {
|
||||
|
||||
@@ -46,13 +46,13 @@ Output: false
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 74 :lc-lang python3
|
||||
class Solution:
|
||||
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 74
|
||||
class Solution {
|
||||
public:
|
||||
bool searchMatrix(vector<vector<int>>& matrix, int target) {
|
||||
|
||||
@@ -62,13 +62,13 @@ Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 153 :lc-lang python3
|
||||
class Solution:
|
||||
def findMin(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 153
|
||||
class Solution {
|
||||
public:
|
||||
int findMin(vector<int>& nums) {
|
||||
|
||||
@@ -42,13 +42,13 @@ Explanation: 2 does not exist in nums so return -1
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 704 :lc-lang python3
|
||||
class Solution:
|
||||
def search(self, nums: List[int], target: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 704
|
||||
class Solution {
|
||||
public:
|
||||
int search(vector<int>& nums, int target) {
|
||||
|
||||
@@ -54,13 +54,13 @@ Output: 5
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 719 :lc-lang python3
|
||||
class Solution:
|
||||
def smallestDistancePair(self, nums: List[int], k: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 719
|
||||
class Solution {
|
||||
public:
|
||||
int smallestDistancePair(vector<int>& nums, int k) {
|
||||
|
||||
@@ -51,13 +51,13 @@ Output: 23
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 875 :lc-lang python3
|
||||
class Solution:
|
||||
def minEatingSpeed(self, piles: List[int], h: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 875
|
||||
class Solution {
|
||||
public:
|
||||
int minEatingSpeed(vector<int>& piles, int h) {
|
||||
|
||||
@@ -51,7 +51,7 @@ timeMap.get("foo", 5); // return "bar2"
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 981 :lc-lang python3
|
||||
class TimeMap:
|
||||
|
||||
def __init__(self):
|
||||
@@ -71,7 +71,7 @@ class TimeMap:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 981
|
||||
class TimeMap {
|
||||
public:
|
||||
TimeMap() {
|
||||
|
||||
@@ -54,13 +54,13 @@ Thus, [2,0,2] is returned.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 2300 :lc-lang python3
|
||||
class Solution:
|
||||
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 2300
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#+ANKI_DECK: study_deck_02
|
||||
* When to use lower_bound vs binary_search :cpp:binary-search:algorithm:retrieval::recognition:
|
||||
:PROPERTIES:
|
||||
:END:
|
||||
|
||||
** Front
|
||||
When should you use ~std::lower_bound~ instead of ~std::binary_search~?
|
||||
|
||||
** Back
|
||||
Use ~lower_bound~ when you need the *position/index* of the element.
|
||||
Use ~binary_search~ when you only need to know *if it exists* (bool).
|
||||
@@ -0,0 +1,10 @@
|
||||
#+ANKI_DECK: study_deck_02
|
||||
* Two Sum gotcha: sorting destroys original indices :cpp:two-sum:binary-search:retrieval::recognition:
|
||||
:PROPERTIES:
|
||||
:END:
|
||||
|
||||
** Front
|
||||
If you sort ~nums~ in-place for Two Sum, what goes wrong?
|
||||
|
||||
** Back
|
||||
Sorting changes the indices. If the problem requires returning *original* indices, store ~{value, original_index}~ pairs before sorting.
|
||||
@@ -0,0 +1,14 @@
|
||||
#+ANKI_DECK: study_deck_02
|
||||
* std::binary_search: check element exists :cpp:binary-search:algorithm:retrieval::recognition:
|
||||
:PROPERTIES:
|
||||
:END:
|
||||
|
||||
** Front
|
||||
What does ~std::binary_search~ return, and what must the range be?
|
||||
|
||||
** Back
|
||||
Returns ~bool~ (true if element found). The range must be *sorted*.
|
||||
|
||||
#+begin_src cpp
|
||||
bool found = std::binary_search(vec.begin(), vec.end(), value);
|
||||
#+end_src
|
||||
@@ -0,0 +1,14 @@
|
||||
#+ANKI_DECK: study_deck_02
|
||||
* std::lower_bound: find position of element :cpp:binary-search:algorithm:retrieval::recognition:
|
||||
:PROPERTIES:
|
||||
:END:
|
||||
|
||||
** Front
|
||||
What does ~std::lower_bound~ return?
|
||||
|
||||
** Back
|
||||
An iterator to the first element *>=* the given value. Returns ~end()~ if no such element exists.
|
||||
|
||||
#+begin_src cpp
|
||||
auto it = std::lower_bound(vec.begin(), vec.end(), value);
|
||||
#+end_src
|
||||
@@ -0,0 +1,16 @@
|
||||
#+ANKI_DECK: study_deck_02
|
||||
* Task: Two Sum complement lookup with lower_bound :cpp:binary-search:two-sum:retrieval::production:
|
||||
:PROPERTIES:
|
||||
:END:
|
||||
|
||||
** Front
|
||||
Given sorted ~nums~ and index ~i~, write C++ to check if ~target - nums[i]~ exists in the rest of the array using ~std::lower_bound~.
|
||||
|
||||
** Back
|
||||
#+begin_src cpp
|
||||
int complement = target - nums[i];
|
||||
auto it = std::lower_bound(nums.begin() + i + 1, nums.end(), complement);
|
||||
if (it != nums.end() && *it == complement) {
|
||||
return {(int)i, (int)std::distance(nums.begin(), it)};
|
||||
}
|
||||
#+end_src
|
||||
@@ -43,13 +43,13 @@ Output: 21
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 7 :lc-lang python3
|
||||
class Solution:
|
||||
def reverse(self, x: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 7
|
||||
class Solution {
|
||||
public:
|
||||
int reverse(int x) {
|
||||
|
||||
@@ -38,13 +38,13 @@ You must implement a solution with a linear runtime complexity and use only cons
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 136 :lc-lang python3
|
||||
class Solution:
|
||||
def singleNumber(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 136
|
||||
class Solution {
|
||||
public:
|
||||
int singleNumber(vector<int>& nums) {
|
||||
|
||||
@@ -52,13 +52,13 @@ Reverse bits of a given 32 bits signed integer.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 190 :lc-lang python3
|
||||
class Solution:
|
||||
def reverseBits(self, n: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 190
|
||||
class Solution {
|
||||
public:
|
||||
int reverseBits(int n) {
|
||||
|
||||
@@ -46,13 +46,13 @@ The input binary string *1111111111111111111111111111101* has a total of thirty
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 191 :lc-lang python3
|
||||
class Solution:
|
||||
def hammingWeight(self, n: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 191
|
||||
class Solution {
|
||||
public:
|
||||
int hammingWeight(int n) {
|
||||
|
||||
@@ -47,13 +47,13 @@ Output: false
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 231 :lc-lang python3
|
||||
class Solution:
|
||||
def isPowerOfTwo(self, n: int) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 231
|
||||
class Solution {
|
||||
public:
|
||||
bool isPowerOfTwo(int n) {
|
||||
|
||||
@@ -48,13 +48,13 @@ Output: [1,0]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 260 :lc-lang python3
|
||||
class Solution:
|
||||
def singleNumber(self, nums: List[int]) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 260
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> singleNumber(vector<int>& nums) {
|
||||
|
||||
@@ -52,13 +52,13 @@ Given an array ~nums~ containing ~n~ distinct numbers in the range ~[0, n]~, ret
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 268 :lc-lang python3
|
||||
class Solution:
|
||||
def missingNumber(self, nums: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 268
|
||||
class Solution {
|
||||
public:
|
||||
int missingNumber(vector<int>& nums) {
|
||||
|
||||
@@ -49,13 +49,13 @@ Explanation:
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 338 :lc-lang python3
|
||||
class Solution:
|
||||
def countBits(self, n: int) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 338
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> countBits(int n) {
|
||||
|
||||
@@ -30,13 +30,13 @@ Output: 5
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 371 :lc-lang python3
|
||||
class Solution:
|
||||
def getSum(self, a: int, b: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 371
|
||||
class Solution {
|
||||
public:
|
||||
int getSum(int a, int b) {
|
||||
|
||||
@@ -48,13 +48,13 @@ It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 2220 :lc-lang python3
|
||||
class Solution:
|
||||
def minBitFlips(self, start: int, goal: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 2220
|
||||
class Solution {
|
||||
public:
|
||||
int minBitFlips(int start, int goal) {
|
||||
|
||||
@@ -54,13 +54,13 @@ Explanation: The endWord "cog" is not in wordList, therefore there is no valid t
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 127 :lc-lang python3
|
||||
class Solution:
|
||||
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 127
|
||||
class Solution {
|
||||
public:
|
||||
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
|
||||
|
||||
@@ -44,7 +44,7 @@ In the above diagram, the bottom region is not captured because it is on the edg
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 130 :lc-lang python3
|
||||
class Solution:
|
||||
def solve(self, board: List[List[str]]) -> None:
|
||||
"""
|
||||
@@ -53,7 +53,7 @@ class Solution:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 130
|
||||
class Solution {
|
||||
public:
|
||||
void solve(vector<vector<char>>& board) {
|
||||
|
||||
@@ -77,7 +77,7 @@ Explanation: This an empty graph, it does not have any nodes.
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 133 :lc-lang python3
|
||||
"""
|
||||
# Definition for a Node.
|
||||
class Node:
|
||||
@@ -92,7 +92,7 @@ class Solution:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 133
|
||||
/*
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
|
||||
@@ -50,13 +50,13 @@ Output: 3
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 200 :lc-lang python3
|
||||
class Solution:
|
||||
def numIslands(self, grid: List[List[str]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 200
|
||||
class Solution {
|
||||
public:
|
||||
int numIslands(vector<vector<char>>& grid) {
|
||||
|
||||
@@ -48,13 +48,13 @@ To take course 1 you should have finished course 0, and to take course 0 you sho
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 207 :lc-lang python3
|
||||
class Solution:
|
||||
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 207
|
||||
class Solution {
|
||||
public:
|
||||
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
|
||||
|
||||
@@ -58,13 +58,13 @@ Output: [0]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 210 :lc-lang python3
|
||||
class Solution:
|
||||
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 210
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 261 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 261
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 286 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 286
|
||||
|
||||
#+end_src
|
||||
|
||||
+2
-2
@@ -8,11 +8,11 @@
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 323 :lc-lang python3
|
||||
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 323
|
||||
|
||||
#+end_src
|
||||
|
||||
@@ -61,13 +61,13 @@ Explanation: The water can flow from the only cell to the Pacific and Atlantic o
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 417 :lc-lang python3
|
||||
class Solution:
|
||||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 417
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
|
||||
|
||||
@@ -48,13 +48,13 @@ Output: [1,4]
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 684 :lc-lang python3
|
||||
class Solution:
|
||||
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 684
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
|
||||
|
||||
@@ -43,13 +43,13 @@ Output: 0
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
#+begin_src python :lc-problem 695 :lc-lang python3
|
||||
class Solution:
|
||||
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
#+begin_src cpp :lc-problem 695
|
||||
class Solution {
|
||||
public:
|
||||
int maxAreaOfIsland(vector<vector<int>>& grid) {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user