-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_subarray.rs
57 lines (45 loc) · 1.13 KB
/
maximum_subarray.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
56
57
/// Given an integer array `nums`, find the subarray with the largest sum, and
/// return its sum.
struct Solution;
impl Solution {
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut result = i32::MIN;
let mut sum = 0;
for i in 0..n {
sum = sum.max(0);
let num = nums[i];
sum += num;
result = result.max(sum);
}
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![-2,1,-3,4,-1,2,1,-5,4];
let result = Solution::max_sub_array(nums);
assert_eq!(result, 6);
}
#[test]
fn example_2() {
let nums = vec![1];
let result = Solution::max_sub_array(nums);
assert_eq!(result, 1);
}
#[test]
fn example_3() {
let nums = vec![5,4,-1,7,8];
let result = Solution::max_sub_array(nums);
assert_eq!(result, 23);
}
#[test]
fn all_negative() {
let nums = vec![-7,-3,-5,-2,-8];
let result = Solution::max_sub_array(nums);
assert_eq!(result, -2);
}
}