Skip to content

Commit a0aaa90

Browse files
author
Chris Wu
committed
no message
1 parent 464fe6b commit a0aaa90

5 files changed

+124
-0
lines changed

problems/combination-sum-iv.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def combinationSum4(self, nums, target):
3+
def helper(t):
4+
if t<0: return 0
5+
if dp[t]>=0: return dp[t]
6+
7+
ans = 0
8+
for num in nums:
9+
ans += helper(t-num)
10+
11+
dp[t] = ans
12+
return ans
13+
14+
dp = [-1]*(target+1)
15+
dp[0] = 1
16+
17+
for i in xrange(target+1):
18+
helper(i)
19+
return dp[target]
20+
21+
"""
22+
Time: O(TN), T is the value of target and N is the count of nums.
23+
Space: O(T)
24+
"""

problems/last-stone-weight-ii.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
dp[i][t] = considering stones[0~i-1], if it can sum up to target t
3+
4+
Time: O(SN), S is the sum of stone weight. N is the number of stones.
5+
Space: O(SN), can reduce to O(S).
6+
"""
7+
class Solution(object):
8+
def lastStoneWeightII(self, stones):
9+
total = sum(stones)
10+
target = total/2
11+
dp = [[False for _ in xrange(target+1)] for _ in xrange(len(stones)+1)]
12+
dp[0][0] = True
13+
14+
maxSum = 0
15+
# Keep trace of the max sum that stones can sum up to.
16+
17+
for i in xrange(1, len(stones)+1):
18+
for t in xrange(target+1):
19+
if (dp[i-1][t] or (t-stones[i-1]>=0 and dp[i-1][t-stones[i-1]])):
20+
# it can sum up to t considering stones[0~i-2]
21+
# OR
22+
# it can sum up to t considering stones[0~i-1]
23+
dp[i][t] = True
24+
maxSum = max(maxSum, t)
25+
if t==target: return total-maxSum*2
26+
27+
# Two collection of stones will be total-maxSum and maxSum
28+
# (total-maxSum) - maxSum => total-maxSum*2
29+
return total-maxSum*2

problems/last-stone-weight.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import bisect
2+
class Solution(object):
3+
def lastStoneWeight(self, stones):
4+
stones.sort()
5+
6+
while len(stones)>1:
7+
x = stones.pop()
8+
y = stones.pop()
9+
bisect.insort_left(stones, abs(x-y))
10+
11+
return 0 if not stones else stones[0]
12+
13+
import heapq
14+
class Solution(object):
15+
def lastStoneWeight(self, stones):
16+
h = []
17+
18+
for stone in stones:
19+
heapq.heappush(h, stone*-1)
20+
21+
while len(h)>1:
22+
x = heapq.heappop(h)
23+
y = heapq.heappop(h)
24+
heapq.heappush(h, abs(x-y)*-1)
25+
26+
return 0 if not h else abs(h[0])
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
dp[i] = maxSumAfterPartitioning(A[0~i-1], K) and must contain A[i-1]
3+
"""
4+
class Solution(object):
5+
def maxSumAfterPartitioning(self, A, K):
6+
dp = [0 for _ in xrange(len(A)+1)]
7+
8+
for i in xrange(1, len(A)+1):
9+
m = float('-inf') # max in A[i-k]~A[i-1]
10+
for k in xrange(1, min(i+1, K+1)):
11+
m = max(m, A[i-k])
12+
dp[i] = max(dp[i], m*k + dp[i-k])
13+
14+
return dp[-1]

problems/target-sum.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def findTargetSumWays(self, nums, S):
3+
stack = [(0, 0)]
4+
ans = 0
5+
6+
while stack:
7+
i, s = stack.pop()
8+
if i==len(nums) and s==S: ans += 1
9+
if i>=len(nums): continue
10+
11+
stack.append((i+1, s+nums[i]))
12+
stack.append((i+1, s-nums[i]))
13+
14+
return ans
15+
16+
import collections
17+
18+
19+
20+
class Solution(object):
21+
def findTargetSumWays(self, nums, target):
22+
S = sum(nums)
23+
dp = [collections.Counter() for _ in xrange(len(nums)+1)]
24+
dp[0][0] = 1
25+
26+
for i in xrange(1, len(nums)+1):
27+
for j in xrange(-S, S+1):
28+
dp[i][j] = dp[i-1][j+nums[i-1]] + dp[i-1][j-nums[i-1]]
29+
30+
return dp[len(nums)][target]
31+

0 commit comments

Comments
 (0)