1891. Cutting Ribbons
Description
You are given an integer array ribbons, where ribbons[i] represents the length of the i^th 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
3and one ribbon of length1,Cut it into two ribbons of length
2,Cut it into one ribbon of length
2and two ribbons of length1, orCut 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.
Example 1:
1 | Input: ribbons = [9,7,5], k = 3 |
Example 3:
1 | Input: ribbons = [5,7,9], k = 22 |
Constraints:
1 <= ribbons.length <= 10^51 <= ribbons[i] <= 10^51 <= k <= 10^9
Hints/Notes
- 2025/02/09 Q1
- binary search
- Leetcode solution(checked)
Solution
Language: C++
1 | class Solution { |