Skip to content

Commit 8b8d4ba

Browse files
authored
Add maximum subarray solution (rust-lang#209)
1 parent 6f20d01 commit 8b8d4ba

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ These are for demonstration purposes only.
3434
- [x] [Longest common subsequence](./src/dynamic_programming/longest_common_subsequence.rs)
3535
- [ ] Longest increasing subsequence
3636
- [x] [K-Means Clustering](./src/general/kmeans.rs)
37-
- [ ] Coin Change
37+
- [x] [Coin Change](./src/dynamic_programming/coin_change.rs)
3838
- [ ] Rod cut
3939
- [x] [Egg Dropping Puzzle](./src/dynamic_programming/egg_dropping.rs)
40+
- [x] [Maximum Subarray](./src/dynamic_programming/maximum_subarray.rs)
4041

4142
## [Data Structures](./src/data_structures)
4243

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// ## maximum subarray via Dynamic Programming
2+
3+
/// maximum_subarray(array) find the subarray (containing at least one number) which has the largest sum
4+
/// and return its sum.
5+
///
6+
/// A subarray is a contiguous part of an array.
7+
///
8+
/// Arguments:
9+
/// * `array` - an integer array
10+
/// Complexity
11+
/// - time complexity: O(array.length),
12+
/// - space complexity: O(array.length),
13+
pub fn maximum_subarray(array: &[i32]) -> i32 {
14+
let mut dp = vec![0; array.len()];
15+
dp[0] = array[0];
16+
let mut result = dp[0];
17+
18+
for i in 1..array.len() {
19+
if dp[i - 1] > 0 {
20+
dp[i] = dp[i - 1] + array[i];
21+
} else {
22+
dp[i] = array[i];
23+
}
24+
result = result.max(dp[i]);
25+
}
26+
27+
result
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
34+
#[test]
35+
fn non_negative() {
36+
//the maximum value: 1 + 0 + 5 + 8 = 14
37+
let array = vec![1, 0, 5, 8];
38+
assert_eq!(maximum_subarray(&array), 14);
39+
}
40+
41+
#[test]
42+
fn negative() {
43+
//the maximum value: -1
44+
let array = vec![-3, -1, -8, -2];
45+
assert_eq!(maximum_subarray(&array), -1);
46+
}
47+
48+
#[test]
49+
fn normal() {
50+
//the maximum value: 3 + (-2) + 5 = 6
51+
let array = vec![-4, 3, -2, 5, -8];
52+
assert_eq!(maximum_subarray(&array), 6);
53+
}
54+
55+
#[test]
56+
fn single_element() {
57+
let array = vec![6];
58+
assert_eq!(maximum_subarray(&array), 6);
59+
let array = vec![-6];
60+
assert_eq!(maximum_subarray(&array), -6);
61+
}
62+
}

src/dynamic_programming/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod egg_dropping;
44
mod fibonacci;
55
mod knapsack;
66
mod longest_common_subsequence;
7+
mod maximum_subarray;
78

89
pub use self::coin_change::coin_change;
910
pub use self::edit_distance::{edit_distance, edit_distance_se};
@@ -12,3 +13,4 @@ pub use self::fibonacci::fibonacci;
1213
pub use self::fibonacci::recursive_fibonacci;
1314
pub use self::knapsack::knapsack;
1415
pub use self::longest_common_subsequence::longest_common_subsequence;
16+
pub use self::maximum_subarray::maximum_subarray;

0 commit comments

Comments
 (0)