Skip to content

Commit 34e7330

Browse files
authored
Add files via upload
1 parent 870fdeb commit 34e7330

6 files changed

+149
-0
lines changed

Permutations - Leetcode 46.py

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

Ransom Note - Leetcode 383.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
3+
counter = {}
4+
5+
for c in magazine:
6+
if c in counter:
7+
counter[c] += 1
8+
else:
9+
counter[c] = 1
10+
11+
for c in ransomNote:
12+
if c not in counter:
13+
return False
14+
elif counter[c] == 1:
15+
del counter[c]
16+
else:
17+
counter[c] -= 1
18+
19+
return True

Rotting Oranges - Leetcode 994.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import deque
2+
class Solution:
3+
def orangesRotting(self, grid: List[List[int]]) -> int:
4+
EMPTY, FRESH, ROTTEN = 0, 1, 2
5+
m, n = len(grid), len(grid[0])
6+
num_fresh = 0
7+
q = deque()
8+
9+
for i in range(m):
10+
for j in range(n):
11+
if grid[i][j] == ROTTEN:
12+
q.append((i, j))
13+
elif grid[i][j] == FRESH:
14+
num_fresh += 1
15+
16+
if num_fresh == 0:
17+
return 0
18+
19+
num_minutes = -1
20+
while q:
21+
q_size = len(q)
22+
num_minutes += 1
23+
24+
for _ in range(q_size):
25+
i, j = q.popleft()
26+
for r, c in [(i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j)]:
27+
if 0 <= r < m and 0 <= c < n and grid[r][c] == FRESH:
28+
grid[r][c] = ROTTEN
29+
num_fresh -= 1
30+
q.append((r, c))
31+
32+
if num_fresh == 0:
33+
return num_minutes
34+
else:
35+
return -1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def searchInsert(self, nums: List[int], target: int) -> int:
3+
n = len(nums)
4+
l = 0
5+
r = n - 1
6+
7+
while l <= r:
8+
m = (l + r) // 2
9+
10+
if nums[m] < target:
11+
l = m + 1
12+
elif nums[m] > target:
13+
r = m - 1
14+
else:
15+
return m
16+
17+
if nums[m] < target:
18+
return m + 1
19+
else:
20+
return m

Subsets - Leetcode 78.py

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

Word Search - Leetcode 79.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
m = len(board)
4+
n = len(board[0])
5+
W = len(word)
6+
7+
if m == 1 and n == 1:
8+
return board[0][0] == word
9+
10+
def backtrack(pos, index):
11+
i, j = pos
12+
13+
if index == W:
14+
return True
15+
16+
if board[i][j] != word[index]:
17+
return False
18+
19+
char = board[i][j]
20+
board[i][j] = "#"
21+
22+
for i_off, j_off in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
23+
r, c = i + i_off, j + j_off
24+
25+
if 0 <= r < m and 0 <= c < n:
26+
if backtrack((r, c), index + 1):
27+
return True
28+
29+
board[i][j] = char
30+
return False
31+
32+
for i in range(m):
33+
for j in range(n):
34+
if backtrack((i, j), 0):
35+
return True
36+
37+
return False

0 commit comments

Comments
 (0)