-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_subarray.rs
55 lines (46 loc) · 1.23 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
#![allow(dead_code)]
/*
#53 Maximum Subarray
Given an integer array nums, find the subarray
with the largest sum, and return its sum.
Constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
*/
/*
Kadane's Algo
T: O(N)
S: O(1)
*/
pub fn solution_a(nums: Vec<i32>) -> i32 {
let mut max_sub = i32::MIN;
let mut curr_sub = i32::MIN;
for n in nums.iter() {
let curr_sub_plus_n = curr_sub.saturating_add(*n);
// if curr_sub is negative, curr_sub_plus_n will always be < n
// therefore max of n and curr_sub_plus_n is equivalent to "reset curr_sub if negative"
curr_sub = (*n).max(curr_sub_plus_n);
max_sub = max_sub.max(curr_sub);
}
max_sub
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_0() {
let input = vec![-2, 1, -3, 4, -1, 2, 1, -5, 4];
assert_eq!(solution_a(input), 6);
}
#[test]
fn a_1() {
let input = vec![1];
assert_eq!(solution_a(input), 1);
}
#[test]
fn a_2() {
let input = vec![5, 4, -1, 7, 8];
assert_eq!(solution_a(input), 23);
}
}