Skip to content

Commit 78b45e6

Browse files
committed
Add solution #1547
1 parent 8210923 commit 78b45e6

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,361 LeetCode solutions in JavaScript
1+
# 1,362 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1182,6 +1182,7 @@
11821182
1544|[Make The String Great](./solutions/1544-make-the-string-great.js)|Easy|
11831183
1545|[Find Kth Bit in Nth Binary String](./solutions/1545-find-kth-bit-in-nth-binary-string.js)|Medium|
11841184
1546|[Maximum Number of Non-Overlapping Subarrays With Sum Equals Target](./solutions/1546-maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.js)|Medium|
1185+
1547|[Minimum Cost to Cut a Stick](./solutions/1547-minimum-cost-to-cut-a-stick.js)|Hard|
11851186
1550|[Three Consecutive Odds](./solutions/1550-three-consecutive-odds.js)|Easy|
11861187
1551|[Minimum Operations to Make Array Equal](./solutions/1551-minimum-operations-to-make-array-equal.js)|Medium|
11871188
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1547. Minimum Cost to Cut a Stick
3+
* https://leetcode.com/problems/minimum-cost-to-cut-a-stick/
4+
* Difficulty: Hard
5+
*
6+
* Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a
7+
* stick of length 6 is labelled as follows.
8+
*
9+
* Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.
10+
*
11+
* You should perform the cuts in order, you can change the order of the cuts as you wish.
12+
*
13+
* The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs
14+
* of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of
15+
* their lengths is the length of the stick before the cut). Please refer to the first example
16+
* for a better explanation.
17+
*
18+
* Return the minimum total cost of the cuts.
19+
*/
20+
21+
/**
22+
* @param {number} n
23+
* @param {number[]} cuts
24+
* @return {number}
25+
*/
26+
var minCost = function(n, cuts) {
27+
cuts.push(0, n);
28+
cuts.sort((a, b) => a - b);
29+
30+
const memo = new Map();
31+
32+
return computeMinCost(0, cuts.length - 1);
33+
34+
function computeMinCost(left, right) {
35+
if (right - left <= 1) return 0;
36+
37+
const key = `${left},${right}`;
38+
if (memo.has(key)) return memo.get(key);
39+
40+
let minCost = Infinity;
41+
for (let i = left + 1; i < right; i++) {
42+
const cost = (cuts[right] - cuts[left]) + computeMinCost(left, i) + computeMinCost(i, right);
43+
minCost = Math.min(minCost, cost);
44+
}
45+
46+
memo.set(key, minCost);
47+
return minCost;
48+
}
49+
};

0 commit comments

Comments
 (0)