当前位置:网站首页>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
提交一次过,还是挺开心的。
边栏推荐
- Hello World generation
- Druid监控统计数据源
- Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
- 安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
- 【机器学习】向量化计算 -- 机器学习路上必经路
- 【小程序项目开发-- 京东商城】uni-app之首页商品楼层
- Subnet division (10)
- 倍福TwinCAT3 Ads相关错误详细列表
- The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
- How to verify whether the contents of two files are the same
猜你喜欢

Restcloud ETL WebService data synchronization to local

第03章_用户与权限管理
![Servlet [first introduction]](/img/2a/aff3b93e43550d30a33c1683210d3a.png)
Servlet [first introduction]

Network address translation (NAT) technology

第03章_用戶與權限管理

XXL job User Guide

STM32——一线协议之DS18B20温度采样
![Od modify DLL and exe pop-up contents [OllyDbg]](/img/ff/7249e6e6644376ae048b23bf63b046.jpg)
Od modify DLL and exe pop-up contents [OllyDbg]

Detailed explanation of pointer array and array pointer (comprehensive knowledge points)

Hal library setting STM32 interrupt
随机推荐
Pytest -- plug-in writing
终极套娃 2.0 | 云原生交付的封装
C#实现图的深度优先遍历--非递归代码
Lavaweb [first understanding the solution of subsequent problems]
Saving images of different depths in opencv
Analyze datahub, a new generation metadata platform of 4.7K star
[us match preparation] complete introduction to word editing formula
POI exports excel and displays hierarchically according to parent-child nodes
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
C语言多线程编程入门学习笔记
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
Nacos
8 pits of redis distributed lock
Redis 教程
Communication protocol -- Classification and characteristics Introduction
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
HTB-Lame
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
About the application of MySQL
A few lines of transaction codes cost me 160000 yuan