Skip to content

Commit 9861d3c

Browse files
committed
Updated tasks 208-1143
1 parent 2e12f39 commit 9861d3c

File tree

27 files changed

+133
-171
lines changed

27 files changed

+133
-171
lines changed

src/main/python/g0201_0300/s0208_implement_trie_prefix_tree/Trie.py

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,40 @@
11
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Design #Trie
22
# #LeetCode_75_Trie #Level_2_Day_16_Design #Udemy_Trie_and_Heap #Top_Interview_150_Trie
33
# #Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N)
4-
# #2025_07_25_Time_59_ms_(32.46%)_Space_35.26_MB_(14.23%)
4+
# #2025_07_25_Time_44_ms_(68.94%)_Space_32.08_MB_(76.05%)
55

66
class TrieNode:
7-
# Initialize your data structure here.
87
def __init__(self):
9-
self.children = [None] * 26
10-
self.isWord = False
8+
self.children = dict()
9+
self.isWordEnd = False
1110

1211
class Trie:
1312
def __init__(self):
1413
self.root = TrieNode()
15-
self.startWith = False
1614

17-
# Inserts a word into the trie.
18-
def insert(self, word):
19-
self._insert(word, self.root, 0)
20-
21-
def _insert(self, word, root, idx):
22-
if idx == len(word):
23-
root.isWord = True
24-
return
25-
index = ord(word[idx]) - ord('a')
26-
if root.children[index] is None:
27-
root.children[index] = TrieNode()
28-
self._insert(word, root.children[index], idx + 1)
29-
30-
# Returns if the word is in the trie.
31-
def search(self, word):
32-
return self._search(word, self.root, 0)
33-
34-
def _search(self, word, root, idx):
35-
if idx == len(word):
36-
self.startWith = True
37-
return root.isWord
38-
index = ord(word[idx]) - ord('a')
39-
if root.children[index] is None:
40-
self.startWith = False
41-
return False
42-
return self._search(word, root.children[index], idx + 1)
43-
44-
# Returns if there is any word in the trie
45-
# that starts with the given prefix.
46-
def startsWith(self, prefix):
47-
self.search(prefix)
48-
return self.startWith
15+
def insert(self, word: str) -> None:
16+
curr = self.root
17+
for c in word:
18+
if c not in curr.children:
19+
curr.children[c] = TrieNode()
20+
curr = curr.children[c]
21+
curr.isWordEnd = True
22+
23+
def search(self, word: str) -> bool:
24+
curr = self.root
25+
for c in word:
26+
if c not in curr.children:
27+
return False
28+
curr = curr.children[c]
29+
return curr.isWordEnd
30+
31+
def startsWith(self, prefix: str) -> bool:
32+
curr = self.root
33+
for c in prefix:
34+
if c not in curr.children:
35+
return False
36+
curr = curr.children[c]
37+
return True
4938

5039
# Your Trie object will be instantiated and called as such:
5140
# obj = Trie()
Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion
22
# #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
3-
# #2025_07_25_Time_36_ms_(47.14%)_Space_34.79_MB_(83.78%)
3+
# #2025_07_25_Time_23_ms_(81.92%)_Space_39.40_MB_(37.99%)
44

