Skip to content

Commit c9cc4b3

Browse files
biabebxelahigithub-actions
authored
feat: add Rod Cutting (rust-lang#190)
* Add Rod Cutting * updating DIRECTORY.md Co-authored-by: xelahi <xelahi@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 632626b commit c9cc4b3

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* [Longest Common Subsequence](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/longest_common_subsequence.rs)
2929
* [Longest Continuous Increasing Subsequence](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/longest_continuous_increasing_subsequence.rs)
3030
* [Maximum Subarray](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/maximum_subarray.rs)
31+
* [Rod Cutting](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/rod_cutting.rs)
3132
* General
3233
* [Convex Hull](https://github.com/TheAlgorithms/Rust/blob/master/src/general/convex_hull.rs)
3334
* [Hanoi](https://github.com/TheAlgorithms/Rust/blob/master/src/general/hanoi.rs)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ RESTART BUILD
4343
- [ ] Longest increasing subsequence
4444
- [x] [K-Means Clustering](./src/general/kmeans.rs)
4545
- [x] [Coin Change](./src/dynamic_programming/coin_change.rs)
46-
- [ ] Rod cut
46+
- [x] [Rod Cutting](./src/dynamic_programming/rod_cutting.rs)
4747
- [x] [Egg Dropping Puzzle](./src/dynamic_programming/egg_dropping.rs)
4848
- [x] [Maximum Subarray](./src/dynamic_programming/maximum_subarray.rs)
4949

src/dynamic_programming/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod knapsack;
66
mod longest_common_subsequence;
77
mod longest_continuous_increasing_subsequence;
88
mod maximum_subarray;
9+
mod rod_cutting;
910

1011
pub use self::coin_change::coin_change;
1112
pub use self::edit_distance::{edit_distance, edit_distance_se};
@@ -16,3 +17,4 @@ pub use self::knapsack::knapsack;
1617
pub use self::longest_common_subsequence::longest_common_subsequence;
1718
pub use self::longest_continuous_increasing_subsequence::longest_continuous_increasing_subsequence;
1819
pub use self::maximum_subarray::maximum_subarray;
20+
pub use self::rod_cutting::rod_cut;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Solves the rod-cutting problem
2+
use std::cmp::max;
3+
4+
/// `rod_cut(p)` returns the maximum possible profit if a rod of length `n` = `p.len()`
5+
/// is cut into up to `n` pieces, where the profit gained from each piece of length
6+
/// `l` is determined by `p[l - 1]` and the total profit is the sum of the profit
7+
/// gained from each piece.
8+
///
9+
/// # Arguments
10+
/// - `p` - profit for rods of length 1 to n inclusive
11+
///
12+
/// # Complexity
13+
/// - time complexity: O(n^2),
14+
/// - space complexity: O(n^2),
15+
///
16+
/// where n is the length of `p`.
17+
pub fn rod_cut(p: &[usize]) -> usize {
18+
let n = p.len();
19+
// f is the dynamic programming table
20+
let mut f = vec![0; n];
21+
22+
for i in 0..n {
23+
let mut max_price = p[i];
24+
for j in 1..=i {
25+
max_price = max(max_price, p[j - 1] + f[i - j]);
26+
}
27+
f[i] = max_price;
28+
}
29+
30+
// accomodate for input with length zero
31+
if n != 0 {
32+
f[n - 1]
33+
} else {
34+
0
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::rod_cut;
41+
42+
#[test]
43+
fn test_rod_cut() {
44+
assert_eq!(0, rod_cut(&[]));
45+
assert_eq!(15, rod_cut(&[5, 8, 2]));
46+
assert_eq!(10, rod_cut(&[1, 5, 8, 9]));
47+
assert_eq!(25, rod_cut(&[5, 8, 2, 1, 7]));
48+
assert_eq!(87, rod_cut(&[0, 0, 0, 0, 0, 87]));
49+
assert_eq!(49, rod_cut(&[7, 6, 5, 4, 3, 2, 1]));
50+
assert_eq!(22, rod_cut(&[1, 5, 8, 9, 10, 17, 17, 20]));
51+
assert_eq!(60, rod_cut(&[6, 4, 8, 2, 5, 8, 2, 3, 7, 11]));
52+
assert_eq!(30, rod_cut(&[1, 5, 8, 9, 10, 17, 17, 20, 24, 30]));
53+
assert_eq!(12, rod_cut(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
54+
}
55+
}

0 commit comments

Comments
 (0)