Skip to content

Commit 870fdeb

Browse files
authored
Add files via upload
1 parent 5180148 commit 870fdeb

9 files changed

+190
-0
lines changed

Baseball Game - Leetcode 682.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def calPoints(self, operations: List[str]) -> int:
3+
stk = []
4+
5+
for op in operations:
6+
if op == "+":
7+
stk.append(stk[-1] + stk[-2])
8+
elif op == "D":
9+
stk.append(stk[-1] * 2)
10+
elif op == "C":
11+
stk.pop()
12+
else:
13+
stk.append(int(op))
14+
return sum(stk)

Clone Graph - Leetcode 133.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Optional
2+
class Solution:
3+
def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]:
4+
if not node:
5+
return None
6+
7+
start = node
8+
o_to_n = {}
9+
stk = [start]
10+
visited = set()
11+
visited.add(start)
12+
13+
while stk:
14+
node = stk.pop()
15+
o_to_n[node] = Node(val=node.val)
16+
17+
for nei in node.neighbors:
18+
if nei not in visited:
19+
visited.add(nei)
20+
stk.append(nei)
21+
22+
for old_node, new_node in o_to_n.items():
23+
for nei in old_node.neighbors:
24+
new_nei = o_to_n[nei]
25+
new_node.neighbors.append(new_nei)
26+
27+
return o_to_n[start]

Combination Sum - Leetcode 39.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
res, sol = [], []
4+
nums = candidates
5+
n = len(nums)
6+
7+
def backtrack(i, cur_sum):
8+
if cur_sum == target:
9+
res.append(sol[:])
10+
return
11+
12+
if cur_sum > target or i == n:
13+
return
14+
15+
backtrack(i + 1, cur_sum)
16+
17+
sol.append(nums[i])
18+
backtrack(i, cur_sum + nums[i])
19+
sol.pop()
20+
21+
backtrack(0, 0)
22+
return res

Combinations - Leetcode 77.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def combine(self, n: int, k: int) -> List[List[int]]:
3+
ans, sol = [], []
4+
5+
def backtrack(x):
6+
if len(sol) == k:
7+
ans.append(sol[:])
8+
return
9+
10+
left = x
11+
still_need = k - len(sol)
12+
13+
if left > still_need:
14+
backtrack(x - 1)
15+
16+
sol.append(x)
17+
backtrack(x - 1)
18+
sol.pop()
19+
20+
backtrack(n)
21+
return ans

Generate Parentheses - Leetcode 22.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def generateParenthesis(self, n: int) -> List[str]:
3+
ans, sol = [], []
4+
5+
def backtrack(openn, close):
6+
if len(sol) == 2 * n:
7+
ans.append("".join(sol))
8+
return
9+
if openn < n:
10+
sol.append("(")
11+
backtrack(openn + 1, close)
12+
sol.pop()
13+
14+
if openn > close:
15+
sol.append(")")
16+
backtrack(openn, close + 1)
17+
sol.pop()
18+
19+
backtrack(0, 0)
20+
return ans
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def letterCombinations(self, digits: str) -> List[str]:
3+
if digits == "":
4+
return []
5+
6+
ans, sol = [], []
7+
8+
letter_map = {
9+
"2": "abc",
10+
"3": "def",
11+
"4": "ghi",
12+
"5": "jkl",
13+
"6": "mno",
14+
"7": "pqrs",
15+
"8": "tuv",
16+
"9": "wxyz",
17+
}
18+
19+
n = len(digits)
20+
21+
def backtrack(i=0):
22+
if i == n:
23+
ans.append("".join(sol))
24+
return
25+
26+
for letter in letter_map[digits[i]]:
27+
sol.append(letter)
28+
backtrack(i + 1)
29+
sol.pop()
30+
31+
backtrack(0)
32+
return ans

Max Area of Island - Leetcode 695.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
3+
m = len(grid)
4+
n = len(grid[0])
5+
6+
def dfs(i, j):
7+
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] != 1:
8+
return 0
9+
else:
10+
grid[i][j] = 0
11+
return 1 + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i - 1, j) + dfs(i, j + 1)
12+
13+
max_area = 0
14+
for i in range(m):
15+
for j in range(n):
16+
if grid[i][j] == 1:
17+
max_area = max(max_area, dfs(i, j))
18+
19+
return max_area
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxNumberOfBalloons(self, text: str) -> int:
3+
counter = defaultdict(int)
4+
balloon = "balloon"
5+
6+
for c in text:
7+
if c in balloon:
8+
counter[c] += 1
9+
10+
if any(c not in counter for c in balloon):
11+
return 0
12+
else:
13+
return min(counter["b"], counter["a"], counter["l"] // 2, counter["o"] // 2, counter["n"])

Number of Islands - Leetcode 200.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def numIslands(self, grid: List[List[str]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
5+
def dfs(i, j):
6+
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] != "1":
7+
return
8+
else:
9+
grid[i][j] = "0"
10+
dfs(i, j + 1)
11+
dfs(i + 1, j)
12+
dfs(i, j - 1)
13+
dfs(i - 1, j)
14+
15+
num_islands = 0
16+
for i in range(m):
17+
for j in range(n):
18+
if grid[i][j] == "1":
19+
num_islands += 1
20+
dfs(i, j)
21+
22+
return num_islands

0 commit comments

Comments
 (0)