Skip to content

Commit fd5bc7c

Browse files
committed
no message
1 parent 133c9bc commit fd5bc7c

6 files changed

+161
-1
lines changed

problems/count-binary-substrings.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def countBinarySubstrings(self, s):
3+
groups = []
4+
count = 0
5+
last = s[0]
6+
ans = 0
7+
8+
for bit in s:
9+
if bit==last:
10+
count += 1
11+
else:
12+
groups.append(count)
13+
count = 1
14+
last = bit
15+
16+
groups.append(count)
17+
18+
for i in xrange(1, len(groups)):
19+
ans += min(groups[i], groups[i-1])
20+
21+
return ans

problems/guess-the-word.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# """
2+
# This is Master's API interface.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
#class Master(object):
6+
# def guess(self, word):
7+
# """
8+
# :type word: str
9+
# :rtype int
10+
# """
11+
12+
class Solution(object):
13+
def findSecretWord(self, wordlist, master):
14+
def similarity(w1, w2):
15+
count = 0
16+
for i in xrange(6):
17+
if w1[i]==w2[i]: count += 1
18+
return count
19+
20+
for _ in xrange(10):
21+
22+
temp = []
23+
24+
word = random.choice(wordlist)
25+
k = master.guess(word)
26+
if k==6: return
27+
28+
for otherWord in wordlist:
29+
if otherWord==word: continue
30+
if similarity(word, otherWord)==k: temp.append(otherWord)
31+
32+
wordlist = temp
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
dp[i][j] := considering from points[0]~points[i], the max points if choosing points[i][j]
3+
"""
4+
class Solution(object):
5+
def maxPoints(self, points):
6+
N = len(points)
7+
M = len(points[0])
8+
dp = [[0]*M for _ in xrange(N)]
9+
10+
for i in xrange(N):
11+
for j in xrange(M):
12+
if i==0:
13+
dp[i][j] = points[i][j]
14+
else:
15+
dp[i][j] = points[i][j]+max([dp[i-1][k] - abs(k-j) for k in xrange(M)])
16+
return max(dp[N-1])
17+
18+
19+
"""
20+
The above solution will take O(NM^2) in time.
21+
The bottle neck is for each j we need to traverse the whole last row.
22+
Let us see a little bit closer on `dp[i][j]`
23+
24+
dp[i][j] = points[i][j]+max([dp[i-1][k] - abs(k-j) for k in xrange(M)])
25+
26+
So, if j>=k (Part 1)
27+
points[i][j]+max([dp[i-1][k] - (j-k) for k in xrange(M)])
28+
points[i][j] - j + max([dp[i-1][k] + k) for k in xrange(M)])
29+
30+
if k>=j (Part 2)
31+
points[i][j] + max([dp[i-1][k] - (k-j) for k in xrange(M)])
32+
points[i][j] + j + max([dp[i-1][k] - k) for k in xrange(M)])
33+
34+
Since we cannot do a full scan
35+
why not we update the value from left to right for Part 1 and
36+
right to left for part 2
37+
With a variable call rollingMax to store the max.
38+
39+
That way dp[i][j] will be updated as if we do a full scan.
40+
41+
The time complexity will become O(NM)
42+
"""
43+
class Solution(object):
44+
def maxPoints(self, points):
45+
N = len(points)
46+
M = len(points[0])
47+
dp = [[0]*M for _ in xrange(N)]
48+
49+
for j in xrange(M):
50+
dp[0][j] = points[0][j]
51+
52+
for i in xrange(1, N):
53+
rollingMax = float('-inf')
54+
for j in xrange(M):
55+
rollingMax = max(rollingMax, dp[i-1][j] - j)
56+
dp[i][j] = max(dp[i][j], points[i][j] + j + rollingMax))
57+
58+
rollingMax = float('-inf')
59+
for j in xrange(M, -1, -1):
60+
rollingMax = max(rollingMax, dp[i-1][j] + j)
61+
dp[i][j] = max(dp[i][j], points[i][j] - j + rollingMax))
62+
63+
return max(dp[N-1])

problems/maximum-subarray-sum-with-one-deletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def maximumSum(self, arr):
2323
dp[i][1] = arr[i]
2424
else:
2525
dp[i][0] = max(dp[i-1][0]+arr[i], arr[i])
26-
dp[i][1] = max(dp[i-1][0], dp[i-1][1]+arr[i], arr[i])
26+
dp[i][1] = max(dp[i-1][0], dp[i-1][1]+arr[i])
2727
subarrayMaxSum = max(subarrayMaxSum, dp[i][0], dp[i][1])
2828

2929
return subarrayMaxSum
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def shortestPath(self, grid, K):
3+
N = len(grid)
4+
M = len(grid[0])
5+
6+
q = collections.deque([(0, 0, 0, K)])
7+
qNext = collections.deque()
8+
visited = set()
9+
10+
while q:
11+
step, i0, j0, k0 = q.popleft()
12+
if (i0, j0, k0) in visited: continue
13+
visited.add((i0, j0, k0))
14+
15+
if i0==N-1 and j0==M-1: return step
16+
17+
for i, j in [(i0+1, j0), (i0-1, j0), (i0, j0+1), (i0, j0-1)]:
18+
if i>=N or i<0 or j>=M or j<0: continue
19+
if grid[i][j]==1 and k0>0:
20+
qNext.append((step+1, i, j, k0-1))
21+
elif grid[i][j]==0:
22+
qNext.append((step+1, i, j, k0))
23+
if not q: q = qNext
24+
25+
return -1

problems/snapshot-array.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class SnapshotArray(object):
2+
3+
def __init__(self, length):
4+
self.data = [[(-1, 0)] for _ in xrange(length)]
5+
self.snapId = 0
6+
7+
8+
def set(self, index, val):
9+
self.data[index].append((self.snapId, val))
10+
11+
12+
def snap(self):
13+
self.snapId += 1
14+
return self.snapId - 1
15+
16+
17+
def get(self, index, snapId):
18+
j = bisect.bisect_right(self.data[index], (snapId, float('inf'))) - 1
19+
return self.data[index][j][1]

0 commit comments

Comments
 (0)