当前位置:网站首页>leetcode 1482 猜猜看啊,这道题目怎么二分?
leetcode 1482 猜猜看啊,这道题目怎么二分?
2022-07-01 03:09:00 【风一样的航哥】
难度中等245收藏分享切换为英文接收动态反馈
给你一个整数数组 bloomDay,以及两个整数 m 和 k 。
现需要制作 m 束花。制作花束时,需要使用花园中 相邻的 k 朵花 。
花园中有 n 朵花,第 i 朵花会在 bloomDay[i] 时盛开,恰好 可以用于 一束 花中。
请你返回从花园中摘 m 束花需要等待的最少的天数。如果不能摘到 m 束花则返回 -1 。
示例 1:
输入:bloomDay = [1,10,3,10,2], m = 3, k = 1 输出:3 解释:让我们一起观察这三天的花开过程,x 表示花开,而 _ 表示花还未开。 现在需要制作 3 束花,每束只需要 1 朵。 1 天后:[x, _, _, _, _] // 只能制作 1 束花 2 天后:[x, _, _, _, x] // 只能制作 2 束花 3 天后:[x, _, x, _, x] // 可以制作 3 束花,答案为 3
示例 2:
输入:bloomDay = [1,10,3,10,2], m = 3, k = 2 输出:-1 解释:要制作 3 束花,每束需要 2 朵花,也就是一共需要 6 朵花。而花园中只有 5 朵花,无法满足制作要求,返回 -1 。
示例 3:
输入:bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 输出:12 解释:要制作 2 束花,每束需要 3 朵。 花园在 7 天后和 12 天后的情况如下: 7 天后:[x, x, x, x, _, x, x] 可以用前 3 朵盛开的花制作第一束花。但不能使用后 3 朵盛开的花,因为它们不相邻。 12 天后:[x, x, x, x, x, x, x] 显然,我们可以用不同的方式制作两束花。
示例 4:
输入:bloomDay = [1000000000,1000000000], m = 1, k = 1 输出:1000000000 解释:需要等 1000000000 天才能采到花来制作花束
示例 5:
输入:bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2 输出:9
提示:
bloomDay.length == n1 <= n <= 10^51 <= bloomDay[i] <= 10^91 <= m <= 10^61 <= k <= n
通过次数34,833提交次数58,935
题解:本题的考点是二分法,二分法就要确定查找的范围是什么,查找的上下边界是什么,是否一定有解。本题中查找的是一个k个相邻的元素且都小于给定值的组合个数,下边界是k,因为至少有k个,上边界是数组最大值,因为超过数组最大值就没有太多意思了。判断是否有解,很简单,只要数组长度能够做出来这么多花就行了。
以下是代码:
class Solution {
int countTripleNums(vector<int>& nums,int tarValue,int k)
{
int count=0;
int i=0;
while(i<=nums.size()-k)
{
bool isfind=true;
for(int j=0;j<k;j++)
{
if(nums[i+j]>tarValue)
{
i=j+i+1;
isfind = false;
break;
}
}
if(isfind)
{
count++;
i+=k;
}
}
return count;
}
public:
int minDays(vector<int>& bloomDay, int m, int k) {
if(m*k>bloomDay.size())
return -1;
else
{
int left = k, right = *max_element(bloomDay.begin(), bloomDay.end());
while (left < right)
{
int mid = (right - left) / 2 + left;
if (countTripleNums(bloomDay,mid,k) < m)
{
left = mid + 1;
}
else
right = mid;
}
return left;
}
}
};执行结果:
通过
显示详情
添加备注
执行用时:152 ms, 在所有 C++ 提交中击败了41.29%的用户
内存消耗:61.8 MB, 在所有 C++ 提交中击败了25.93%的用户
通过测试用例:91 / 91
提交一次过,还是挺开心的。
边栏推荐
- MySQL knowledge points
- 终极套娃 2.0 | 云原生交付的封装
- split(),splice(),slice()傻傻分不清楚?
- POI exports excel and displays hierarchically according to parent-child nodes
- Redis高效点赞与取消功能
- A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
- Overview of EtherCAT principle
- Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
- Common interview questions for performance test
- Edge Drawing: A combined real-time edge and segment detector 翻译
猜你喜欢

VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可

Overview of EtherCAT principle

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

Kmeans

servlet【初识】

EtherCAT简介

Network address translation (NAT) technology

Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example

Hello World generation

JUC learning
随机推荐
pytest-fixture
【Qt】添加第三方库的知识补充
别再说不会解决 “跨域“ 问题啦
A few lines of transaction codes cost me 160000 yuan
Pytest -- plug-in writing
Poj-3486-computers[dynamic planning]
PHP batch Excel to word
xxl-job使用指南
C#实现图的深度优先遍历--非递归代码
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
C语言多线程编程入门学习笔记
Borrowing constructor inheritance and composite inheritance
The best learning method in the world: Feynman learning method
lavaweb【初识后续问题的解决】
# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Analyze datahub, a new generation metadata platform of 4.7K star
[us match preparation] complete introduction to word editing formula
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
手把手带你了解一块电路板,从设计到制作(干货)
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)