Skip to content

Commit ac0ddec

Browse files
author
Chris Wu
committed
no message
1 parent 28f997a commit ac0ddec

7 files changed

+196
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
`p` is the index on s which already processed.
3+
"""
4+
class Solution(object):
5+
def findReplaceString(self, s, indices, sources, targets):
6+
p = -1
7+
ans = ''
8+
9+
memo = {}
10+
for i, index in enumerate(indices):
11+
memo[index] = (sources[i], targets[i])
12+
13+
for i in xrange(len(s)):
14+
if i<=p: continue
15+
16+
if i in memo and (s[i:i+len(memo[i][0])] if i+len(memo[i][0])<len(s) else s[i:])==memo[i][0]:
17+
ans += memo[i][1]
18+
p = i+len(memo[i][0])-1
19+
else:
20+
ans += s[i]
21+
p = i
22+
23+
return ans

problems/insert-interval.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def insert(self, intervals, newInterval):
3+
if not intervals: return [newInterval]
4+
5+
max_index = float('-inf')
6+
min_index = float('inf')
7+
s0 = newInterval[0]
8+
e0 = newInterval[1]
9+
10+
for i, interval in enumerate(intervals):
11+
s = interval[0]
12+
e = interval[1]
13+
14+
if not (newInterval[1]<s or e<newInterval[0]):
15+
s0 = min(s0, s)
16+
e0 = max(e0, e)
17+
max_index = max(max_index, i)
18+
min_index = min(min_index, i)
19+
20+
return intervals[:min_index]+[[s0, e0]]+intervals[max_index+1:] if min_index!=float('inf') else sorted(intervals+[[s0, e0]])

problems/merge-intervals.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@
1414
class Solution(object):
1515
def merge(self, intervals):
1616
result = []
17-
intervals.sort(key=lambda x: x.start) #[1]
17+
intervals.sort() #[1]
1818

1919
for inter in intervals:
20-
if len(result)>0 and result[-1].end>=inter.start: #[2]
21-
result[-1].end = max(result[-1].end, inter.end) #[3]
20+
if len(result)>0 and result[-1][1]>=inter[0]: #[2]
21+
result[-1][1] = max(result[-1][1], inter[1]) #[3]
2222
else: #[4]
2323
result.append(inter)
24-
return result
24+
return result
25+
26+
27+
class Solution(object):
28+
def merge(self, intervals):
29+
intervals.sort()
30+
31+
i = 0
32+
while i<len(intervals)-1:
33+
if intervals[i][1]>=intervals[i+1][0]:
34+
intervals = intervals[:i] + [[min(intervals[i][0], intervals[i+1][0]), max(intervals[i][1], intervals[i+1][1])]] + intervals[i+2:]
35+
else:
36+
i += 1
37+
return intervals

problems/minimum-size-subarray-sum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
As we move right the i and increament s, if the s >= target, we
3+
4+
"""
5+
class Solution(object):
6+
def minSubArrayLen(self, target, nums):
7+
s = 0
8+
l = 0
9+
ans = float('inf')
10+
11+
for i in xrange(len(nums)):
12+
s += nums[i]
13+
14+
while s>=target:
15+
ans = min(i-l+1, ans)
16+
s -= nums[l]
17+
l += 1
18+
19+
return ans if ans!=float('inf') else 0
20+

problems/partition-labels.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
1. Find each char's ranges. Store into `charRange`.
3+
2. Merge the ranges. Store into `r`.
4+
3. Change the `r` format.
5+
6+
For example:
7+
The input is "ababcbacadefegdehijhklij"
8+
charRange will be: `{'a': [0, 8], 'c': [4, 7], 'b': [1, 5], 'e': [10, 15], 'd': [9, 14], 'g': [13, 13], 'f': [11, 11], 'i': [17, 22], 'h': [16, 19], 'k': [20, 20], 'j': [18, 23], 'l': [21, 21]}`
9+
After merging above the range should be `[[0, 8], [9, 15], [16, 23]]`. And store it in `r`: `[8, 15, 23]`.
10+
`ans` will be `[9,7,8]`
11+
"""
12+
class Solution(object):
13+
def partitionLabels(self, s):
14+
charRange = {}
15+
r = []
16+
17+
for i, c in enumerate(s):
18+
if c not in charRange:
19+
charRange[c] = [i, i]
20+
else:
21+
charRange[c][1] = i
22+
23+
for i, c in enumerate(s):
24+
if i==0 or charRange[c][0]>r[-1]:
25+
r.append(charRange[c][1])
26+
else:
27+
r[-1] = max(r[-1], charRange[c][1])
28+
29+
ans = []
30+
for i in xrange(len(r)):
31+
if i==0:
32+
ans.append(r[i]+1)
33+
else:
34+
ans.append(r[i]-r[i-1])
35+
return ans
36+
37+
"""
38+
This offical answer is even cleaner.
39+
"""
40+
class Solution(object):
41+
def partitionLabels(self, s):
42+
lastSeen = {}
43+
for i, c in enumerate(s): lastSeen[c] = i
44+
45+
ans = []
46+
start = end = 0
47+
for i, c in enumerate(s):
48+
end = max(end, lastSeen[c])
49+
if i==end:
50+
ans.append(end-start+1)
51+
start = i+1
52+
return ans

problems/product-of-array-except-self.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,49 @@ def productExceptSelf(self, nums):
3636
output[i] = output[i]*t #[5]
3737
t = t*nums[i] #[4]
3838

39-
return output
39+
return output
40+
41+
"""
42+
Time: O(N)
43+
Space: O(N)
44+
45+
left[i] := product of all the element left to nums[i]
46+
right[i] := product of all the element right to nums[i]
47+
"""
48+
class Solution(object):
49+
def productExceptSelf(self, nums):
50+
N = len(nums)
51+
52+
left = [1]*N
53+
for i in xrange(1, len(nums)):
54+
left[i] = left[i-1] * nums[i-1]
55+
56+
right = [1]*N
57+
for i in xrange(N-2, -1, -1):
58+
right[i] = right[i+1] * nums[i+1]
59+
60+
ans = []
61+
for i in xrange(N):
62+
ans.append(left[i]*right[i])
63+
return ans
64+
65+
"""
66+
Time: O(N)
67+
Space: O(1)
68+
"""
69+
class Solution(object):
70+
def productExceptSelf(self, nums):
71+
N = len(nums)
72+
73+
#[1]
74+
opt = [1]*N
75+
for i in xrange(1, len(nums)):
76+
opt[i] = opt[i-1] * nums[i-1]
77+
78+
#[2]
79+
t = 1
80+
for i in xrange(N-2, -1, -1):
81+
t *= nums[i+1]
82+
opt[i] *= t
83+
84+
return opt

problems/summary-ranges.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
For each iteration, if we found a new starting point we will append previous range to the opt.
3+
The range start from index s to index i-1.
4+
"""
5+
class Solution(object):
6+
def summaryRanges(self, nums):
7+
nums.append('#')
8+
opt = []
9+
s = 0
10+
11+
for i in xrange(1, len(nums)):
12+
if nums[i-1]+1!=nums[i]:
13+
if i-1>s:
14+
opt.append(str(nums[s])+'->'+str(nums[i-1]))
15+
else:
16+
opt.append(str(nums[s]))
17+
s = i
18+
return opt

0 commit comments

Comments
 (0)