|
| 1 | +/** |
| 2 | + * [1547] Minimum Cost to Cut a Stick |
| 3 | + * |
| 4 | + * Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows: |
| 5 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/21/statement.jpg" style="width: 521px; height: 111px;" /> |
| 6 | + * Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. |
| 7 | + * You should perform the cuts in order, you can change the order of the cuts as you wish. |
| 8 | + * The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation. |
| 9 | + * Return the minimum total cost of the cuts. |
| 10 | + * |
| 11 | + * Example 1: |
| 12 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/23/e1.jpg" style="width: 350px; height: 284px;" /> |
| 13 | + * Input: n = 7, cuts = [1,3,4,5] |
| 14 | + * Output: 16 |
| 15 | + * Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: |
| 16 | + * <img alt="" src="https://assets.leetcode.com/uploads/2020/07/21/e11.jpg" style="width: 350px; height: 284px;" /> |
| 17 | + * The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. |
| 18 | + * Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). |
| 19 | + * Example 2: |
| 20 | + * |
| 21 | + * Input: n = 9, cuts = [5,6,1,4,2] |
| 22 | + * Output: 22 |
| 23 | + * Explanation: If you try the given cuts ordering the cost will be 25. |
| 24 | + * There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible. |
| 25 | + * |
| 26 | + * |
| 27 | + * Constraints: |
| 28 | + * |
| 29 | + * 2 <= n <= 10^6 |
| 30 | + * 1 <= cuts.length <= min(n - 1, 100) |
| 31 | + * 1 <= cuts[i] <= n - 1 |
| 32 | + * All the integers in cuts array are distinct. |
| 33 | + * |
| 34 | + */ |
| 35 | +pub struct Solution {} |
| 36 | + |
| 37 | +// problem: https://leetcode.com/problems/minimum-cost-to-cut-a-stick/ |
| 38 | +// discuss: https://leetcode.com/problems/minimum-cost-to-cut-a-stick/discuss/?currentPage=1&orderBy=most_votes&query= |
| 39 | + |
| 40 | +// submission codes start here |
| 41 | + |
| 42 | +impl Solution { |
| 43 | + // Credit: https://leetcode.com/problems/minimum-cost-to-cut-a-stick/solutions/3167350/just-a-runnable-solution/ |
| 44 | + pub fn min_cost(n: i32, cuts: Vec<i32>) -> i32 { |
| 45 | + let n = n as usize; |
| 46 | + let mut cuts = cuts; |
| 47 | + |
| 48 | + cuts.push(0); |
| 49 | + cuts.push(n as i32); |
| 50 | + cuts.sort_unstable(); |
| 51 | + |
| 52 | + let m = cuts.len(); |
| 53 | + let mut dp = vec![vec![0; m]; m]; |
| 54 | + |
| 55 | + for (i, item) in dp.iter_mut().enumerate().take(m) { |
| 56 | + item[i] = 0; |
| 57 | + } |
| 58 | + |
| 59 | + for (i, item) in dp.iter_mut().enumerate().take(m - 1) { |
| 60 | + item[i + 1] = 0; |
| 61 | + } |
| 62 | + |
| 63 | + for len in 2..m { |
| 64 | + for i in 0..m - len { |
| 65 | + let j = i + len; |
| 66 | + let mut min = std::i32::MAX; |
| 67 | + |
| 68 | + for k in i + 1..j { |
| 69 | + let cost = dp[i][k] + dp[k][j] + cuts[j] - cuts[i]; |
| 70 | + if cost < min { |
| 71 | + min = cost; |
| 72 | + } |
| 73 | + } |
| 74 | + dp[i][j] = min; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + dp[0][m - 1] |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// submission codes end |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_1547_example_1() { |
| 90 | + let n = 7; |
| 91 | + let cuts = vec![1, 3, 4, 5]; |
| 92 | + |
| 93 | + let result = 16; |
| 94 | + |
| 95 | + assert_eq!(Solution::min_cost(n, cuts), result); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_1547_example_2() { |
| 100 | + let n = 9; |
| 101 | + let cuts = vec![5, 6, 1, 4, 2]; |
| 102 | + |
| 103 | + let result = 22; |
| 104 | + |
| 105 | + assert_eq!(Solution::min_cost(n, cuts), result); |
| 106 | + } |
| 107 | +} |
0 commit comments