首页 > 其他分享 >LeetCode 1891. Cutting Ribbons

LeetCode 1891. Cutting Ribbons

时间:2024-04-06 23:33:06浏览次数:16  
标签:Cut Ribbons two length Cutting integer ribbons LeetCode ribbon

原题链接在这里:https://leetcode.com/problems/cutting-ribbons/description/

题目:

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 goal is to obtain k ribbons of all the same positive integer length. You are allowed to throw away any excess ribbon as a result of cutting.

Return the maximum possible positive integer length that you can obtain k ribbons of, or 0 if you cannot obtain k ribbons of the same length.

Example 1:

Input: ribbons = [9,7,5], k = 3
Output: 5
Explanation:
- Cut the first ribbon to two ribbons, one of length 5 and one of length 4.
- Cut the second ribbon to two ribbons, one of length 5 and one of length 2.
- Keep the third ribbon as it is.
Now you have 3 ribbons of length 5.

Example 2:

Input: ribbons = [7,5,9], k = 4
Output: 4
Explanation:
- Cut the first ribbon to two ribbons, one of length 4 and one of length 3.
- Cut the second ribbon to two ribbons, one of length 4 and one of length 1.
- Cut the third ribbon to three ribbons, two of length 4 and one of length 1.
Now you have 4 ribbons of length 4.

Example 3:

Input: ribbons = [5,7,9], k = 22
Output: 0
Explanation: You cannot obtain k ribbons of the same positive integer length.

Constraints:

  • 1 <= ribbons.length <= 105
  • 1 <= ribbons[i] <= 105
  • 1 <= k <= 109

题解:

Keey trying with binary search.

l = 1, r = max(ribbons) + 1.

Guess a mid length, accumulate the count, if the count < k. This means the guess length is still too long, r = mid.

Otherwise, l = mid + 1.

Eventually return l - 1.

Note: since return l - 1, then r neds to be max(ribbons) + 1. Otherwise, [1, 1, 1], k = 3, it will return 0.

Time Complexity: O(n + log(max(ribbons))). n = ribbons.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int maxLength(int[] ribbons, int k) {
 3         if(ribbons == null || ribbons.length == 0 || k <= 0){
 4             return 0;
 5         }
 6 
 7         int l = 1;
 8         int r = 1;
 9         for(int rib : ribbons){
10             r = Math.max(r, rib);
11         }
12 
13         r++;
14 
15         while(l < r){
16             int mid = l + (r - l) / 2;
17             if(getCount(ribbons, mid) < k){
18                 r = mid;
19             }else{
20                 l = mid + 1;
21             }
22         }
23 
24         return l - 1;
25     }
26 
27     private int getCount(int[] ribbons, int can){
28         int res = 0;
29         for(int rib : ribbons){
30             res += rib / can;
31         }
32 
33         return res;
34     }
35 }

类似Capacity To Ship Packages Within D Days.

标签:Cut,Ribbons,two,length,Cutting,integer,ribbons,LeetCode,ribbon
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18118194

相关文章

  • LeetCode 面试经典150题---001
    少年听雨歌楼上,红烛昏罗帐。壮年听雨客舟中,江阔云低、断雁叫西风而今听雨僧庐下鬓已星星也。悲欢离合总无情,一任阶前、点滴到天明。###88.合并两个有序数组给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的......
  • [leetcode 单词搜索]-[trie树]
    解法:trie树importjava.util.*;classSolution{intm,n;char[][]board;String[]querys;publicstaticvoidmain(String[]args){Solutionsolution=newSolution();List<String>list=solution.findWords(newchar......
  • leetcode.206.反转链表
    题目题意:反转一个单链表。示例:输入:1->2->3->4->5->NULL输出:5->4->3->2->1->NULL思路双指针:创建指针p,curr,初始分别指向null和头节点,每轮循环移动一个节点的指向,直到指到最后一个位置为止。递归法:基于双指针。注意递归的退出条件实现双指针classSolution{......
  • leetcode.面试题 02.07. 链表相交
    题目给你两个单链表的头节点headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回null。图示两个链表在节点c1开始相交:思路假a在链表A上移动,b在链表B上移动,a移动完在B上开始,b移动完再A上开始。最终a移动的距离a+c+x,b移动的距......
  • 【LeetCode刷题记录】简单篇-13-罗马数字转整数
    【题目描述】 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。字符数值I1V5X10L50C100D500M1000例如,罗马数字 2 写做 II ,即为两个并列的1。12 ......
  • Leetcode 无重复字符的最长子串
    powcai的滑动窗口解决问题:不断向后滑动窗口,出现重复元素,重新计算窗口,巧妙利用map来记录先前出现的元素的位置索引classSolution{publicintlengthOfLongestSubstring(Strings){//滑动窗口解决该问题intleft=0;intmax=0;Map......
  • Leetcode 412. Fizz Buzz
    给你一个整数n,找出从1到n各个整数的FizzBuzz表示,并用字符串数组answer(下标从1开始)返回结果,其中:answer[i]==“FizzBuzz”如果i同时是3和5的倍数。answer[i]==“Fizz”如果i是3的倍数。answer[i]==“Buzz”如果i是5的倍数。answer[i]......
  • LeetCode in Python 300. Longest Increasing Subsequence (最长递增子序列)
    求最长递增子序列是深度优先搜索(DFS)的一种应用,有两种比较好的方法可以解决。第一种是动态规划法,时间复杂度为O(n*n),即设置边界条件和更新迭代公式求解最优解。第二种使用二分查找将时间复杂度降为O(nlogn)。本文给出两种方法的实现代码及说明。示例:图1最长递增子序列输入......
  • LeetCode 13. 罗马数字转整数
    解题思路通过样例我们可以知道,将目标对应值和下一个目标对应值进行比较,如果小于,则sum=sum+目标对应值,如果大于,则sum=sum-目标对应值。最终的sum就是正确答案。相关代码classSolution{public:intromanToInt(strings){unordered_map<char,int>a;......
  • LeetCode-142. 环形链表 II【哈希表 链表 双指针】
    LeetCode-142.环形链表II【哈希表链表双指针】题目描述:解题思路一:快慢指针判断是否有环见解题思路二:set()解题思路三:0题目描述:给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回null。如果链表中有某个节点,可以通过连续跟踪next......