```python
[Read More]
724. Find Pivot Index
main idea: given total sum, use total-psum[left] - pivot == psum[left] class Solution: def pivotIndex(self, nums: List[int]) -> int: # time O(N) space (1) # step1: get total sum total = sum(nums) psum = 0 # step2: iterate through nums, get psum for i,num in enumerate(nums): # step3: check if...
[Read More]
53. Maximum Subarray
DP way (space O(N)): key point here is keep recording local max subarray sum while updating global max subarray sum class Solution: def maxSubArray(self, A: List[int]) -> int: """ tc O(N) sc O(N) dp[i]: max sub array sum ending with A[i] use a global res to cache local subarary sum...
[Read More]
518. Coin Change 2
```python class Solution: def change(self, amount: int, coins: List[int]) -> int: “"”tc O(KN) sc O(KN) main idea: Top Down dp[i][j]: ways of COMBINATION to fill j amount with first time using coins[i-1] “”” n = len(coins) dp = [[0 for x in range(amount+1)] for _ in range(n+1)] # base case...
[Read More]
121. Best Time to Buy and Sell Stock
main idea: each price can be choosen as buy point, only price later than buy point can be use as sell point. Since there is only one transaction allowed, we need to mantain 2 variables: minPrice and maxProfit ```python time O(N) space(1) class Solution: def maxProfit(self, prices: List[int]) -> int:...
[Read More]