MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / max_sub_array

Method max_sub_array

53. Maximum Subarray/src/main.rs:10–24  ·  view source on GitHub ↗

divide and conquer dp[i] means the maxsubarray[0..i]

(nums: Vec<i32>)

Source from the content-addressed store, hash-verified

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 {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected