Skip to content

Commit 13578b4

Browse files
committed
update
1 parent 8a0b280 commit 13578b4

7 files changed

+180
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
pre calculate all the accumulated sum, thus when changing the i and j, we don't need to use a for loop to do sum again,
3+
we just need to use the j sum substract i sum
4+
'''
5+
6+
lass NumArray:
7+
8+
def __init__(self, nums: List[int]):
9+
self.presum = nums # pass by pointer!
10+
for i in range(len(nums)-1):
11+
self.presum[i+1] += self.presum[i]
12+
13+
14+
15+
def sumRange(self, left: int, right: int) -> int:
16+
if left == 0: return self.presum[right]
17+
return self.presum[right] - self.presum[left-1]

Linked List/21. Merge Two Sorted Lists.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
3030
l2 = l2.next
3131
cur = cur.next
3232
cur.next = l1 or l2
33-
return dummy.next
33+
return dummy.next
34+
35+
36+
37+

Linked List/86. Partition List.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
7+
# Solution 1, split the notes by value into two lists, use append to keep order of the nodes,
8+
# re-connect the nodes in the two lists in the end
9+
# This solution is not inplace
10+
class Solution:
11+
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
12+
small=[]
13+
big=[]
14+
result = ListNode()
15+
marker=result
16+
while head:
17+
if head.val<x:
18+
small.append(head)
19+
print(f"small {head.val}")
20+
else:
21+
big.append(head)
22+
print(f"big {head.val}")
23+
head = head.next
24+
25+
for item in small:
26+
item.next=None
27+
marker.next=item
28+
marker = marker.next
29+
for item in big:
30+
item.next=None
31+
marker.next=item
32+
marker = marker.next
33+
return result.next
34+
35+
#Solution 2
36+
class Solution:
37+
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
38+
small = ListNode()
39+
big= ListNode()
40+
ps = small
41+
pb = big
42+
while head:
43+
print(f"head value {head.val}")
44+
if head.val <x:
45+
ps.next = head
46+
ps = head
47+
else:
48+
pb.next= head
49+
pb = head
50+
head = head.next
51+
pb.next=None
52+
53+
ps.next = big.next
54+
return small.next
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
move right pointer while adding visited to a map; compare max maxlen on the way with len(map)
3+
when reaching a visited one, find the initial index
4+
move and pop the left pointer to the initial index+1
5+
add the new duplicate and nex index to the visited map
6+
7+
'''
8+
9+
class Solution:
10+
def lengthOfLongestSubstring(self, s: str) -> int:
11+
maxlen=0
12+
left,right=0,0
13+
dict ={}
14+
for right in range(len(s)):
15+
c = s[right]
16+
if c not in dict:
17+
dict[c] = right
18+
maxlen = max(maxlen,len(dict))
19+
else:
20+
mark = dict[c]+1
21+
22+
for i in range(left,mark):
23+
dict.pop(s[i])
24+
dict[c] = right
25+
left = mark
26+
27+
return maxlen
28+
29+
30+
31+
32+
33+
34+

Sliding window/438. Find All Anagrams in a String.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,31 @@
1010
The following two parts are the same. just one use counter, one use dictionary
1111
"""
1212

13+
class Solution:
14+
def findAnagrams(self, s: str, p: str) -> List[int]:
15+
if len(p) >len(s):
16+
return
17+
l = len(p)
18+
dictp = defaultdict(int)
19+
window = defaultdict(int)
20+
for i in range(len(p)):
21+
dictp[ p[i] ] +=1
22+
window[ s[i] ] +=1
23+
result=[]
1324

25+
26+
for j in range(len(s)-len(p)+1):
27+
if window == dictp:
28+
result.append(j)
29+
if j +len(p) >= len(s):
30+
pass
31+
else:
32+
window[s[j+len(p)]]+=1
33+
window[s[j]] -=1
34+
if window[s[j]] ==0:
35+
window.pop(s[j])
36+
return result
37+
1438
class Solution:
1539
def findAnagrams(self, s: str, p: str) -> List[int]:
1640
l = len(p)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Very same to 438.
3+
Maintain a window dict that is the same length of the target substring
4+
shifting the window
5+
+1 to value of existed char on j+l position
6+
-1 to value of existed char on j position
7+
'''
8+
class Solution:
9+
def checkInclusion(self, s1: str, s2: str) -> bool:
10+
if len(s1)>len(s2):
11+
return
12+
target = defaultdict(int)
13+
window = defaultdict(int)
14+
l = len(s1)
15+
for index,char in enumerate(s1):
16+
target[char] +=1
17+
window[s2[index]] +=1
18+
19+
for i in range(len(s2)-l+1):
20+
if target == window:
21+
return True
22+
23+
if i+l >= len(s2):
24+
pass
25+
else:
26+
window[s2[i+l]] +=1
27+
window[s2[i]] -=1
28+
if window[s2[i]] ==0:
29+
window.pop(s2[i])
30+
return False

resource ocupation/1094. Car Pooling.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
1414

1515
return True
1616

17+
maxlen=0
18+
sub=[]
19+
dic={}
20+
st=0
21+
ed=0
22+
for i,char in enumerate(s) :
23+
if char not in dic:
24+
dic[char] = i
25+
maxlen = max(len(dic),maxlen)
26+
else:
27+
ed=dic[char]+1
28+
for p in range(st,ed):
29+
dic.pop(s[p])
30+
dic[char]=i
31+
st=ed
32+
return maxlen

0 commit comments

Comments
 (0)