Skip to content

Commit f773508

Browse files
committed
leetcode
1 parent 5c5d965 commit f773508

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
type Solution struct {
9+
set []int
10+
n int
11+
memo map[int]int
12+
}
13+
14+
func minCost(n int, cuts []int) int {
15+
s := Solution{}
16+
s.set = make([]int, len(cuts))
17+
copy(s.set, cuts)
18+
s.n = n
19+
s.memo = make(map[int]int)
20+
21+
return s.helper(0, n)
22+
}
23+
24+
func (s *Solution) helper(i, j int) int {
25+
key := i*(s.n+1) + j
26+
if val, ok := s.memo[key]; ok {
27+
return val
28+
}
29+
30+
res := math.MaxInt32
31+
for _, c := range s.set {
32+
if c > i && c < j {
33+
res = min(res, (j-i)+s.helper(i, c)+s.helper(c, j))
34+
}
35+
}
36+
37+
if res == math.MaxInt32 {
38+
res = 0
39+
}
40+
s.memo[key] = res
41+
42+
return res
43+
}
44+
45+
func min(a, b int) int {
46+
if a < b {
47+
return a
48+
}
49+
return b
50+
}
51+
52+
func main() {
53+
n := 9
54+
cuts := []int{5, 6, 1, 4, 2}
55+
fmt.Println(minCost(n, cuts))
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 🔥 Minimum Cost to Cut a Stick 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Explanation
4+
5+
1. The function `minCost` takes an integer `n` (representing the length of the stick) and a list of integers `cuts` (representing the positions where the stick needs to be cut).
6+
2. The code begins by creating a new list `combinedCuts` and populating it with the elements from the `cuts` list, appending `0` at the beginning and `n` at the end. This list represents all the cut positions, including the start and end points of the stick.
7+
3. The `combinedCuts` list is then sorted in ascending order, ensuring that the cut positions are in increasing order.
8+
4. The variable `m` is assigned the length of the `combinedCuts` list.
9+
5. The `dp` list is initialized as a 2D list with dimensions `m` x `m`, where each element is initially set to `0`. This list will store the minimum cost for cutting the stick between different positions.
10+
6. Next, a nested loop is used to fill the `dp` table. The outer loop iterates over the lengths of the cut ranges (starting from 2), and the inner loop iterates over the start positions of the cut ranges.
11+
7. Inside the nested loops, the variable `j` is calculated as `i + length`, representing the end position of the cut range.
12+
8. The `dp[i][j]` value is initialized to infinity (`double.infinity.toInt()`), indicating that it hasn't been computed yet.
13+
9. Another loop iterates over the possible cut positions within the range (`k` ranges from `i + 1` to `j - 1`).
14+
10. For each `k`, the code compares the current `dp[i][j]` value with the sum of `dp[i][k]`, `dp[k][j]`, and the cost of cutting the stick between positions `combinedCuts[j]` and `combinedCuts[i]`.
15+
11. If the new sum is smaller than the current `dp[i][j]` value, it updates `dp[i][j]` with the new sum.
16+
12. Once all the iterations are completed, the minimum cost to cut the entire stick is stored in `dp[0][m - 1]`.
17+
13. The function returns the minimum cost.
18+
19+
## Time Complexity
20+
21+
The time complexity of this code is O(n^3), where n is the length of the `cuts` list. This is because there are three nested loops: one for the lengths of the cut ranges, one for the start positions, and one for the possible cut positions within each range. Since each loop can iterate up to `n` times, the overall time complexity is O(n^3).
22+
23+
## Solution - 1
24+
25+
```dart
26+
class Solution {
27+
int minCost(int n, List<int> cuts) {
28+
cuts
29+
..add(0)
30+
..add(n);
31+
int m = cuts.length;
32+
cuts.sort();
33+
List<List<int>> dp = List.generate(m, (_) => List<int>.filled(m, -1));
34+
35+
/*
36+
I've defined maxIntValue as 1 << 30,
37+
which represents a large integer value that can act as a substitute for infinity in this context.
38+
*/
39+
final int maxIntValue = 1 << 30; // Define a large constant value
40+
41+
int cost(int i, int j) {
42+
if (j - i <= 1) return 0;
43+
if (dp[i][j] != -1) return dp[i][j];
44+
int result = maxIntValue;
45+
for (int k = i + 1; k < j; k++) {
46+
result =
47+
result < cost(i, k) + cost(k, j) ? result : cost(i, k) + cost(k, j);
48+
}
49+
return dp[i][j] = result + (cuts[j] - cuts[i]);
50+
}
51+
52+
return cost(0, m - 1);
53+
}
54+
}
55+
```
56+
57+
## Solution - 2
58+
59+
```dart
60+
class Solution {
61+
int minCost(int n, List<int> cuts) {
62+
List<int> combinedCuts = List<int>.from(cuts);
63+
combinedCuts
64+
..add(0)
65+
..add(n);
66+
combinedCuts.sort();
67+
int m = combinedCuts.length;
68+
69+
List<List<int>> dp = List.generate(m, (_) => List<int>.filled(m, 0));
70+
71+
for (int length = 2; length < m; length++) {
72+
for (int i = 0; i < m - length; i++) {
73+
int j = i + length;
74+
dp[i][j] = 1 << 30;
75+
for (int k = i + 1; k < j; k++) {
76+
dp[i][j] =
77+
dp[i][j] < dp[i][k] + dp[k][j] + combinedCuts[j] - combinedCuts[i]
78+
? dp[i][j]
79+
: dp[i][k] + dp[k][j] + combinedCuts[j] - combinedCuts[i];
80+
}
81+
}
82+
}
83+
84+
return dp[0][m - 1];
85+
}
86+
}
87+
```

New21Game/new_21_game.dart

+4
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,7 @@ class Solution {
112112

113113

114114

115+
class A {
116+
var a =9;
117+
118+
}

0 commit comments

Comments
 (0)