当前位置:网站首页>Leetcode 1482 guess, how about this question?
Leetcode 1482 guess, how about this question?
2022-07-01 03:19:00 【Air brother like the wind】
1482. Make m The minimum number of days it takes to bunch flowers
Medium difficulty 245 Switch to English to receive dynamic feedback
Give you an array of integers bloomDay, And two integers m and k .
Now it needs to be made m Bouquet of flowers . When making a bouquet , It needs to be used in the garden The adjacent k A flower .
There are many flowers in the garden n A flower , The first i A flower will be there bloomDay[i] In full bloom , just It can be used for a tuft of Huazhong .
Please go back and pick from the garden m The minimum number of days to wait for a bunch of flowers . If you can't get it m The flowers return -1 .
Example 1:
Input :bloomDay = [1,10,3,10,2], m = 3, k = 1 Output :3 explain : Let's observe the blooming process of these three days ,x It means the flowers are blooming , and _ The flowers are still in bloom . Now we need to make 3 Bouquet of flowers , Each bundle just needs 1 Duo . 1 Days later :[x, _, _, _, _] // You can only make 1 Bouquet of flowers 2 Days later :[x, _, _, _, x] // You can only make 2 Bouquet of flowers 3 Days later :[x, _, x, _, x] // You can make 3 Bouquet of flowers , The answer for 3
Example 2:
Input :bloomDay = [1,10,3,10,2], m = 3, k = 2 Output :-1 explain : To make 3 Bouquet of flowers , Each bundle needs 2 A flower , That is to say, we need 6 A flower . And there's only... In the garden 5 A flower , Unable to meet production requirements , return -1 .
Example 3:
Input :bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 Output :12 explain : To make 2 Bouquet of flowers , Each bundle needs 3 Duo . Where is the garden 7 Queen of heaven and 12 The situation is as follows : 7 Days later :[x, x, x, x, _, x, x] You can use it before 3 Make the first bunch of flowers from a blooming flower . But it can't be used after 3 A blooming flower , Because they're not adjacent . 12 Days later :[x, x, x, x, x, x, x] obviously , We can make two bunches of flowers in different ways .
Example 4:
Input :bloomDay = [1000000000,1000000000], m = 1, k = 1 Output :1000000000 explain : Need to wait 1000000000 Genius can pick flowers to make bouquets
Example 5:
Input :bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2 Output :9
Tips :
bloomDay.length == n1 <= n <= 10^51 <= bloomDay[i] <= 10^91 <= m <= 10^61 <= k <= n
Pass times 34,833 Submit the number 58,935
Answer key : The test point of this question is dichotomy , Dichotomy is about determining what to look for , What are the upper and lower boundaries of the search , Whether there must be a solution . What we are looking for in this question is a k The number of combinations of adjacent elements that are smaller than the given value , The lower boundary is k, Because at least k individual , The upper bound is the array maximum , Because it's not meaningful to exceed the maximum value of the array . Judge whether there is a solution , It's simple , As long as the array length can make so many flowers .
Here's the code :
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;
}
}
};Execution results :
adopt
According to the details
Add notes
Execution time :152 ms, In all C++ Defeated in submission 41.29% Users of
Memory consumption :61.8 MB, In all C++ Defeated in submission 25.93% Users of
Pass the test case :91 / 91
Submit once , I'm still very happy .
边栏推荐
- An article explaining the publisher subscriber model and the observer model
- Dart training and sphygmomanometer inflation pump power control DPC
- C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
- Overview of EtherCAT principle
- Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
- HTB-Lame
- 打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
- About the application of MySQL
- The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
- Detailed list of errors related to twincat3 ads of Beifu
猜你喜欢

MySQL index --01--- design principle of index

So easy 将程序部署到服务器
![[applet project development -- JD mall] uni app commodity classification page (Part 2)](/img/f3/752f41f5b5cc16c8a71498ea9cabb5.png)
[applet project development -- JD mall] uni app commodity classification page (Part 2)

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

Redis 教程
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

Communication protocol -- Classification and characteristics Introduction

手把手带你了解一块电路板,从设计到制作(干货)

php批量excel转word

C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
随机推荐
[applet project development -- JD mall] uni app commodity classification page (Part 2)
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
如何校验两个文件内容是否相同
PHP batch Excel to word
最好用的信任关系自动化脚本(shell)
Const and the secret of pointers
How do spark tasks of 10W workers run? (Distributed Computing)
php批量excel转word
Best used trust automation script (shell)
Hal library setting STM32 interrupt
Borrowing constructor inheritance and composite inheritance
Complete training and verification of a neural network based on pytorch
HTB-Lame
EDLines: A real-time line segment detector with a false detection control翻译
Subnet division (10)
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
Introduction to EtherCAT
MySQL knowledge points
【日常训练】1175. 质数排列
Edge Drawing: A combined real-time edge and segment detector 翻译