|
| 1 | +/* |
| 2 | +
|
| 3 | +-* Minimum Cost to Cut a Stick *- |
| 4 | +
|
| 5 | +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: |
| 6 | +
|
| 7 | +
|
| 8 | +Given an integer array cuts where cuts[i] denotes a position you should perform a cut at. |
| 9 | +
|
| 10 | +You should perform the cuts in order, you can change the order of the cuts as you wish. |
| 11 | +
|
| 12 | +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. |
| 13 | +
|
| 14 | +Return the minimum total cost of the cuts. |
| 15 | +
|
| 16 | + |
| 17 | +
|
| 18 | +Example 1: |
| 19 | +
|
| 20 | +
|
| 21 | +Input: n = 7, cuts = [1,3,4,5] |
| 22 | +Output: 16 |
| 23 | +Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: |
| 24 | +
|
| 25 | +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. |
| 26 | +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). |
| 27 | +Example 2: |
| 28 | +
|
| 29 | +Input: n = 9, cuts = [5,6,1,4,2] |
| 30 | +Output: 22 |
| 31 | +Explanation: If you try the given cuts ordering the cost will be 25. |
| 32 | +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. |
| 33 | + |
| 34 | +
|
| 35 | +Constraints: |
| 36 | +
|
| 37 | +2 <= n <= 106 |
| 38 | +1 <= cuts.length <= min(n - 1, 100) |
| 39 | +1 <= cuts[i] <= n - 1 |
| 40 | +All the integers in cuts array are distinct. |
| 41 | +
|
| 42 | +*/ |
| 43 | + |
| 44 | +import 'dart:collection'; |
| 45 | + |
| 46 | +class A { |
| 47 | + int minCost(int n, List<int> cuts) { |
| 48 | + cuts |
| 49 | + ..add(0) |
| 50 | + ..add(n); |
| 51 | + int m = cuts.length; |
| 52 | + cuts.sort(); |
| 53 | + List<List<int>> dp = List.generate(m, (_) => List<int>.filled(m, -1)); |
| 54 | + |
| 55 | +/* |
| 56 | + I've defined maxIntValue as 1 << 30, |
| 57 | + which represents a large integer value that can act as a substitute for infinity in this context. |
| 58 | +*/ |
| 59 | + final int maxIntValue = 1 << 30; // Define a large constant value |
| 60 | + |
| 61 | + int cost(int i, int j) { |
| 62 | + if (j - i <= 1) return 0; |
| 63 | + if (dp[i][j] != -1) return dp[i][j]; |
| 64 | + int result = maxIntValue; |
| 65 | + for (int k = i + 1; k < j; k++) { |
| 66 | + result = |
| 67 | + result < cost(i, k) + cost(k, j) ? result : cost(i, k) + cost(k, j); |
| 68 | + } |
| 69 | + return dp[i][j] = result + (cuts[j] - cuts[i]); |
| 70 | + } |
| 71 | + |
| 72 | + return cost(0, m - 1); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class B { |
| 77 | + int minCost(int n, List<int> cuts) { |
| 78 | + List<int> combinedCuts = List<int>.from(cuts); |
| 79 | + combinedCuts |
| 80 | + ..add(0) |
| 81 | + ..add(n); |
| 82 | + combinedCuts.sort(); |
| 83 | + int m = combinedCuts.length; |
| 84 | + |
| 85 | + List<List<int>> dp = List.generate(m, (_) => List<int>.filled(m, 0)); |
| 86 | + |
| 87 | + for (int length = 2; length < m; length++) { |
| 88 | + for (int i = 0; i < m - length; i++) { |
| 89 | + int j = i + length; |
| 90 | + dp[i][j] = 1 << 30; |
| 91 | + for (int k = i + 1; k < j; k++) { |
| 92 | + dp[i][j] = |
| 93 | + dp[i][j] < dp[i][k] + dp[k][j] + combinedCuts[j] - combinedCuts[i] |
| 94 | + ? dp[i][j] |
| 95 | + : dp[i][k] + dp[k][j] + combinedCuts[j] - combinedCuts[i]; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return dp[0][m - 1]; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// import 'dart:collection'; |
| 105 | + |
| 106 | +class Solution { |
| 107 | + SplayTreeSet<int> set = SplayTreeSet<int>(); |
| 108 | + late int n; |
| 109 | + |
| 110 | + Map<int, int> memo = {}; |
| 111 | + |
| 112 | + int minCost(int n, List<int> cuts) { |
| 113 | + for (int c in cuts) set.add(c); |
| 114 | + this.n = n; |
| 115 | + |
| 116 | + return helper(0, n); |
| 117 | + } |
| 118 | + |
| 119 | + int helper(int i, int j) { |
| 120 | + int key = i * (n + 1) + j; |
| 121 | + if (memo.containsKey(key)) return memo[key]!; |
| 122 | + |
| 123 | + int res = double.infinity.toInt(); |
| 124 | + int index = set.toList().indexWhere((value) => value > i); |
| 125 | + for (int k = index; k < set.length; k++) { |
| 126 | + int value = set.elementAt(k); |
| 127 | + res = res < (j - i) + helper(i, value) + helper(value, j) |
| 128 | + ? res |
| 129 | + : (j - i) + helper(i, value) + helper(value, j); |
| 130 | + } |
| 131 | + |
| 132 | + if (res == double.infinity.toInt()) res = 0; |
| 133 | + memo[key] = res; |
| 134 | + |
| 135 | + return res; |
| 136 | + } |
| 137 | +} |
0 commit comments