divide and conquer dp[i] means the maxsubarray[0..i]
(nums: Vec<i32>)
| 8 | /// divide and conquer |
| 9 | /// dp[i] means the maxsubarray[0..i] |
| 10 | pub fn max_sub_array(nums: Vec<i32>) -> i32 { |
| 11 | let mut max_sum = nums[0]; |
| 12 | let mut max_sum_before = 0; |
| 13 | for d in nums { |
| 14 | max_sum_before = if max_sum_before > 0 { |
| 15 | max_sum_before + d |
| 16 | } else { |
| 17 | d |
| 18 | }; |
| 19 | if max_sum_before > max_sum { |
| 20 | max_sum = max_sum_before; |
| 21 | } |
| 22 | } |
| 23 | max_sum |
| 24 | } |
| 25 | |
| 26 | /// linear O(n) search |
| 27 | pub fn max_sub_array_linear(nums: Vec<i32>) -> i32 { |
nothing calls this directly
no outgoing calls
no test coverage detected