Skip to content

Commit b7dfe9c

Browse files
author
Chris Wu
committed
no message
1 parent c39681e commit b7dfe9c

7 files changed

+137
-2
lines changed

problems/last-stone-weight-ii.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ def lastStoneWeightII(self, stones):
2727
# Two collection of stones will be total-maxSum and maxSum
2828
# (total-maxSum) - maxSum => total-maxSum*2
2929
return total-maxSum*2
30+
31+
32+
"""
33+
dp[i][w] := if weight w is achievable using stones[:i]
34+
Find the smallest achivable w where w>=0.
35+
"""
36+
import collections
37+
class Solution(object):
38+
def lastStoneWeightII(self, stones):
39+
N = len(stones)
40+
W = sum(stones)
41+
dp = [collections.defaultdict(bool) for _ in xrange(N+1)]
42+
dp[0][0] = True
43+
44+
for i in xrange(1, N+1):
45+
for w in xrange(-W, W+1):
46+
dp[i][w] = dp[i-1][w+stones[i-1]] or dp[i-1][w-stones[i-1]]
47+
48+
for w, b in dp[N].iteritems():
49+
if b and w>=0: return w
50+
51+
return 0

problems/longest-palindromic-substring.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,26 @@ def findPalindorome(mid, mid2=None):
4141
max_pal = p2 if len(p2)>len(max_pal) else max_pal
4242

4343
return max_pal
44-
44+
45+
46+
#2021/6/18 DP TLE
47+
"""
48+
dp[i][j] := if s[i:j+1] is palindrom
49+
"""
50+
class Solution(object):
51+
def longestPalindrome(self, s):
52+
if not s: return s
53+
54+
N = len(s)
55+
dp = [[False for _ in xrange(N+1)] for _ in xrange(N+1)]
56+
for i in xrange(N+1): dp[i][i] = True
57+
ans = s[0]
58+
59+
for l in xrange(2, N+1):
60+
for i in xrange(1, N+1):
61+
j = i+l-1
62+
if j>N: continue
63+
dp[i][j] = s[i-1]==s[j-1] and (dp[i+1][j-1] or j-1<i+1)
64+
if dp[i][j]: ans = s[i-1:j]
65+
66+
return ans

problems/ones-and-zeroes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
dp[i][n][m] := max size of the subset with total m 0's and n 1's.
3+
Find max size for each m<=M and n<=N.
4+
"""
5+
class Solution(object):
6+
def findMaxForm(self, strs, M, N):
7+
dp = [[[0 for _ in xrange(M+1)] for _ in xrange(N+1)] for _ in xrange(len(strs)+1)]
8+
9+
for i in xrange(1, len(strs)+1):
10+
count0 = strs[i-1].count('0')
11+
count1 = len(strs[i-1])-count0
12+
13+
for n in xrange(N+1):
14+
for m in xrange(M+1):
15+
dp[i][n][m] = max(dp[i-1][n][m], (dp[i-1][n-count1][m-count0]+1) if m>=count0 and n>=count1 else 0)
16+
17+
ans = 0
18+
for n in xrange(N+1):
19+
for m in xrange(M+1):
20+
ans = max(ans, dp[-1][n][m])
21+
22+
return ans

problems/profitable-schemes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TLE
3+
dp[i][n][p] := considering profit[:i], what is the number of ways produce profit p with n people.
4+
"""
5+
class Solution(object):
6+
def profitableSchemes(self, maxMember, minProfit, group, profit):
7+
P = sum(profit)
8+
N = sum(group)
9+
dp = [[[0 for _ in xrange(P+1)] for _ in xrange(N+1)] for _ in xrange(len(profit)+1)]
10+
dp[0][0][0] = 1
11+
12+
count = 0
13+
for i in xrange(1, len(profit)+1):
14+
for n in xrange(N+1):
15+
for p in xrange(P+1):
16+
dp[i][n][p] = (dp[i-1][n-group[i-1]][p-profit[i-1]] if p-profit[i-1]>=0 and n-group[i-1]>=0 else 0) + dp[i-1][n][p]
17+
if i==len(profit) and p>=minProfit and n<=maxMember: count += dp[i][n][p]
18+
return count
19+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
j is the index to insert when a new number are found.
3+
j will never catch up i, so j will not mess up the check.
4+
"""
5+
class Solution(object):
6+
def removeDuplicates(self, nums):
7+
j = 1
8+
for i in xrange(1, len(nums)):
9+
if nums[i]!=nums[i-1]:
10+
nums[j] = nums[i]
11+
j += 1
12+
return j

problems/tallest-billboard.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#TLE
2+
class Solution(object):
3+
def tallestBillboard(self, rods):
4+
D = sum(rods)
5+
N = len(rods)
6+
7+
dp = [[float('-inf') for _ in xrange(-D, D+1)] for _ in xrange(N+1)]
8+
dp[0][D] = 0
9+
10+
for i in xrange(1, N+1):
11+
for d in xrange(-D, D+1):
12+
h = rods[i-1]
13+
dp[i][d+D] = max(dp[i-1][d+D], (dp[i-1][d+D-h]+h) if d+D-h>=0 else float('-inf'), dp[i-1][d+D+h] if d+D+h<2*D+1 else float('-inf'))
14+
15+
return dp[N][D]

problems/target-sum.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,27 @@ def findTargetSumWays(self, nums, target):
3232
dp[i][j] = dp[i-1][j+nums[i-1]] + dp[i-1][j-nums[i-1]]
3333

3434
return dp[len(nums)][target]
35-
35+
36+
37+
#2021/6/7
38+
"""
39+
dp[i][t] := number of ways nums[:i] can sum up to t applying + or -.
40+
For all i, calculate all posible target (From minTarget~maxTarget)
41+
"""
42+
class Solution(object):
43+
def findTargetSumWays(self, nums, target):
44+
N = len(nums)
45+
maxTarget = sum(nums)
46+
minTarget = -maxTarget
47+
48+
if target<minTarget or target>maxTarget: return 0
49+
50+
dp = [[0 for _ in xrange(minTarget, maxTarget+1)] for _ in xrange(N+1)]
51+
52+
dp[0][0] = 1
53+
54+
for i in xrange(1, N+1):
55+
for t in xrange(minTarget, maxTarget+1):
56+
dp[i][t] = (dp[i-1][t-nums[i-1]] if t-nums[i-1]>=minTarget else 0) + (dp[i-1][t+nums[i-1]] if t+nums[i-1]<=maxTarget else 0)
57+
58+
return dp[N][target]

0 commit comments

Comments
 (0)