Skip to content

Commit 83139c1

Browse files
committed
add combination sum, maximum subarray solution
1 parent eea1160 commit 83139c1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

combination-sum/i-mprovising.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Time complexity O(c*t)
3+
Space complexity O(c*t)
4+
5+
Dynamic programming
6+
"""
7+
8+
class Solution:
9+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
10+
# init dp array
11+
dp = [[] for _ in range(target+1)] # dp[i] : combinations to sum to i
12+
dp[0] = [[]]
13+
14+
for candidate in candidates:
15+
for num in range(candidate, target+1):
16+
for comb in dp[num-candidate]:
17+
dp[num].append(comb + [candidate])
18+
19+
return dp[-1]

maximum-subarray/i-mprovising.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Time complexity O(n)
3+
Space complexity O(n)
4+
5+
Dynamic programming
6+
"""
7+
8+
9+
class Solution:
10+
def maxSubArray(self, nums: List[int]) -> int:
11+
n = len(nums)
12+
dp = [0 for _ in range(n)]
13+
dp[0] = nums[0]
14+
15+
for i in range(1, n):
16+
dp[i] = max(dp[i-1]+nums[i], nums[i])
17+
18+
return max(dp)

0 commit comments

Comments
 (0)