55
# Definition for singly-linked list.
66
# class ListNode:
@@ -9,31 +9,15 @@
99
# self.next = next
1010
class Solution:
1111
def isPalindrome(self, head: Optional[ListNode]) -> bool:
12-
# Calculate the length of the linked list
13-
length = 0
14-
right = head
15-
while right:
16-
right = right.next
17-
length += 1
18-
19-
# Reverse the right half of the list
20-
length //= 2
21-
right = head
22-
for _ in range(length):
23-
right = right.next
24-
25-
prev = None
26-
while right:
27-
next_node = right.next
28-
right.next = prev
29-
prev = right
30-
right = next_node
31-
32-
# Compare the left half and the right half
33-
for _ in range(length):
34-
if head and prev and head.val == prev.val:
35-
head = head.next
36-
prev = prev.next
37-
else:
12+
arr = []
13+
while head:
14+
arr.append(head.val)
15+
head = head.next
16+
l = 0
17+
r = len(arr) -1
18+
while l <= r:
19+
if arr[l] != arr[r]:
3820
return False
21+
l+=1
22+
r-=1
3923
return True
Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
# #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
22
# #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
3-
# #2025_07_25_Time_184_ms_(37.84%)_Space_31.28_MB_(63.38%)
4-
5-
from collections import deque
3+
# #2025_07_25_Time_152_ms_(81.96%)_Space_30.79_MB_(96.65%)
64

75
class Solution:
86
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
9-
n = len(nums)
10-
res = [0] * (n - k + 1)
11-
dq = deque()
12-
i, j = 0, 0
13-
x = 0
14-
15-
while j < len(nums):
16-
while dq and dq[-1] < nums[j]:
17-
dq.pop()
18-
dq.append(nums[j])
19-
20-
if j - i + 1 == k:
21-
res[x] = dq[0]
22-
x += 1
23-
if dq[0] == nums[i]:
24-
dq.popleft()
25-
i += 1
26-
j += 1
27-
28-
return res
7+
q = deque()
8+
result = []
9+
for right in range(len(nums)):
10+
while q and nums[right] > nums[q[-1]]:
11+
q.pop()
12+
q.append(right)
13+
if q[0] <= right - k:
14+
q.popleft()
15+
if right >= k - 1:
16+
result.append(nums[q[0]])
17+
return result
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree
22
# #LeetCode_75_Binary_Tree/DFS #Level_2_Day_7_Tree #Big_O_Time_O(n)_Space_O(n)
3-
# #2025_07_25_Time_310_ms_(27.21%)_Space_18.22_MB_(71.08%)
3+
# #2025_07_25_Time_6_ms_(68.63%)_Space_18.23_MB_(71.08%)
44

55
# Definition for a binary tree node.
66
# class TreeNode:
@@ -9,24 +9,20 @@
99
# self.left = left
1010
# self.right = right
1111
class Solution:
12-
def __init__(self):
13-
self.count = 0
12+
def pathSum(self, root: TreeNode, targetSum: int) -> int:
13+
def dfs(node: TreeNode, targetSum: int, curr_sum: int) -> None:
14+
if not node:
15+
return
1416

15-
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
16-
if root is None:
17-
return 0
18-
self.helper(root, targetSum, 0)
19-
self.pathSum(root.left, targetSum)
20-
self.pathSum(root.right, targetSum)
21-
return self.count
17+
curr_sum += node.val
18+
self.count += self.prefix_sum.get(curr_sum - targetSum, 0)
19+
self.prefix_sum[curr_sum] = self.prefix_sum.get(curr_sum, 0) + 1
20+
dfs(node.left, targetSum, curr_sum)
21+
dfs(node.right, targetSum, curr_sum)
22+
23+
self.prefix_sum[curr_sum] -= 1
2224

23-
def helper(self, node: TreeNode, targetSum: int, currSum: int) -> None:
24-
if node is None:
25-
return
26-
currSum += node.val
27-
if targetSum == currSum:
28-
self.count += 1
29-
if node.left is not None:
30-
self.helper(node.left, targetSum, currSum)
31-
if node.right is not None:
32-
self.helper(node.right, targetSum, currSum)
25+
self.count = 0
26+
self.prefix_sum = {0: 1}
27+
dfs(root, targetSum, 0)
28+
return self.count
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
# #Medium #Top_100_Liked_Questions #Array #Hash_Table #Prefix_Sum #Data_Structure_II_Day_5_Array
2-
# #Big_O_Time_O(n)_Space_O(n) #2025_07_25_Time_39_ms_(36.13%)_Space_20.37_MB_(71.80%)
2+
# #Big_O_Time_O(n)_Space_O(n) #2025_07_25_Time_27_ms_(84.54%)_Space_20.34_MB_(71.80%)
33

