Skip to content

Commit e159603

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents 9f4de7c + 1793d53 commit e159603

File tree

480 files changed

+16374
-452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

480 files changed

+16374
-452
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

200-Number-of-Islands.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ def numIslands(self, grid: List[List[str]]) -> int:
44
return 0
55

66
islands = 0
7-
q = collections.deque()
87
visit = set()
98
rows, cols = len(grid), len(grid[0])
109

212-Word-Search-II.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ class TrieNode:
22
def __init__(self):
33
self.children = {}
44
self.isWord = False
5+
self.refs = 0
56

67
def addWord(self, word):
78
cur = self
9+
cur.refs += 1
810
for c in word:
911
if c not in cur.children:
1012
cur.children[c] = TrieNode()
1113
cur = cur.children[c]
14+
cur.refs += 1
1215
cur.isWord = True
16+
17+
def removeWord(self, word):
18+
cur = self
19+
cur.refs -= 1
20+
for c in word:
21+
if c in cur.children:
22+
cur = cur.children[c]
23+
cur.refs -= 1
1324

1425

1526
class Solution:
@@ -24,14 +35,18 @@ def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
2435
def dfs(r, c, node, word):
2536
if (r < 0 or c < 0 or
2637
r == ROWS or c == COLS or
27-
board[r][c] not in node.children or (r, c) in visit):
38+
board[r][c] not in node.children or
39+
node.children[board[r][c]].refs < 1 or
40+
(r, c) in visit):
2841
return
2942

3043
visit.add((r, c))
3144
node = node.children[board[r][c]]
3245
word += board[r][c]
3346
if node.isWord:
47+
node.isWord = False
3448
res.add(word)
49+
root.removeWord(word)
3550

3651
dfs(r + 1, c, node, word)
3752
dfs(r - 1, c, node, word)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def removeDuplicates(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
k = 0
8+
i = 0
9+
while i < len(nums):
10+
val = nums[i]
11+
while i + 1 < len(nums) and nums[i+1] == val:
12+
nums.remove(val)
13+
k += 1
14+
i += 1
15+
return len(nums)
16+

27-Remove-Element.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def removeElement(self, nums, val):
3+
"""
4+
:type nums: List[int]
5+
:type val: int
6+
:rtype: int
7+
"""
8+
for i in range(nums.count(val)): # loop how many val is in the list
9+
nums.remove(val) # remove each val one by one
10+
return len(nums) # return len of nums

286-Walls-and-Gates.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ def addRooms(r, c):
1414
return
1515
visit.add((r, c))
1616
q.append([r, c])
17-
17+
18+
for r in range(ROWS):
19+
for c in range(COLS):
20+
if rooms[r][c] == 0:
21+
q.append([r, c])
22+
visit.add((r, c))
23+
1824
dist = 0
1925
while q:
2026
for i in range(len(q)):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class UnionFind:
2+
def __init__(self):
3+
self.f = {}
4+
5+
def findParent(self,x):
6+
y = self.f.get(x,x)
7+
if x!=y:
8+
y = self.f[x] = self.findParent(y)
9+
return y
10+
11+
def union(self,x,y):
12+
self.f[self.findParent(x)] = self.findParent(y)
13+
14+
class Solution:
15+
def countComponents(self, n: int, edges: List[List[int]]) -> int:
16+
dsu = UnionFind()
17+
for a,b in edges:
18+
dsu.union(a,b)
19+
return len(set(dsu.findParent(x) for x in range(n)))

416-Partition-Equal-Subset-Sum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def canPartition(self, nums: List[int]) -> bool:
1515
nextDP.add(t + nums[i])
1616
nextDP.add(t)
1717
dp = nextDP
18-
return True if target in dp else False
18+
return False
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
class Solution:
3+
def count_components(self, n: int, edges: List[List[int]]) -> int:
4+
parent = [i for i in range(n)]
5+
rank = [1] * n
6+
7+
def find_union(node: int) -> None:
8+
result = node
9+
while result != parent[result]:
10+
parent[result] = parent[parent[result]] # path compression
11+
result = parent[result]
12+
return result
13+
14+
def union(node_1: int, node_2: int):
15+
parent_1 = find_union(node_1)
16+
parent_2 = find_union(node_2)
17+
if parent_1 == parent_2:
18+
return 0
19+
if rank[parent_2] < rank[parent_1]:
20+
parent[parent_1] = parent_2
21+
rank[parent_2] += rank[parent_1]
22+
else:
23+
parent[parent_2] = parent_1
24+
rank[parent_1] += rank[parent_2]
25+
return 1
26+
27+
result = n
28+
for node_1, node_2 in edges:
29+
result -= union(node_1, node_2)
30+
return result
31+
File renamed without changes.

0 commit comments

Comments
 (0)