forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyhkee0404.rs
More file actions
35 lines (35 loc) · 887 Bytes
/
yhkee0404.rs
File metadata and controls
35 lines (35 loc) · 887 Bytes
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
impl Solution {
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
return Self::solve(&nums, 0, nums.len()).unwrap_or(0);
}
fn solve(nums: &Vec<i32>, l: usize, r: usize) -> Option<i32> {
if l >= r {
return None
}
if l + 1 == r {
return Some(nums[l])
}
let mid = l + ((r - l) >> 1);
let a = Self::solve(nums, l, mid);
let b = Self::solve(nums, mid, r);
if a.is_none() || b.is_none() {
return a.or(b)
}
let mut ans = a.max(b);
let mut c = 0;
let mut d = 0;
for i in (l..mid).rev() {
c += nums[i];
d = d.max(c);
}
if d == 0 {
return ans
}
c = d;
for i in mid..r {
c += nums[i];
d = d.max(c);
}
ans.max(Some(d))
}
}