File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 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 ]
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments