-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
55 lines (49 loc) · 1.26 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn main() {
println!("Hello, world!");
}
struct Solution {}
impl Solution {
/// divide and conquer
/// dp[i] means the maxsubarray[0..i]
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
let mut max_sum = nums[0];
let mut max_sum_before = 0;
for d in nums {
max_sum_before = if max_sum_before > 0 {
max_sum_before + d
} else {
d
};
if max_sum_before > max_sum {
max_sum = max_sum_before;
}
}
max_sum
}
/// linear O(n) search
pub fn max_sub_array_linear(nums: Vec<i32>) -> i32 {
let mut max_sum = nums[0];
let mut sum = 0;
for d in nums {
sum += d;
if sum > max_sum {
max_sum = sum;
}
if sum < 0 {
sum = 0;
}
}
max_sum
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::max_sub_array(vec![-2,1,-3,4,-1,2,1,-5,4]), 6);
assert_eq!(Solution::max_sub_array(vec![-2,1,5,4,-8,2,1,-5,4]), 10);
assert_eq!(Solution::max_sub_array(vec![-1]), -1);
assert_eq!(Solution::max_sub_array(vec![-2, -1]), -1);
}
}