Skip to content

Commit 3ea69cd

Browse files
authored
Merge pull request #3620 from sreevidya-16/main
Add Solution to LeetCode Problem 1547 (Hard)
2 parents 40b26d0 + 7da12cf commit 3ea69cd

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
id: minimum-cost-to-cut-a-stick
3+
title: Minimum Cost to Cut a Stick
4+
sidebar_label: 1547 - Minimum Cost to Cut a Stick
5+
tags: [Dynamic Programming, Array, C++]
6+
description: Solve the problem of finding the minimum cost to cut a stick into smaller pieces at specified positions, using dynamic programming.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
Given a wooden stick of length `n` units. The stick is labeled from 0 to `n`. For example, a stick of length 6 is labeled as follows:
14+
```
15+
0 - 1 - 2 - 3 - 4 - 5 - 6
16+
```
17+
18+
- Given an integer array `cuts` where `cuts[i]` denotes a position you should perform a cut at.
19+
20+
- You should perform the cuts in order, but you can change the order of the cuts as you wish.
21+
22+
- 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).
23+
24+
- Return the minimum total cost of the cuts.
25+
26+
### Example
27+
28+
**Example 1:**
29+
```
30+
Input:
31+
n = 7, cuts = [1, 3, 4, 5]
32+
33+
Output:
34+
16
35+
```
36+
37+
**Explanation:**
38+
39+
- Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
40+
41+
- The first cut is done to a rod of length 7 so the cost is 7.
42+
43+
- 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.
44+
45+
- The total cost is 7 + 6 + 4 + 3 = 20.
46+
47+
- 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).
48+
49+
50+
### Constraints
51+
52+
- 2 <= `n` <= 10^6
53+
- 1 <= `cuts.length` <= min(`n` - 1, 100)
54+
- 1 <= `cuts[i]` <= `n` - 1
55+
- All the integers in the `cuts` array are distinct.
56+
57+
## Solution
58+
59+
### Intuition
60+
61+
- Sort the cuts in ascending order.
62+
- Use dynamic programming to minimize the cost of cuts.
63+
- Define `dp[i][j]` as the minimum cost to cut the stick between cuts[i-1] and cuts[j-1].
64+
- Use a bottom-up approach to solve the subproblems and combine them to get the final result.
65+
66+
### Time Complexity and Space Complexity Analysis
67+
68+
- **Initialization**:
69+
- Sorting the cuts array takes $O(m log m)$ time, where m is the number of cuts.
70+
- Initializing the `dp` array takes $O(m^3)$ time.
71+
- Overall initialization time complexity is $O(m log m + m^2)$.
72+
73+
- **DP Table Calculation**:
74+
- Filling the `dp` table involves iterating over all possible subintervals and calculating the minimum cost for each subinterval using nested loops.
75+
- This takes $O(m^3)$ time in the worst case.
76+
77+
- **Overall Time Complexity**:
78+
- The overall time complexity is $O(m^3)$.
79+
80+
- **Space Complexity**:
81+
- The `dp` table requires $O(m^2)$ space.
82+
- The space complexity is $O(m^2)$.
83+
84+
### Code
85+
86+
#### C++
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int minCost(int n, std::vector<int>& cuts) {
92+
std::sort(cuts.begin(), cuts.end());
93+
int m = cuts.size();
94+
std::vector<std::vector<int>> dp(m + 2, std::vector<int>(m + 2, 0));
95+
96+
for (int l = 2; l <= m + 1; l++) {
97+
for (int i = 0; i + l <= m + 1; i++) {
98+
int j = i + l;
99+
dp[i][j] = INT_MAX;
100+
for (int k = i + 1; k < j; k++) {
101+
dp[i][j] = std::min(dp[i][j], dp[i][k] + dp[k][j]);
102+
}
103+
int left = (i == 0) ? 0 : cuts[i - 1];
104+
int right = (j == m + 1) ? n : cuts[j - 1];
105+
dp[i][j] += right - left;
106+
}
107+
}
108+
109+
return dp[0][m + 1];
110+
}
111+
};
112+
```

0 commit comments

Comments
 (0)