problem
給定一陣列,求出總和最大連續子陣列,並返回其值
Solution
option 1 - Divide and Conquer
1 | class Solution { |
option 2 - dp
維護一個dp 紀錄遍歷至當前的最大的子陣列和。
1 | class Solution { |
option 3 - Kadane’s Algorithm
reduce dp,用變數取代dp array
1 | class Solution { |
analysis
- option 1
- time complexity
O(nlogn)
- space complexity
O(1)
- time complexity
- option 2
- time complexity
O(n)
- speed complexity
O(n)
- time complexity
- option 3
- time complexity
O(n)
- speed complexity
O(1)
- time complexity