-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1891-cutting-ribbons.js
More file actions
53 lines (48 loc) · 1.48 KB
/
1891-cutting-ribbons.js
File metadata and controls
53 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* 1891. Cutting Ribbons
* https://leetcode.com/problems/cutting-ribbons/
* Difficulty: Medium
*
* You are given an integer array ribbons, where ribbons[i] represents the length of the ith
* ribbon, and an integer k. You may cut any of the ribbons into any number of segments of
* positive integer lengths, or perform no cuts at all.
*
* - For example, if you have a ribbon of length 4, you can:
* - Keep the ribbon of length 4,
* - Cut it into one ribbon of length 3 and one ribbon of length 1,
* - Cut it into two ribbons of length 2,
* - Cut it into one ribbon of length 2 and two ribbons of length 1, or
* - Cut it into four ribbons of length 1.
*
* Your task is to determine the maximum length of ribbon, x, that allows you to cut at least
* k ribbons, each of length x. You can discard any leftover ribbon from the cuts. If it is
* impossible to cut k ribbons of the same length, return 0.
*/
/**
* @param {number[]} ribbons
* @param {number} k
* @return {number}
*/
var maxLength = function(ribbons, k) {
let left = 1;
let right = Math.max(...ribbons);
let result = 0;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (helper(mid)) {
result = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
function helper(length) {
let count = 0;
for (const ribbon of ribbons) {
count += Math.floor(ribbon / length);
if (count >= k) return true;
}
return false;
}
};