Skip to content

Commit 803fb76

Browse files
committed
no message
1 parent a2551c9 commit 803fb76

10 files changed

+215
-8
lines changed

problems/binary-subarrays-with-sum.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def numSubarraysWithSum(self, nums, goal):
3+
#number of subarrays with sum at most "goal"
4+
def atMost(goal):
5+
ans = 0
6+
total = 0
7+
i = 0
8+
9+
for j, num in enumerate(nums):
10+
if num==1: total += 1
11+
12+
while i<len(nums) and total>goal:
13+
if nums[i]==1: total -= 1
14+
i += 1
15+
16+
ans += j-i+1 #number of subarrays that can generate from nums[i~j]
17+
return ans
18+
19+
return atMost(goal)-atMost(goal-1) if goal>0 else atMost(goal)

problems/coloring-a-border.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
A node is on the "border" if
3+
1. It is on the actual border of the grid.
4+
Or
5+
2. Any of its neighbor has different color.
6+
7+
BFS the grid starting from (row, col), if the node "isBorder" add it to nodesToColor.
8+
Color the nodes in the nodesToColor.
9+
10+
Time: O(N)
11+
Space: O(N)
12+
"""
13+
class Solution(object):
14+
def colorBorder(self, grid, row, col, color):
15+
def isValid(i, j):
16+
return i>=0 and j>=0 and i<len(grid) and j<len(grid[0])
17+
18+
def isBorder(i, j, color):
19+
if i==0 or i==len(grid)-1 or j==0 or j==len(grid[0])-1: return True
20+
if any(grid[iNei][jNei]!=color for iNei, jNei in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]): return True
21+
return False
22+
23+
q = collections.deque([(row, col)])
24+
nodesToColor = []
25+
visited = set()
26+
27+
while q:
28+
i, j = q.popleft()
29+
30+
if not isValid(i, j): continue
31+
if grid[i][j]!=grid[row][col]: continue
32+
if (i, j) in visited: continue
33+
visited.add((i, j))
34+
35+
if isBorder(i, j, grid[row][col]): nodesToColor.append((i, j))
36+
37+
for iNext, jNext in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
38+
q.append((iNext, jNext))
39+
40+
for i, j in nodesToColor:
41+
grid[i][j] = color
42+
43+
return grid
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def numberOfSubarrays(self, nums, K):
3+
def atMost(k):
4+
oddCount = 0
5+
ans = 0
6+
i = 0
7+
8+
for j in xrange(len(nums)):
9+
if nums[j]%2!=0: oddCount += 1
10+
11+
while oddCount>k:
12+
if nums[i]%2!=0: oddCount -= 1
13+
i += 1
14+
15+
ans += j-i+1
16+
17+
return ans
18+
19+
return atMost(K)-atMost(K-1)

problems/delete-duplicate-folders-in-system.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ def __init__(self, val):
44
self.key = None
55
self.children = {}
66

7-
87
class Solution(object):
98
def deleteDuplicateFolder(self, paths):
109
def setKey(node):
11-
if not node.children:
12-
node.key = node.val
13-
else:
14-
node.key = ''
15-
for c in sorted(node.children.keys()):
16-
setKey(node.children[c])
17-
node.key += node.children[c].val + '|' + node.children[c].key + '|'
10+
node.key = ''
11+
for c in sorted(node.children.keys()): #need to be sorted. so when child structs are the same, we won't generate different key from different iteration order.
12+
setKey(node.children[c])
13+
node.key += node.children[c].val + '|' + node.children[c].key + '|' #generate a key for each node. only considering its children structure. (see the "identical" definition, it does not consider the val of the node itself.)
1814

1915
keyCount[node.key] += 1
2016

problems/fruit-into-baskets.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,26 @@ def totalFruit(self, tree):
4444
counter = max(counter, i-start+1)
4545
mark[t] = i
4646
return counter
47+
48+
49+
"""
50+
Find the length of longest subarray that at most has 2 unique number.
51+
"""
52+
class Solution(object):
53+
def totalFruit(self, fruits):
54+
ans = 0
55+
uniqueFruits = 0
56+
i = 0
57+
counter = collections.Counter()
58+
59+
for j, fruit in enumerate(fruits):
60+
counter[fruit] += 1
61+
if counter[fruit]==1: uniqueFruits += 1
62+
63+
while uniqueFruits>2:
64+
counter[fruits[i]] -= 1
65+
if counter[fruits[i]]==0: uniqueFruits -= 1
66+
i += 1
67+
68+
ans = max(ans, j-i+1)
69+
return ans

problems/max-consecutive-ones-iii.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def longestOnes(self, nums, k):
3+
ans = 0
4+
zeroCount = 0
5+
i = 0
6+
7+
for j, num in enumerate(nums):
8+
if num==0: zeroCount += 1
9+
10+
while zeroCount>k:
11+
if nums[i]==0: zeroCount -= 1
12+
i += 1
13+
ans = max(ans, j-i+1)
14+
15+
return ans

problems/minimum-knight-moves.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Bidirectional BFS
2+
class Solution(object):
3+
def minKnightMoves(self, x, y):
4+
offsets = [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)]
5+
q1 = collections.deque([(0, 0)])
6+
q2 = collections.deque([(x, y)])
7+
steps1 = {(0, 0): 0} #steps needed starting from (0, 0)
8+
steps2 = {(x, y): 0} #steps needed starting from (x,y)
9+
10+
while q1 and q2:
11+
i1, j1 = q1.popleft()
12+
if (i1, j1) in steps2: return steps1[(i1, j1)]+steps2[(i1, j1)]
13+
14+
i2, j2 = q2.popleft()
15+
if (i2, j2) in steps1: return steps1[(i2, j2)]+steps2[(i2, j2)]
16+
17+
for ox, oy in offsets:
18+
nextI1 = i1+ox
19+
nextJ1 = j1+oy
20+
if (nextI1, nextJ1) not in steps1:
21+
q1.append((nextI1, nextJ1))
22+
steps1[(nextI1, nextJ1)] = steps1[(i1, j1)]+1
23+
24+
nextI2 = i2+ox
25+
nextJ2 = j2+oy
26+
if (nextI2, nextJ2) not in steps2:
27+
q2.append((nextI2, nextJ2))
28+
steps2[(nextI2, nextJ2)] = steps2[(i2, j2)]+1
29+
30+
return float('inf')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def numberOfSubstrings(self, s):
3+
#number of subarrays that at most have k unique char
4+
def atMost(k):
5+
counter = collections.Counter()
6+
uniqueCount = 0
7+
ans = 0
8+
i = 0
9+
10+
for j, c in enumerate(s):
11+
counter[c] += 1
12+
if counter[c]==1: uniqueCount += 1
13+
14+
while uniqueCount>k:
15+
counter[s[i]] -= 1
16+
if counter[s[i]]==0: uniqueCount-= 1
17+
i += 1
18+
ans += j-i+1
19+
return ans
20+
21+
n = len(s)
22+
return atMost(3) - atMost(2)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def balancedString(self, s):
3+
n = len(s)
4+
counter = collections.Counter(s)
5+
i = 0
6+
ans = n
7+
8+
for j, c in enumerate(s):
9+
counter[c] -= 1
10+
11+
while i<n and all(counter[c]<=n/4 for c in 'QEWR'):
12+
counter[s[i]] += 1
13+
ans = min(ans, j-i+1)
14+
i += 1
15+
return ans
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def subarraysWithKDistinct(self, nums, K):
3+
#number of subarray of nums which have at most k different numbers.
4+
def atMost(k):
5+
i = 0
6+
ans = 0
7+
counter = collections.Counter()
8+
uniqueCount = 0
9+
10+
for j in xrange(len(nums)):
11+
counter[nums[j]] += 1
12+
if counter[nums[j]]==1: uniqueCount += 1
13+
14+
while uniqueCount>k:
15+
counter[nums[i]] -= 1
16+
if counter[nums[i]]==0: uniqueCount -= 1
17+
i += 1
18+
19+
# the logest subarray that ends at j is nums[i:j+1]
20+
# nums[i:j+1] can produce j-i+1 subarrays that at most has k different number.
21+
ans += j-i+1
22+
23+
return ans
24+
25+
return atMost(K)-atMost(K-1)

0 commit comments

Comments
 (0)