Crates.io | max-subarray-sum |
lib.rs | max-subarray-sum |
version | 0.1.7 |
source | src |
created_at | 2021-07-24 14:17:50.22854 |
updated_at | 2021-07-31 13:29:15.854987 |
description | Finds the maximum subarray sum in a list. |
homepage | |
repository | https://github.com/Inoshy/rust-book-helper/tree/master/problems/max-subarray-sum |
max_upload_size | |
id | 426785 |
size | 5,927 |
Finds maximum subarray sum in a list. If there are multiple such subarrays, only the one that comes first is selected.
The algorithm has time complexity of O(N)
and space complexity
of O(1)
.
version note: Some modularity problems in previous versions have been patched.
use max_subarray_sum::interface::Elements;
fn main() {
let list = vec![-2, -3, 4, -1, -2, 1, 5, -3];
//Or you can use an array instead:
let list = [-2, -3, 4, -1, -2, 1, 5, -3];
let elements = Elements::new(&mut list);
let max_sum = elements.find_max_sum().result();
assert_eq!(7, max_sum);
}