implemented kadanes
This commit is contained in:
parent
9cbbb916ef
commit
9f4985f127
1 changed files with 23 additions and 0 deletions
23
src/kadanes.rs
Normal file
23
src/kadanes.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use std::cmp;
|
||||
|
||||
pub fn kadanes(arr: &[i32]) -> (i32, Vec<i32>) {
|
||||
let mut max_so_far = i32::MIN;
|
||||
let mut max_ending_here = 0;
|
||||
let mut start_index = 0;
|
||||
let mut end_index = 0;
|
||||
let mut temp_start_index = 0;
|
||||
|
||||
for (i, &num) in arr.iter().enumerate() {
|
||||
max_ending_here = cmp::max(max_ending_here + num, num);
|
||||
if max_ending_here == num {
|
||||
temp_start_index = i;
|
||||
}
|
||||
if max_so_far < max_ending_here {
|
||||
max_so_far = max_ending_here;
|
||||
start_index = temp_start_index;
|
||||
end_index = i;
|
||||
}
|
||||
}
|
||||
|
||||
(max_so_far, arr[start_index..=end_index].to_vec())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue