Skip to content

Commit 3fd9514

Browse files
author
Chris Wu
committed
no message
1 parent a0aaa90 commit 3fd9514

6 files changed

+111
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ https://github.com/wuduhren/leetcode-python
1111
For example, `merge-sorted-array.py`'s solution is at `https://leetcode.com/problems/merge-sorted-array/`.
1212

1313

14+
# Leetcode 74 Classic Problem
15+
https://leetcode.com/list/?selectedList=535ukjh5
16+
1417
# Leetcode Similar Problems
1518
I found it makes sense to solve similar problems together, so that we can recognize the problem faster when we encounter a new one. My suggestion is to skip the HARD problems when you first go through these list.
1619

@@ -188,6 +191,8 @@ If you are new or know nothing about data structures and algorithms, I recommend
188191

189192
4. [Narendra's Youtube Channel](https://www.youtube.com/channel/UCn1XnDWhsLS5URXTi5wtFTA/playlists)
190193

194+
5. [System Design Primer](https://github.com/donnemartin/system-design-primer)
195+
191196

192197
# Knowledge Base Question
193198
1. [Session vs Cookie](https://medium.com/@chriswrite/session-vs-cookie-software-engineer-top-asked-question-1-9bdbc0766739)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#DP
2+
"""
3+
Time: O(N)
4+
Space: O(N). Can reduce to O(1).
5+
6+
dp[i][0] max number n, which n%3==0, considering nums[0~i-1]
7+
dp[i][1] max number n, which n%3==1, considering nums[0~i-1]
8+
dp[i][2] max number n, which n%3==2, considering nums[0~i-1]
9+
"""
10+
class Solution(object):
11+
def maxSumDivThree(self, nums):
12+
dp = [[0]*3 for _ in xrange(len(nums)+1)]
13+
14+
for i in xrange(len(nums)+1):
15+
if i==0:
16+
dp[i][0] = 0
17+
dp[i][1] = float('-inf')
18+
dp[i][2] = float('-inf')
19+
continue
20+
21+
n = nums[i-1]
22+
23+
if n%3==0:
24+
dp[i][0] = max(dp[i-1][0], dp[i-1][0]+n)
25+
dp[i][1] = max(dp[i-1][1], dp[i-1][1]+n)
26+
dp[i][2] = max(dp[i-1][2], dp[i-1][2]+n)
27+
elif n%3==1:
28+
dp[i][0] = max(dp[i-1][0], dp[i-1][2]+n)
29+
dp[i][1] = max(dp[i-1][1], dp[i-1][0]+n)
30+
dp[i][2] = max(dp[i-1][2], dp[i-1][1]+n)
31+
elif n%3==2:
32+
dp[i][0] = max(dp[i-1][0], dp[i-1][1]+n)
33+
dp[i][1] = max(dp[i-1][1], dp[i-1][2]+n)
34+
dp[i][2] = max(dp[i-1][2], dp[i-1][0]+n)
35+
36+
return dp[-1][0]
37+
38+
# Greedy
39+
class Solution(object):
40+
def videoStitching(self, clips, T):
41+
if T==0: return 0
42+
if not clips: return -1
43+
44+
clips.sort()
45+
print clips
46+
47+
if clips[0][0]!=0: return -1
48+
if clips[0][1]>=T: return 1
49+
50+
count = 0
51+
i = 0
52+
rightMost = 0
53+
54+
while i<len(clips):
55+
right = rightMost
56+
while i<len(clips) and clips[i][0]<=rightMost:
57+
right = max(right, clips[i][1])
58+
i += 1
59+
60+
if rightMost==right: return -1 #rightMost cannot be update anymore
61+
62+
rightMost = right
63+
count += 1
64+
if rightMost >= T: return count
65+
66+
return -1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def minScoreTriangulation(self, values):
3+
N = len(values)
4+
dp = [[float('inf')]*N for _ in xrange(N)]
5+
6+
for i in xrange(N-1):
7+
dp[i][i+1] = 0
8+
9+
for l in xrange(3, N+1):
10+
for i in xrange(N-l+1):
11+
j = i+l-1
12+
13+
for k in xrange(i+1, j):
14+
dp[i][j] = min(dp[i][j], dp[i][k]+values[i]*values[k]*values[j]+dp[k][j])
15+
return dp[0][N-1]
16+

problems/partition-array-for-maximum-sum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
dp[i] = maxSumAfterPartitioning(A[0~i-1], K) and must contain A[i-1]
2+
dp[i] = max(k*max(A[i-k]~A[i]) + dp[i-k]) for k in 1~K.
33
"""
44
class Solution(object):
55
def maxSumAfterPartitioning(self, A, K):

problems/stone-game-ii.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def stoneGameII(self, piles):
3+
def helper(start, m):
4+
if (start, m) in history: return history[(start, m)]
5+
6+
if start>=len(piles): return 0
7+
if start+m*2>=len(piles): return sum(piles[start:])
8+
9+
stones = float('-inf')
10+
for x in xrange(1, m*2+1):
11+
stones = max(stones, sum(piles[start:])-helper(start+x, max(m, x)))
12+
13+
history[(start, m)] = stones
14+
return history[(start, m)]
15+
16+
history = {}
17+
return helper(0, 1)

problems/target-sum.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ def findTargetSumWays(self, nums, S):
1313

1414
return ans
1515

16-
import collections
17-
16+
"""
17+
dp[i][j] = number of ways to sum to j using nums[0~i-1]
18+
Time: O(SN)
19+
Space: O(SN)
20+
"""
1821

1922

23+
import collections
2024
class Solution(object):
2125
def findTargetSumWays(self, nums, target):
2226
S = sum(nums)

0 commit comments

Comments
 (0)