-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaximum-sum-circular-subarray.rs
59 lines (57 loc) · 2.05 KB
/
maximum-sum-circular-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
58
59
// 918. Maximum Sum Circular Subarray
// 🟠 Medium
//
// https://leetcode.com/problems/maximum-sum-circular-subarray/
//
// Tags: Array - Divide and Conquer - Dynamic Programming - Queue - Monotonic Queue
struct Solution;
impl Solution {
// The maximum sum if we can go over the end and use values at the
// beginning of the array will be either the classical maximum that we
// can obtain using Kadane's algorithm, if the maximum subarray does not
// overlap the going around point, or the sum of all values in the array
// minus the minimum subarray in the original input. We can compute both,
// and the sum of values in the array, using a single pass and Kadane's
// algorithm.
//
// Time complexity: O(n) - We visit each value in the input once.
// Space complexity: O(1) - We use constant extra memory.
//
// Runtime 3 ms Beats 100%
// Memory 2.5 MB Beats 63.64%
pub fn max_subarray_sum_circular(nums: Vec<i32>) -> i32 {
let mut max_sum = nums[0];
let mut min_sum = nums[0];
let mut current_max = nums[0];
let mut current_min = nums[0];
let mut total = nums[0];
for num in nums.iter().skip(1) {
current_max = if current_max < 0 {
*num
} else {
current_max + num
};
max_sum = max_sum.max(current_max);
current_min = if current_min > 0 {
*num
} else {
current_min + num
};
min_sum = min_sum.min(current_min);
total += *num;
}
if min_sum == total {
max_sum
} else {
max_sum.max(total - min_sum)
}
}
}
// Tests.
fn main() {
assert_eq!(Solution::max_subarray_sum_circular(vec![5, -3, 5]), 10);
assert_eq!(Solution::max_subarray_sum_circular(vec![-3, -2, -3]), -2);
assert_eq!(Solution::max_subarray_sum_circular(vec![1, -2, 3, -2]), 3);
assert_eq!(Solution::max_subarray_sum_circular(vec![5, -3, -7, 5]), 10);
println!("All tests passed!")
}