44
from collections import defaultdict
55

66
class Solution:
77
def subarraySum(self, nums: List[int], k: int) -> int:
8-
tempSum = 0
9-
ret = 0
10-
sumCount = defaultdict(int)
11-
sumCount[0] = 1
12-
13-
for i in nums:
14-
tempSum += i
15-
if tempSum - k in sumCount:
16-
ret += sumCount[tempSum - k]
17-
sumCount[tempSum] += 1
8+
sub_num = {0:1}
9+
total, count = 0, 0
10+
11+
for n in nums:
12+
total += n
13+
if total - k in sub_num:
14+
count += sub_num[total-k]
15+
sub_num[total] = 1 + sub_num.get(total, 0)
1816

19-
return ret
17+
return count
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# #Medium #String #Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n)
2-
# #2025_07_25_Time_141_ms_(40.24%)_Space_17.93_MB_(34.39%)
2+
# #2025_07_25_Time_4_ms_(99.42%)_Space_17.67_MB_(87.53%)
33

44
class Solution:
5-
def expand(self, a: List[str], l: int, r: int, res: List[int]) -> None:
6-
while l >= 0 and r < len(a):
7-
if a[l] != a[r]:
8-
return
9-
else:
10-
res[0] += 1
11-
l -= 1
12-
r += 1
13-
145
def countSubstrings(self, s: str) -> int:
15-
a = list(s)
16-
res = [0]
17-
for i in range(len(a)):
18-
self.expand(a, i, i, res)
19-
self.expand(a, i, i + 1, res)
20-
return res[0]
6+
result = 0
7+
i = 0
8+
while i < len(s):
9+
l = i
10+
while i < len(s) and s[i] == s[l]:
11+
i+=1
12+
r = i - 1
13+
for j in range(r-l+1):
14+
result += j+1
15+
while l-1>=0 and r+1 < len(s) and s[l-1] == s[r+1]:
16+
result+=1
17+
18+
l-=1
19+
r+=1
20+
return result

src/main/racket/g0001_0100/s0006_zigzag_conversion/Solution.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; #Medium #String #Top_Interview_150_Array/String
1+
; #Medium #String #Top_Interview_150_Array/String #Big_O_Time_O(n)_Space_O(n)
22
; #2025_02_03_Time_57_ms_(100.00%)_Space_130.82_MB_(60.00%)
33

44
(define/contract (convert s numRows)

src/main/racket/g0001_0100/s0007_reverse_integer/Solution.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; #Medium #Top_Interview_Questions #Math #Udemy_Integers
1+
; #Medium #Top_Interview_Questions #Math #Udemy_Integers #Big_O_Time_O(log10(x))_Space_O(1)
22
; #2025_02_03_Time_204_ms_(100.00%)_Space_101.45_MB_(100.00%)
33

44
(define/contract (reverse x)

src/main/racket/g0001_0100/s0008_string_to_integer_atoi/Solution.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; #Medium #Top_Interview_Questions #String
1+
; #Medium #Top_Interview_Questions #String #Big_O_Time_O(n)_Space_O(n)
22
; #2025_02_03_Time_3_ms_(100.00%)_Space_101.64_MB_(100.00%)
33

44
(define/contract (my-atoi s)

src/main/racket/g0001_0100/s0009_palindrome_number/Solution.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; #Easy #Math #Udemy_Integers #Top_Interview_150_Math
1+
; #Easy #Math #Udemy_Integers #Top_Interview_150_Math #Big_O_Time_O(log10(x))_Space_O(1)
22
; #2025_02_03_Time_8_ms_(100.00%)_Space_129.01_MB_(88.24%)
33

44
(define/contract (is-palindrome x)

0 commit comments

Comments
 (0)