id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> freq;\r\n for(int &x : arr) {\r\n freq[x]++;\r\n }\r\n vector<int> fast;\r\n for(auto x : freq) {\r\n fast.push_back(x.second);\r\n }\r\n make_heap(fast.begin(),fast.end());\r\n priority_queue<int, vector<int>, greater<int>> pq(fast.begin(), fast.end());\r\n int x;\r\n for(int i = 0; i < k; i++) {\r\n if(pq.top() > 1) {\r\n x = pq.top();\r\n pq.pop();\r\n pq.push(x - 1);\r\n }\r\n else {\r\n pq.pop();\r\n }\r\n }\r\n return pq.size();\r\n }\r\n};", "memory": "67600" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n \r\n int n=arr.size();\r\n\r\n map<int,int> m;\r\n\r\n for(auto it:arr){\r\n m[it]++;\r\n }\r\n\r\n priority_queue<int,vector<int>,greater<int>> pq;\r\n\r\n for(auto i:m){\r\n\r\n pq.push(i.second);\r\n }\r\n\r\n\r\n while(!pq.empty()){\r\n\r\n k-=pq.top();\r\n\r\n if(k<0){\r\n\r\n return pq.size();\r\n }\r\n\r\n\r\n pq.pop();\r\n }\r\n\r\n\r\n return 0;\r\n\r\n\r\n }\r\n};", "memory": "67700" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int, int> counts;\r\n\r\n for(int i = 0; i < arr.size(); i++)\r\n counts[arr[i]]++;\r\n\r\n priority_queue<int, vector<int>, greater<int>> repeats;\r\n for(auto& pair : counts)\r\n repeats.push(pair.second);\r\n\r\n while(arr.size() && k) {\r\n int amount = repeats.top();\r\n \r\n if (amount <= k) {\r\n k -= amount;\r\n repeats.pop();\r\n } else {\r\n break;\r\n }\r\n }\r\n\r\n return repeats.size();\r\n }\r\n};", "memory": "67800" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int> m;\r\n for(int x : arr)\r\n m[x]++;\r\n vector<int> freq;\r\n for(auto [v,x] : m)\r\n freq.push_back(x);\r\n sort(freq.begin(),freq.end());\r\n int n = freq.size();\r\n for(int i = 0 ; i < n ; i++){\r\n k -= freq[i];\r\n if(k < 0)\r\n return n-i;\r\n }\r\n return 0;\r\n }\r\n};", "memory": "67900" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int> mp;\r\n vector<int> check;\r\n for(auto i:arr) mp[i]++;\r\n for(auto i:mp) check.push_back(i.second);\r\n sort(check.begin(),check.end());\r\n int total = mp.size();\r\n for(auto i:check){\r\n if(k-i<0){\r\n break;\r\n }\r\n else k-=i,total--;\r\n }\r\n\r\n return total;\r\n\r\n \r\n }\r\n};", "memory": "67900" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int>mp;\r\n for(int i=0;i<arr.size();i++){\r\n mp[arr[i]]++;\r\n }\r\n vector<int>freq;\r\n for(auto it=mp.begin();it!=mp.end();it++){\r\n freq.push_back(it->second);\r\n }\r\n sort(freq.begin(),freq.end());\r\n // int j=0;\r\n int i;\r\n for(i=0;i<freq.size();i++){\r\n if(freq[i]<=k){\r\n k-=freq[i];\r\n }\r\n else break;\r\n }\r\n return freq.size()-i;\r\n }\r\n};", "memory": "68000" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n static bool compare(pair<int,int>&a, pair<int,int>&b){\r\n return a.second<b.second;\r\n }\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\r\n unordered_map<int,int>mp;\r\n for(auto &i : arr){\r\n mp[i]++;\r\n }\r\n vector<pair<int,int>>nums;\r\n for(auto i : mp){\r\n nums.push_back({i.first, i.second});\r\n }\r\n sort(nums.begin(), nums.end(), compare);\r\n\r\n int i =0 ;\r\n while(i<nums.size() && k>0){\r\n if(k-mp[nums[i].first]>=0){\r\n k-= mp[nums[i].first];\r\n mp[nums[i].first]=0;\r\n i++;\r\n }\r\n else{\r\n k=0;\r\n }\r\n }\r\n return nums.size()-i;\r\n }\r\n};", "memory": "68100" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> mp;\r\n for(auto i : arr){\r\n mp[i]++;\r\n }\r\n vector<pair<int,int>>v;\r\n for(auto it=mp.begin();it!=mp.end();it++)\r\n {\r\n \tv.push_back(make_pair(it->second,it->first)); \r\n }\r\n sort(v.begin(),v.end());\r\n \r\n int idx=0;\r\n \r\n while(idx<v.size())\r\n {\r\n \t int ele=v[idx].second;\r\n \t int freq=v[idx].first;\r\n if(k==0)\r\n\t {\r\n\t \tbreak;\r\n\t }\r\n \t else if(freq > k) \r\n \t {\r\n \t k=0;\r\n }\r\n\t else {\r\n\t \tk=k-freq;\r\n\t \tidx++; \r\n\t }\r\n\t \r\n }\r\n \r\n return v.size()-idx;\r\n }\r\n};", "memory": "68600" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int>m;\r\nint count=0;\r\n for(int i=0;i<arr.size();i++)\r\n { m[arr[i]]++; }\r\nvector<pair<int,int>>v;\r\nfor(auto it:m)\r\n{ v.push_back(make_pair(it.second,it.first)); }\r\nsort(v.begin(),v.end());\r\nint s=v.size();\r\nfor(int i=0;i<s;i++)\r\n{\r\n if(v[i].first<=k){\r\n k=k-v[i].first; count++;\r\n } else{\r\nbreak;\r\n }}\r\nreturn s-count;\r\n }\r\n};", "memory": "68700" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int>m;\r\nint count=0;\r\n for(int i=0;i<arr.size();i++)\r\n {\r\n m[arr[i]]++;\r\n }\r\nvector<pair<int,int>>v;\r\n\r\nfor(auto it:m){\r\n v.push_back(make_pair(it.second,it.first));\r\n}\r\nsort(v.begin(),v.end());\r\n\r\nint s=v.size();\r\n\r\nfor(auto& p:v)\r\n{\r\n if(p.first<=k){\r\n k=k-p.first;\r\n count++;\r\n }\r\n else{\r\nbreak;\r\n }\r\n}\r\nreturn s-count;\r\n }\r\n};", "memory": "68800" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
2
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int>m;\r\nint count=0;\r\n for(int i=0;i<arr.size();i++)\r\n { m[arr[i]]++; }\r\nvector<pair<int,int>>v;\r\n\r\nfor(auto it:m){\r\n v.push_back(make_pair(it.second,it.first));\r\n}\r\nsort(v.begin(),v.end());\r\n\r\nint s=v.size();\r\n\r\nfor(int i=0;i<s;i++)\r\n{\r\n if(v[i].first<=k){\r\n k=k-v[i].first;\r\n count++;\r\n }\r\n else{\r\nbreak;\r\n }\r\n}\r\nreturn s-count;\r\n }\r\n};", "memory": "68800" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> map;\r\n for(auto x:arr)\r\n map[x]++;\r\n vector<pair<int,int>> v;\r\n for(auto x:map)\r\n v.push_back(make_pair(x.second,x.first));\r\n sort(v.begin(),v.end());\r\n int i=0,n=v.size();\r\n // for(auto x:v)\r\n // cout<<x.first<<\" \"<<x.second<<endl;\r\n while(k>0)\r\n {\r\n k-=v[i].first;\r\n i++;\r\n n--;\r\n }\r\n if(k<0)n++;\r\n return n;\r\n }\r\n};", "memory": "68900" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> m;\r\n for(auto it:arr){\r\n m[it]++;\r\n }\r\n\r\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\r\n for(auto it:m){\r\n pq.push({it.second, it.first});\r\n }\r\n\r\n while(k){\r\n auto top=pq.top(); \r\n pq.pop();\r\n\r\n if(top.first>k){\r\n pq.push({top.first-k, top.second});\r\n k=0;\r\n }\r\n else{\r\n k-=top.first;\r\n }\r\n }\r\n\r\n return pq.size();\r\n }\r\n};", "memory": "69000" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> mp;\r\n for(int i=0;i<arr.size();i++){\r\n mp[arr[i]]++;\r\n }\r\n int s = mp.size();\r\n\r\n map<int,vector<int>> freq;\r\n for(auto x:mp){\r\n freq[x.second].push_back(x.first);\r\n }\r\n\r\n int deleted =0;\r\n while(k){\r\n for(auto x:freq){\r\n vector<int> temp = x.second;\r\n if(k<x.first)\r\n return s-deleted;\r\n while(k>= x.first && temp.size()>0){\r\n k = k-x.first;\r\n temp.erase(temp.begin());\r\n deleted++;\r\n }\r\n\r\n if(temp.size()==0)\r\n freq.erase(x.first);\r\n }\r\n }\r\n\r\n return s-deleted;\r\n }\r\n};", "memory": "69100" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int> m;\r\n for(auto i: arr){\r\n m[i]++;\r\n }\r\n vector<int> ans;\r\n for(auto i:m){\r\n ans.push_back(i.second);\r\n }\r\n sort(ans.begin(),ans.end());\r\n queue<int> q;\r\n for(auto i:ans){\r\n q.push(i);\r\n }\r\n int count=0;\r\n while(count<k){\r\n count++;\r\n // int a=q.front();\r\n q.front()--;\r\n if(q.front()==0){\r\n q.pop();\r\n }\r\n\r\n }\r\n return q.size();\r\n }\r\n};", "memory": "69500" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int len = arr.size();\r\n if (k >= len) {\r\n return 0;\r\n }\r\n unordered_map<int, int> freq;\r\n for (auto num : arr) {\r\n ++freq[num];\r\n }\r\n vector<int> data;\r\n for (auto [num, count] : freq) {\r\n data.push_back(count);\r\n }\r\n sort(data.rbegin(), data.rend());\r\n int i = 1;\r\n for (auto num : data) {\r\n len -= num;\r\n if (k >= len) {\r\n return i;\r\n }\r\n ++i;\r\n }\r\n return 0;\r\n }\r\n};", "memory": "69700" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int len = arr.size();\r\n if (k >= len) {\r\n return 0;\r\n }\r\n unordered_map<int, int> freq;\r\n for (auto num : arr) {\r\n ++freq[num];\r\n }\r\n vector<int> data;\r\n for (auto [num, count] : freq) {\r\n data.push_back(count);\r\n }\r\n sort(data.rbegin(), data.rend());\r\n int i = 1;\r\n for (auto num : data) {\r\n len -= num;\r\n if (k >= len) {\r\n return i;\r\n }\r\n ++i;\r\n }\r\n return 0;\r\n }\r\n};", "memory": "69800" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int len = arr.size();\r\n if (k >= len) {\r\n return 0;\r\n }\r\n unordered_map<int, int> freq;\r\n for (auto num : arr) {\r\n ++freq[num];\r\n }\r\n vector<int> data;\r\n for (auto [num, count] : freq) {\r\n data.push_back(count);\r\n }\r\n sort(data.rbegin(), data.rend());\r\n int i = 1;\r\n for (auto num : data) {\r\n len -= num;\r\n if (k >= len) {\r\n return i;\r\n }\r\n ++i;\r\n }\r\n return 0;\r\n }\r\n};", "memory": "69800" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int len = arr.size();\r\n if (k >= len) {\r\n return 0;\r\n }\r\n unordered_map<int, int> freq;\r\n for (auto num : arr) {\r\n ++freq[num];\r\n }\r\n vector<int> data;\r\n for (auto [num, count] : freq) {\r\n data.push_back(count);\r\n }\r\n sort(data.rbegin(), data.rend());\r\n int i = 1;\r\n for (auto num : data) {\r\n len -= num;\r\n if (k >= len) {\r\n return i;\r\n }\r\n ++i;\r\n }\r\n return 0;\r\n }\r\n};", "memory": "69900" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int len = arr.size();\r\n if (k >= len) {\r\n return 0;\r\n }\r\n unordered_map<int, int> freq;\r\n for (auto num : arr) {\r\n ++freq[num];\r\n }\r\n vector<int> data;\r\n for (auto [num, count] : freq) {\r\n data.push_back(count);\r\n }\r\n sort(data.rbegin(), data.rend());\r\n int i = 1;\r\n for (auto num : data) {\r\n len -= num;\r\n if (k >= len) {\r\n return i;\r\n }\r\n ++i;\r\n }\r\n return 0;\r\n }\r\n};", "memory": "69900" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int, int> freqs;\r\n for (int i = 0; i < arr.size(); i++){\r\n freqs[arr[i]]++;\r\n }\r\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\r\n for (auto it = freqs.begin(); it != freqs.end(); it++){\r\n pq.push({it->second, it->first});\r\n }\r\n while (k > 0 && pq.top().first <= k && !pq.empty()){\r\n k -= pq.top().first;\r\n freqs.erase(pq.top().second);\r\n pq.pop(); \r\n }\r\n return freqs.size();\r\n }\r\n};", "memory": "70100" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "struct Element {\r\n int val;\r\n int count;\r\n};\r\n\r\nclass Compare {\r\n public:\r\n bool operator()(Element a, Element b){\r\n if(a.count > b.count){\r\n return true;\r\n }\r\n return false;\r\n }\r\n};\r\n\r\n\r\nclass Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int size = arr.size();\r\n\r\n int uniqueCount = 0;\r\n priority_queue<Element, vector<Element>, Compare> pq;\r\n map<int, int> mp;\r\n\r\n for(int i=0; i<size; i++) {\r\n if(mp.find(arr[i]) == mp.end()) {\r\n uniqueCount++;\r\n mp[arr[i]] = 1;\r\n }else {\r\n mp[arr[i]]++;\r\n }\r\n }\r\n\r\n if(k == 0 || size == 0) {\r\n return uniqueCount;\r\n }\r\n\r\n for(auto it=mp.begin(); it!=mp.end(); it++) {\r\n pq.push(Element(it->first, it->second));\r\n }\r\n\r\n int decreaseCount = k;\r\n\r\n while(decreaseCount > 0 && !pq.empty()) {\r\n Element e = pq.top();\r\n pq.pop();\r\n\r\n // element count is greater than what we can reduce\r\n if(decreaseCount < e.count) {\r\n return uniqueCount;\r\n }else {\r\n decreaseCount = decreaseCount - e.count;\r\n uniqueCount--;\r\n }\r\n }\r\n\r\n return uniqueCount;\r\n }\r\n};", "memory": "70200" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int n = arr.size();\r\n priority_queue<pair<int,int>>pq;\r\n map<int,int>mp;\r\n for(int i=0;i<n;i++) mp[arr[i]]++;\r\n for(auto it:mp){\r\n pq.push({-1*it.second,it.first});\r\n }\r\n while(!pq.empty() && k > 0){\r\n int count = -1*pq.top().first;\r\n if(count <= k){\r\n k = k-count;\r\n pq.pop();\r\n }\r\n else{\r\n k=0;\r\n }\r\n }\r\n return pq.size();\r\n }\r\n};", "memory": "70300" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int>m;\r\nint count=0;\r\n for(int i=0;i<arr.size();i++)\r\n { m[arr[i]]++; }\r\nvector<pair<int,int>>v;\r\nfor(auto it:m)\r\n{ v.push_back(make_pair(it.second,it.first)); }\r\nsort(v.begin(),v.end());\r\nint s=v.size();\r\nfor(int i=0;i<s;i++)\r\n{\r\n if(v[i].first<=k){\r\n k=k-v[i].first; count++;\r\n } else{\r\nbreak;\r\n }}\r\nreturn s-count;\r\n }\r\n};", "memory": "70400" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int, int> mp;\r\n for (int x : arr) mp[x]++;\r\n vector<int> counts;\r\n for (auto& p : mp) counts.push_back(p.second);\r\n sort(counts.begin(), counts.end());\r\n vector<int> prefix;\r\n prefix.push_back(counts[0]);\r\n for (int i = 1; i < counts.size(); i++) {\r\n prefix.push_back(prefix.back() + counts[i]);\r\n }\r\n // for (int val : prefix) cout << val << '\\n';\r\n auto it = upper_bound(prefix.begin(), prefix.end(), k);\r\n return mp.size() - (it - prefix.begin());\r\n\r\n }\r\n};", "memory": "70500" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n static bool cmp(pair<int,int>&a,pair<int,int>&b){\r\n return a.second < b.second;\r\n }\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int> mp;\r\n for(int i=0;i<arr.size();i++){\r\n mp[arr[i]]++;\r\n }\r\n deque<pair<int,int>> dq;\r\n for(auto i:mp){\r\n dq.push_back({i.first,i.second});\r\n }\r\n sort(dq.begin(),dq.end(),cmp);\r\n\r\n while(dq.front().second <=k && dq.size()>0){\r\n k-=dq.front().second;\r\n dq.pop_front();\r\n }\r\n return dq.size();\r\n }\r\n};", "memory": "70600" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n if(arr.size()==k ) {\r\n return 0;\r\n }\r\n priority_queue<pair<int,int>,vector<pair<int,int> >,compare> minHeap;\r\n unordered_map<int,int> map;\r\n for(auto& num:arr) {\r\n map[num]++;\r\n\r\n }\r\n for(auto& key:map) {\r\n \r\n minHeap.push(key);\r\n \r\n }\r\n vector<pair<int,int > > ans;\r\n int sum=0;\r\n while(sum<=k) {\r\n int sec=minHeap.top().second;\r\n sum+=sec;\r\n pair<int,int> p1=minHeap.top();\r\n minHeap.pop();\r\n\r\n if(sum>k) {\r\n minHeap.push(p1);\r\n break;\r\n }\r\n }\r\n while(!minHeap.empty()) {\r\n ans.push_back(minHeap.top());\r\n minHeap.pop();\r\n\r\n }\r\n return ans.size();\r\n }\r\n struct compare {\r\n bool operator() (const pair<int,int>& a,const pair<int,int>& b) {\r\n if(a.second==b.second) {\r\n return a.first > b.second;\r\n }\r\n return a.second > b.second;\r\n }\r\n };\r\n};", "memory": "71000" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n if(arr.size()==k ) {\r\n return 0;\r\n }\r\n priority_queue<pair<int,int>,vector<pair<int,int> >,compare> minHeap;\r\n unordered_map<int,int> map;\r\n for(auto& num:arr) {\r\n map[num]++;\r\n\r\n }\r\n for(auto& key:map) {\r\n \r\n minHeap.push(key);\r\n \r\n }\r\n vector<pair<int,int > > ans;\r\n int sum=0;\r\n while(sum<=k) {\r\n int sec=minHeap.top().second;\r\n sum+=sec;\r\n pair<int,int> p1=minHeap.top();\r\n minHeap.pop();\r\n\r\n if(sum>k) {\r\n minHeap.push(p1);\r\n break;\r\n }\r\n }\r\n while(!minHeap.empty()) {\r\n ans.push_back(minHeap.top());\r\n minHeap.pop();\r\n\r\n }\r\n return ans.size();\r\n }\r\n struct compare {\r\n bool operator() (const pair<int,int>& a,const pair<int,int>& b) {\r\n if(a.second==b.second) {\r\n return a.first > b.second;\r\n }\r\n return a.second > b.second;\r\n }\r\n };\r\n};", "memory": "71200" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n if(arr.size()==k ) {\r\n return 0;\r\n }\r\n priority_queue<pair<int,int>,vector<pair<int,int> >,compare> minHeap;\r\n unordered_map<int,int> map;\r\n for(auto& num:arr) {\r\n map[num]++;\r\n\r\n }\r\n for(auto& key:map) {\r\n minHeap.push(key);\r\n \r\n }\r\n vector<pair<int,int > > ans;\r\n int sum=0;\r\n while(sum<=k) {\r\n int sec=minHeap.top().second;\r\n sum+=sec;\r\n pair<int,int> p1=minHeap.top();\r\n minHeap.pop();\r\n if(sum>k) {\r\n minHeap.push(p1);\r\n break;\r\n }\r\n }\r\n while(!minHeap.empty()) {\r\n ans.push_back(minHeap.top());\r\n minHeap.pop();\r\n\r\n }\r\n return ans.size();\r\n }\r\n struct compare {\r\n bool operator() (const pair<int,int>& a,const pair<int,int>& b) {\r\n if(a.second==b.second) {\r\n return a.first > b.second;\r\n }\r\n return a.second > b.second;\r\n }\r\n };\r\n};", "memory": "71200" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n std::unordered_map<int, int> num_map;\r\n for (int v: arr) {\r\n if (num_map.contains(v)) {\r\n int update = num_map[v]+1;\r\n num_map.erase(v);\r\n num_map.insert({v, update});\r\n } else {\r\n num_map.insert({v, 1});\r\n }\r\n }\r\n\r\n int num_nums = num_map.size();\r\n\r\n std::deque<int> occurs;\r\n for (auto it = num_map.begin(); it != num_map.end(); it++) {\r\n occurs.push_back((*it).second);\r\n }\r\n std::sort(occurs.begin(), occurs.end());\r\n\r\n while (k > 0 && !occurs.empty()) {\r\n if (occurs.front() > k) break;\r\n k -= occurs.front();\r\n occurs.pop_front();\r\n num_nums--;\r\n }\r\n\r\n return num_nums;\r\n\r\n }\r\n};", "memory": "71300" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n std::unordered_map<int, int> num_map;\r\n for (int v: arr) {\r\n if (num_map.contains(v)) {\r\n int update = num_map[v]+1;\r\n num_map.erase(v);\r\n num_map.insert({v, update});\r\n } else {\r\n num_map.insert({v, 1});\r\n }\r\n }\r\n\r\n int num_nums = num_map.size();\r\n\r\n std::deque<int> occurs;\r\n for (auto it = num_map.begin(); it != num_map.end(); it++) {\r\n occurs.push_back((*it).second);\r\n }\r\n std::sort(occurs.begin(), occurs.end());\r\n\r\n while (k > 0 && !occurs.empty()) {\r\n if (occurs.front() > k) break;\r\n k -= occurs.front();\r\n occurs.pop_front();\r\n num_nums--;\r\n }\r\n\r\n return num_nums;\r\n\r\n }\r\n};", "memory": "71300" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> counts;\r\n for (auto i : arr) counts[i]++;\r\n auto comp = [&](auto left, auto right){\r\n if (counts[left] == counts[right]) return left < right;\r\n return counts[left] < counts[right];\r\n };\r\n unordered_set<int> s;\r\n sort(arr.begin(), arr.end(), comp);\r\n \r\n for (auto i = k; i < arr.size(); i++) s.insert(arr[i]);\r\n return s.size();\r\n }\r\n};", "memory": "71400" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> hash; // <value, count>\r\n for (int v : arr) {\r\n hash[v]++;\r\n }\r\n /* Actually we don't need values now, only the counts.\r\n * But it's easier to debug */\r\n vector<pair<int, int>> vec; // <count, value>\r\n for (auto [value, count] : hash) {\r\n vec.push_back({count, value});\r\n }\r\n /* sort in decreasing order,\r\n * so that we removed values from the end */\r\n sort(vec.rbegin(), vec.rend());\r\n int size = vec.size();\r\n int i = size-1;\r\n while (i >= 0) {\r\n auto [count, value] = vec[i];\r\n if (k < count)\r\n break;\r\n k -= count;\r\n i--;\r\n }\r\n return i+1; \r\n }\r\n};", "memory": "72200" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> hash; // <value, count>\r\n for (int v : arr) {\r\n hash[v]++;\r\n }\r\n /* Actually we don't need values now, only the counts.\r\n * But it's easier to debug */\r\n vector<pair<int, int>> vec; // <count, value>\r\n for (auto [value, count] : hash) {\r\n vec.push_back({count, value});\r\n }\r\n /* sort in decreasing order,\r\n * so that we removed values from the end */\r\n sort(vec.rbegin(), vec.rend());\r\n int size = vec.size();\r\n int i;\r\n for (i = size-1; i >= 0; i--) {\r\n auto [count, value] = vec[i];\r\n if (k < count)\r\n break;\r\n k -= count;\r\n }\r\n return i+1; \r\n }\r\n};", "memory": "72300" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n\r\n typedef long long ll;\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) \r\n {\r\n int n = arr.size();\r\n unordered_map <ll, int> mpp;\r\n\r\n for(auto x: arr)\r\n mpp[x]++;\r\n\r\n priority_queue <ll, vector <ll>, greater <ll>> pq;\r\n\r\n for(auto x: mpp)\r\n pq.push(x.second);\r\n\r\n while(!pq.empty() && k > 0)\r\n {\r\n k -= pq.top();\r\n\r\n if(k >= 0)\r\n pq.pop();\r\n }\r\n\r\n return pq.size(); \r\n }\r\n};", "memory": "72900" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<long long, long long> tmp;\r\n for(auto z: arr){\r\n tmp[z]++;\r\n }\r\n vector<pair<long long, long long>> v;\r\n for(auto z: tmp){\r\n v.push_back({z.second, z.first});\r\n }\r\n sort(v.begin(), v.end());\r\n for(auto z: v){\r\n cout << z.first << \" \" << z.second << endl;\r\n }\r\n long long counter = k;\r\n int ue = tmp.size();\r\n cout << ue;\r\n for(long long i=0; i<k; i++){\r\n if( counter>=v[i].first){\r\n counter-=v[i].first;\r\n ue--;\r\n }else{\r\n return ue;\r\n }\r\n\r\n }\r\n return ue;\r\n }\r\n};", "memory": "75100" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n vector<vector<int>> countVec(arr.size()+1);\r\n unordered_map<int, int> countMap;\r\n for(int num : arr) {\r\n countMap[num]++;\r\n }\r\n for(auto it = countMap.begin(); it != countMap.end(); ++it) {\r\n int count = it->second;\r\n int num = it->first;\r\n countVec[count].push_back(num);\r\n }\r\n int totalUnique = 0;\r\n for(int i = 0; i < countVec.size(); ++i) {\r\n if(!countVec[i].empty()) {\r\n int count = i;\r\n for(int j = 0; j < countVec[i].size(); ++j) {\r\n if(count > k) {\r\n totalUnique++;\r\n } else {\r\n k -= count;\r\n }\r\n }\r\n }\r\n }\r\n return totalUnique;\r\n }\r\n};", "memory": "75600" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n #define pr pair <int , int> \r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map <int , int > mp;\r\n vector <pr> vect;\r\n priority_queue <pr> pq;\r\n priority_queue <pr , vector <pr> , greater <pr> > pq1; \r\n for(auto &it : arr)\r\n mp[it]++;\r\n for(auto &it : mp)\r\n vect.push_back({it.second , it.first});\r\n for(auto & it : vect) {\r\n pq.push(it);\r\n if(pq.size() > k)\r\n pq.pop();\r\n }\r\n while(!pq.empty()) {\r\n pq1.push(pq.top());\r\n pq.pop();\r\n }\r\n int removed = 0;\r\n while(!pq1.empty()) {\r\n int count = pq1.top().first , val = pq1.top().second;\r\n if( k >= count) {\r\n k -= count;\r\n removed++;\r\n }\r\n else\r\n break;\r\n pq1.pop();\r\n }\r\n return mp.size() - removed;\r\n\r\n \r\n }\r\n};", "memory": "75800" }
1,604
<p>Given an array of integers&nbsp;<code>arr</code>&nbsp;and an integer <code>k</code>.&nbsp;Find the <em>least number of unique integers</em>&nbsp;after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p> <ol> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input: </strong>arr = [5,5,4], k = 1 <strong>Output: </strong>1 <strong>Explanation</strong>: Remove the single 4, only 5 is left. </pre> <strong class="example">Example 2:</strong> <pre> <strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3 <strong>Output: </strong>2 <strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5</code></li> <li><code>1 &lt;= arr[i] &lt;= 10^9</code></li> <li><code>0 &lt;= k&nbsp;&lt;= arr.length</code></li> </ul>
3
{ "code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n \r\n unordered_map<int,int>mp;\r\n if(k==arr.size())return 0;\r\n\r\n for(int i = 0;i<arr.size();i++){\r\n mp[arr[i]]++;\r\n }\r\n \r\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\r\n \r\n for(auto it : mp){\r\n pq.push({it.second,it.first});\r\n }\r\n set<int>s;\r\n \r\n while(!pq.empty() && k>0){\r\n\r\n int num = pq.top().second;\r\n int f = pq.top().first;\r\n pq.pop();\r\n \r\n if(k>=f){\r\n k-=f;\r\n }\r\n else{\r\n int nw = f - k;\r\n k=0;\r\n pq.push({nw,num});\r\n cout<<\"hm\"<<\" \";\r\n }\r\n cout<<pq.size()<<\" \";\r\n \r\n\r\n }\r\n\r\n while(!pq.empty()){\r\n int num = pq.top().second;\r\n pq.pop();\r\n\r\n s.insert(num);\r\n }\r\n\r\n return s.size();\r\n\r\n }\r\n};", "memory": "77700" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nstd::array<int, 100'000> input;\nsize_t N;\n\nint parse_string(const std::string& s) {\n int max_number = std::numeric_limits<int>::min();\n int number = 0;\n N = 0;\n for (size_t i = 1; i < s.size(); ++i) {\n if (s[i] == ',' || s[i] == ']') {\n input[N] = number;\n N++;\n max_number = std::max(max_number, number);\n number = 0;\n } else {\n number = number * 10 + (s[i] - '0');\n }\n }\n return max_number;\n}\n\nstatic bool is_possible(int m, int k, int days) { \n int bouquets = 0;\n int flowers = 0;\n for (int i = 0; i < N; ++i) {\n const int day = input[i];\n if (day <= days) {\n flowers++;\n if (flowers == k) {\n bouquets++;\n flowers = 0;\n }\n } else {\n flowers = 0;\n }\n }\n return bouquets >= m;\n}\n\nint minDays(int max_number, int m, int k) {\n if (long(k) * m > N) {\n return -1;\n }\n int l = 0;\n int r = max_number;\n while (l < r) {\n int days = l + (r - l) / 2;\n if (is_possible(m, k, days)) {\n r = days;\n } else {\n l = days + 1;\n }\n }\n return l;\n}\n\nstatic bool Solve = [](){\n std::ofstream out(\"user.out\");\n std::string s1;\n std::string s2;\n std::string s3;\n while (std::getline(std::cin, s1) && std::getline(std::cin, s2) && std::getline(std::cin, s3)) {\n const int max_number = parse_string(s1);\n const int m = std::stoi(s2);\n const int k = std::stoi(s3);\n out << minDays(max_number, m, k) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\nclass Solution {\n bool possible(vector<int>& bloomDay,int day, int m, int k){\n int count = 0;\n int totbk = 0;\n for(int i = 0;i<bloomDay.size();i++){\n if(bloomDay[i]<=day){\n count++;\n }\n else{\n totbk += count/k;\n count = 0;\n }\n }\n totbk += count/k;\n if(totbk >= m) return true;\n return false;\n }\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n long long product = (long long)m*k;\n if(bloomDay.size() < product) return -1;\n int left = *min_element(bloomDay.begin(),bloomDay.end());\n int right = *max_element(bloomDay.begin(),bloomDay.end());\n int answer = 0;\n while(left<=right){\n int mid = (left+right)/2;\n if(possible(bloomDay,mid,m,k) == true){\n answer = mid;\n right = mid -1;\n }\n else{\n left = mid +1;\n }\n }\n return answer;\n }\n};", "memory": "11905" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nstd::array<int, 100'000> input;\nsize_t N;\n\nint parse_string(const std::string& s) {\n int max_number = std::numeric_limits<int>::min();\n int number = 0;\n N = 0;\n for (size_t i = 1; i < s.size(); ++i) {\n if (s[i] == ',' || s[i] == ']') {\n input[N] = number;\n N++;\n max_number = std::max(max_number, number);\n number = 0;\n } else {\n number = number * 10 + (s[i] - '0');\n }\n }\n return max_number;\n}\n\nstatic bool is_possible(int m, int k, int days) { \n int bouquets = 0;\n int flowers = 0;\n for (int i = 0; i < N; ++i) {\n const int day = input[i];\n if (day <= days) {\n flowers++;\n if (flowers == k) {\n bouquets++;\n flowers = 0;\n }\n } else {\n flowers = 0;\n }\n }\n return bouquets >= m;\n}\n\nint minDays(int max_number, int m, int k) {\n if (long(k) * m > N) {\n return -1;\n }\n int l = 0;\n int r = max_number;\n while (l < r) {\n int days = l + (r - l) / 2;\n if (is_possible(m, k, days)) {\n r = days;\n } else {\n l = days + 1;\n }\n }\n return l;\n}\n\nstatic bool Solve = [](){\n std::ofstream out(\"user.out\");\n std::string s1;\n std::string s2;\n std::string s3;\n while (std::getline(std::cin, s1) && std::getline(std::cin, s2) && std::getline(std::cin, s3)) {\n const int max_number = parse_string(s1);\n const int m = std::stoi(s2);\n const int k = std::stoi(s3);\n out << minDays(max_number, m, k) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\n\n\n\nclass Solution {\npublic:\n\n bool check(vector<int>& bloomDay, int m, int k,int day)\n {\n int cnt=0,temp=0;\n\n int n=bloomDay.size();\n\n for(int i=0;i<n;i++)\n {\n if(bloomDay[i]>day)\n { \n temp+=cnt/k;\n cnt=0;\n }\n else\n {\n cnt++;\n }\n\n \n }\n temp+=cnt/k;\n\n if(temp>=m)\n return true;\n\n return false;\n }\n\n\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n \n \n int low=bloomDay[0],high=bloomDay[0],mid;\n long long n= bloomDay.size();\n\n\n if((long long)m*k>n)\n return -1;\n \n for(int i=0;i<n;i++)\n {\n low=min(low,bloomDay[i]);\n high=max(high,bloomDay[i]);\n\n }\n \n\n while(low<=high)\n {\n mid=low+(high-low)/2;\n\n if(check(bloomDay,m,k,mid)) \n high=mid-1; \n else\n low=mid+1;\n }\n\n return low;\n }\n};", "memory": "11905" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n long long int p= (double)m*(double)k;\n if(bloomDay.size() < p) {\n return -1;\n }\n \n if(bloomDay.size()==m*k) {return *max_element(bloomDay.begin(),bloomDay.end());}\n int ans=-1,n=m;\n int s=*min_element(bloomDay.begin(),bloomDay.end());\n int e=*max_element(bloomDay.begin(),bloomDay.end());\n int mid=s+(e-s)/2;\n while(s<=e){\n n=m;\n int c=0;\n for(int i=0;i<bloomDay.size();i++){\n if(bloomDay[i]<=mid){\n c++;\n }\n else{\n c=0;\n }\n if(c==k){\n cout<<\"here\\n\";\n n--;\n c=0;\n if(n==0){\n break;\n }\n }\n }\n if(n==0){\n ans=mid;\n e=mid-1;\n }\n else{\n s=mid+1;\n }\n mid=s+(e-s)/2;\n }\n return ans;\n }\n};", "memory": "13316" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool pickFlowers(vector<int>& v, int day, int m, int k) {\n int cnt = 0;\n int NofB = 0;\n int n = v.size();\n // find total hours:\n for (int i = 0; i < n; i++) {\n if (v[i] <= day) {\n cnt++;\n } else {\n NofB += cnt / k;\n cnt = 0;\n }\n }\n NofB += cnt / k;\n return NofB >= m;\n }\n int minDays(vector<int>& v, int m, int k) {\n int maxi = INT_MIN;\n int mini = INT_MAX;\n int n = v.size();\n // find the maximum:\n for (int i = 0; i < n; i++) {\n maxi = max(maxi, v[i]);\n mini = min(mini, v[i]);\n }\n long long val = m * 1ll * k * 1ll;\n if (n<val) return -1;\n int low = mini, high =maxi;\n while(low <= high){\n int mid = (low+high)/2;\n if(pickFlowers(v, mid, m,k))\n high = mid-1;\n else\n low = mid+1;\n }\n return low;\n }\n};", "memory": "14728" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\n\n\nprivate:\n bool canMakeBouquets(vector<int>& bloomDay, int m, int k, int day) {\n int total = 0;\n for (int i = 0; i < bloomDay.size(); i++) {\n int count = 0;\n while (i < bloomDay.size() && count < k && bloomDay[i] <= day) {\n count++;\n i++;\n }\n\n if (count == k) {\n total++;\n i--;\n }\n\n if (total >= m) {\n return true;\n }\n }\n\n return false;\n }\n public:\n int minDays(vector<int>& bloomDay, int m, int k) {\n if ((long long)m * k > bloomDay.size()) {\n return -1;\n }\n\n int low = 1, high = 1e9;\n while (low < high) {\n int mid = low + (high - low) / 2;\n\n if (canMakeBouquets(bloomDay, m, k, mid)) {\n high = mid;\n } else {\n low = mid + 1;\n }\n }\n\n return low;\n }\n};", "memory": "14728" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n if((long long)m*k > bloomDay.size()) return -1;\n\n int l = 0;\n int r = 0;\n\n for(auto i: bloomDay) r = max(r, i);\n\n int end = r;\n\n int mid = 0;\n int res = -1;\n //sort(bloomDay.begin(), bloomDay.end());\n\n while(l <= r){\n mid = l + (r-l)/2;\n int count = 0;\n \n int kcheck = 0;\n if(bloomDay[0]<=mid) kcheck++;\n if(kcheck == k) count++;\n\n for(int i=1; i<bloomDay.size(); i++){\n if(bloomDay[i] <= mid) {\n kcheck++;\n if(kcheck == k) {\n count++;\n kcheck=0;\n }\n if(kcheck > k){\n kcheck = 1;\n if(kcheck == k) {\n count++;\n kcheck=0;\n }\n }\n }else{\n kcheck = 0;\n }\n }\n // if(count*k == m*k) return mid;\n if(count<m){\n l = mid+1;\n }else{\n res = mid;\n r = mid-1;\n }\n\n }\n\n return res;\n }\n};", "memory": "16139" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int check(vector<int>&arr,int m,int k,int mid){\n int buq=0,flower=0;\n ;\n for(auto i:arr){\n if(i<=mid){\n flower++;\n }\n else{\n flower=0;\n }\n if(flower==k){\n buq++;\n flower=0;\n }\n\n }\n if(buq>=m)return 1;\n else return 0;\n\n\n }\n\n int minDays(vector<int>& arr, int m, int k) {\n int l=arr[0],e=arr[0];\n \n for(auto i:arr){\n e=max(i,e);\n l=min(i,l);\n }\n int ans=-1;\n while(l<=e){\n int mid=l+(e-l)/2;\n if(check(arr,m,k,mid)==1){\n ans=mid;\n e=mid-1;\n\n }\n else l=mid+1;\n }\n return ans;\n }\n};", "memory": "16139" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int numToMake, int k) {\n int l = 0, r = maxDay(bloomDay), res = -1;\n while(l<=r) {\n int m = (l+r)/2;\n int numBouquets = makeBouquets(bloomDay, k, m);\n if (numBouquets >= numToMake) {\n res = m;\n r = m-1;\n }\n else l = m + 1;\n }\n return res;\n }\n\n int makeBouquets(vector<int>& bloomDay, int k, int d) {\n int count = 0;\n for (int i = 0; i < bloomDay.size(); i++) {\n bool check = true;\n if (bloomDay[i] <= d) {\n if (i+k > bloomDay.size()) continue;\n for(int j = i+1; j < i+k; j++) {\n if (bloomDay[j] > d) {\n check = false;\n break;\n }\n }\n if (check) {\n count++;\n i+=k-1;\n }\n }\n }\n return count;\n }\n\n int maxDay(vector<int>& bloomDay) {\n int res = 0;\n for (auto &e: bloomDay) {\n if (res < e) res = e;\n }\n return res;\n }\n};", "memory": "17550" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int numToMake, int k) {\n int l = 0, r = maxDay(bloomDay), res = -1;\n while(l<=r) {\n int m = (l+r)/2;\n int numBouquets = makeBouquets(bloomDay, k, m);\n if (numBouquets >= numToMake) {\n res = m;\n r = m-1;\n }\n else l = m + 1;\n }\n return res;\n }\n\n int makeBouquets(vector<int>& bloomDay, int k, int d) {\n int res = 0, cnt = 0;\n for (int i = 0; i < bloomDay.size(); i++) {\n if (bloomDay[i] <= d) cnt++;\n else cnt = 0;\n if (cnt == k) {\n cnt = 0;\n res++;\n }\n \n }\n return res;\n }\n\n int maxDay(vector<int>& bloomDay) {\n int res = 0;\n for (auto &e: bloomDay) {\n if (res < e) res = e;\n }\n return res;\n }\n};", "memory": "17550" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int l = 0, r = *max_element(bloomDay.begin(), bloomDay.end()) + 1, n = bloomDay.size();\n if ((long long)n < (long long)m * k) return -1;\n while (l + 1 < r) {\n int mid = l + (r - l) / 2;\n int con = 0, f = 0;\n for (auto i : bloomDay) {\n if (i <= mid) {\n if (++con >= k) {\n con -= k;\n f++;\n }\n } else {\n con = 0;\n }\n if (f >= m) break;\n }\n if (f >= m) r = mid;\n else l = mid;\n }\n return r;\n }\n};", "memory": "18961" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int l = 0, r = *max_element(bloomDay.begin(), bloomDay.end()) + 1, n = bloomDay.size();\n if ((long long)n < (long long)m * k) return -1;\n while (l + 1 < r) {\n int mid = l + (r - l) / 2;\n int con = 0, f = 0;\n for (auto i : bloomDay) {\n if (i <= mid) {\n if (++con >= k) {\n con -= k;\n f++;\n }\n } else {\n con = 0;\n }\n if (f >= m) break;\n }\n if (f >= m) r = mid;\n else l = mid;\n }\n return r;\n }\n};", "memory": "18961" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "#include<bits/stdc++.h>\n#define ll long long\n\nclass Solution {\npublic:\n pair<ll, ll> find_Minimum_And_Maximum_Element(vector<int>& arr) {\n ll minimum_element = INT_MAX, maximum_element = INT_MIN;\n for(int i = 0; i < arr.size(); i++) {\n if(arr[i] < minimum_element) {\n minimum_element = arr[i];\n }\n if(arr[i] > maximum_element) {\n maximum_element = arr[i];\n }\n }\n return { minimum_element, maximum_element };\n }\n bool isPossible(vector<int>& arr, int day, int m, int k) {\n ll flowers = 0, cnt = 0;\n\n for(int i = 0; i < arr.size(); i++) {\n if(arr[i] <= day) cnt++;\n else {\n flowers += (cnt/k);\n cnt = 0; \n }\n }\n flowers += (cnt/k);\n\n if(flowers >= m) return true;\n return false;\n }\n int minDays(vector<int>& arr, int m, int k) {\n ll mm = m, kk = k;\n ll product = mm * kk;\n ios::sync_with_stdio(false);\n\t cin.tie(nullptr);\n\t cout.tie(nullptr);\n if(arr.size() < product) return -1;\n pair<ll, ll> maxmin = find_Minimum_And_Maximum_Element(arr);\n ll low = maxmin.first, high = maxmin.second, ans = -1;\n while(low <= high) {\n ll mid = low + (high - low)/2;\n bool isConditionSatisfied = isPossible(arr, mid, m, k);\n if(isConditionSatisfied) {\n ans = mid;\n high = mid - 1;\n }\n else {\n low = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "20373" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\n bool check(vector<int>& bloomDay, int m, int k, int n, int mid) {\n int count = 0;\n int countBouq = 0;\n for (int i = 0; i < n; i++) {\n\n if (bloomDay[i] <= mid) {\n count++;\n } else {\n countBouq += (count / k);\n count = 0;\n }\n }\n countBouq += count / k;\n if (countBouq >= m) {\n return true;\n }\n return false;\n }\n\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n\n int n = bloomDay.size();\n int maxi = INT_MIN;\n int mini = INT_MAX;\n for (int i = 0; i < n; i++) {\n maxi = max(maxi, bloomDay[i]);\n mini = min(mini, bloomDay[i]);\n }\n vector<bool> bloom(n, 0);\n int ans = -1;\n int i = mini;\n int j = maxi;\n int mid, count = 0;\n while (i <= j) {\n mid = i + (j - i) / 2;\n if (check(bloomDay, m, k, n, mid)) {\n ans = mid;\n j = mid - 1;\n } else {\n i = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "21784" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& a, int m, int k) {\n \n int n=a.size();\n if(1LL*m*k>n) return -1;\n int l=1,r=*max_element(a.begin(),a.end());\n \n int ans=*max_element(a.begin(),a.end());;\n while(l<=r){\n int mid=(l+r)/2;\n int b[n];\n for(int i=0;i<n;i++){\n if(a[i]<=mid){\n b[i]=1;\n }\n else{\n b[i]=0;\n }\n }\n int sum=0, count=0;\n for(int i=0;i<n;i++){\n if(b[i]==0){\n sum=0;\n }\n else if(sum+b[i]<k){\n sum++;\n }\n else{\n count++;\n sum=0;\n }\n }\n //if(sum!=0) count++;\n if(count>=m){\n ans=mid;\n r=mid-1;\n }\n else{\n l=mid+1;\n }\n }\n return ans;\n }\n};", "memory": "23195" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "typedef long long ll;\nclass Solution {\n\n\n\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n \n ll low = 1;\n ll high =1e9;\n ll ans=-1;\n ll mid;\n ll n = bloomDay.size();\n while(low<=high) {\n mid = low + (high-low)/2;\n ll cnt=0;\n ll a[n];\n memset(a,0,sizeof(a));\n for(int i=0;i<n;i++) {\n if(bloomDay[i]<=mid) {\n if(i) {\n a[i]=a[i-1]+1;\n } else {\n a[i]++;\n }\n if(a[i]==k){\n a[i]=0;\n cnt++;\n }\n }\n\n }\n\n if(cnt>=m) {\n ans=mid;\n high = mid-1;\n } else {\n low = mid+1;\n }\n }\n\n return ans;\n }\n};", "memory": "28840" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n if(m>(n/k))return -1;\n \n int lo = *min_element(bloomDay.begin(),bloomDay.end());\n int hi = *max_element(bloomDay.begin(),bloomDay.end());\n\n int ans = INT_MAX;\n\n while(lo<=hi){\n int mid = lo+(hi-lo)/2;\n\n // Set Counter\n vector<bool>counter(n,false);\n for(int i=0;i<n;i++){\n if(bloomDay[i]<=mid)counter[i]=true;\n }\n\n // Count bouquets\n int flowers = 0;\n int bouquets = 0;\n\n for(int i=0;i<n;i++){\n if(counter[i]){\n flowers++;\n }else{\n flowers = 0;\n }\n\n if(flowers==k){\n bouquets++;\n flowers = 0;\n }\n }\n\n // Adjust\n if(bouquets>=m){\n ans = min(ans, mid);\n hi = mid-1;\n }else{\n lo = mid+1;\n }\n }\n\n return ans;\n }\n};", "memory": "30251" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(int mid,vector<int>&v,int m,int k,vector<int>&t){\n t.clear();\n int n=v.size();\n int count=0;\n for(auto i:v){\n if(i<=mid){\n count++;\n }\n else{\n if(count)t.push_back(count);\n count=0;\n }\n }\n if(count)t.push_back(count);\n for(auto i:t)cout<<i<<\" \";\n int ans=0;\n for(auto i:t){\n ans+=i/k;\n }\n if(ans>=m)return true;\n return false;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n long long n=bloomDay.size();\n long long z=1LL*m*k;\n if(n<z)return -1;\n //sort(bloomDay.begin(),bloomDay.end());\n vector<int>t;\n int l=1,r=*max_element(bloomDay.begin(),bloomDay.end());\n while(l<=r){\n int mid=(l+r)>>1;\n if(check(mid,bloomDay,m,k,t)){\n //break;\n cout<<mid<<endl;\n r=mid-1;\n }\n else{\n //break;\n l=mid+1;\n }\n }\n return l;\n }\n};", "memory": "31663" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool bouquet(vector<int> &bloomDay,int m,int k,long long int mid){\n vector<bool> result(bloomDay.size());\n int count = 0;\n for(int i=0;i<bloomDay.size();i++){\n if(bloomDay[i]<=mid)\n result[i] = true;\n }\n for(int i=0;i<result.size();i++){\n if(result[i]){\n count++;\n if(count==k){\n m--;\n count=0;\n }\n if(m==0)\n break;\n }\n else\n count = 0;\n }\n if(m==0)\n return true;\n else\n return false;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int ans;\n if(double(m)*double(k) > bloomDay.size())\n return -1;\n int s = *min_element(bloomDay.begin(),bloomDay.end());\n cout<<s<<endl;\n int e = *max_element(bloomDay.begin(),bloomDay.end());\n cout<<e<<endl;\n while(s<=e){\n int mid = (s+(e-s)/2);\n if(bouquet(bloomDay,m,k,mid)){\n ans = mid;\n e = mid-1;\n }\n else\n s = mid+1;\n }\n return ans;\n }\n};", "memory": "33074" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\n bool check(vector<int>& bloomDay, int k, int m, int day){\n int chk[bloomDay.size()], cnt=0;\n for(int i=0; i<bloomDay.size(); i++){\n if(day>=bloomDay[i]){\n chk[i]=1; cnt++;\n } \n else chk[i]=0;\n }\n\n if(cnt<k*m) return 0;\n else{\n cnt=m*k;\n int temp=0;\n for(int i=0; i<bloomDay.size(); i++){\n if(chk[i]==1 && i==bloomDay.size()-1){\n temp++;\n if(temp%k==0) cnt-=temp;\n else cnt-=((temp/k)*k);\n temp=0;\n } \n else if(chk[i]==1 && i<bloomDay.size()-1) temp++;\n else{\n if(temp%k==0) cnt-=temp;\n else cnt-=((temp/k)*k);\n temp=0;\n }\n }\n if(cnt<=0) return 1;\n else return 0;\n } \n }\n\npublic:\n int minDays(vector<int>& bloomDay, long long m, long long k) {\n long long prod=k*m;\n if(bloomDay.size()<prod) return -1;\n\n int bloom[bloomDay.size()];\n for(int i=0; i<bloomDay.size(); i++) bloom[i]=bloomDay[i];\n sort(bloom,bloom+bloomDay.size());\n \n int low=0, high=bloomDay.size()-1, mid, fin=0;\n bool ans;\n while(low<=high){\n mid=(high+low)/2; // ind\n ans=check(bloomDay, k, m, bloom[mid]);\n if(ans==1){\n fin=mid;\n high=mid-1;\n } \n else low=mid+1;\n }\n // 1 2 3 4 5 6 7 8 9 10\n return bloom[fin];\n }\n};", "memory": "34485" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(vector<int>& nums, long long day, int k, int m, int n){\n vector<bool> arr(n);\n for(int i = 0 ; i < n; i++){\n if(day >= nums[i]) arr[i] = true;\n }\n int temp_k = k;\n for(int i = 0 ; i < n ; i ++){\n if(arr[i]) temp_k--;\n else temp_k = k;\n if(temp_k == 0) {\n m--;\n if(m == 0) return true;\n temp_k = k;\n }\n }\n return m<=0;\n \n}\n int minDays(vector<int>& nums, int m, int k) {\n int n = nums.size();\n long long low = *min_element(nums.begin(), nums.end());\n long long high = *max_element(nums.begin(), nums.end());\n long long ans = -1;\n\n while(low <= high){\n long long mid = low + (high - low) /2;\n\n if(check(nums, mid, k, m, n)) {\n high = mid - 1;\n ans = mid;\n }\n\n else low = mid + 1;\n\n }\n return ans;\n }\n};", "memory": "35896" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canhavebouquets(vector<int>& bloomday,int day,int m,int k){\n vector<bool> took(bloomday.size(),false);\n int count=0,count1=0;\n for(int i=0;i<bloomday.size();i++){\n if(bloomday[i]<=day && took[i]==false){\n took[i]=true;\n count++;\n if(count==k){\n count1++;\n if(count1==m){\n return true;\n }\n count=0;\n }\n }\n else if(bloomday[i]>day){\n count=0;\n }\n }\n return count1>=m;\n }\n int minDays(vector<int>& bloomday, int m, int k) {\n int result=-1;\n int left=*min_element(bloomday.begin(),bloomday.end());\n int right=*max_element(bloomday.begin(),bloomday.end());\n while(left<=right){\n int mid=left+(right-left)/2;\n if(canhavebouquets(bloomday,mid,m,k)){\n result=mid;\n right=mid-1;\n }\n else{\n left=mid+1;\n }\n }\n return result;\n }\n};", "memory": "37308" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canhavebouquets(vector<int>& bloomday,int day,int m,int k){\n vector<bool> took(bloomday.size(),false);\n int count=0,count1=0;\n for(int i=0;i<bloomday.size();i++){\n if(bloomday[i]<=day && took[i]==false){\n took[i]=true;\n count++;\n if(count==k){\n count1++;\n if(count1==m){\n return true;\n }\n count=0;\n }\n }\n else if(bloomday[i]>day){\n count=0;\n }\n }\n return count1>=m;\n }\n int minDays(vector<int>& bloomday, int m, int k) {\n int result=-1;\n int left=*min_element(bloomday.begin(),bloomday.end());\n int right=*max_element(bloomday.begin(),bloomday.end());\n while(left<=right){\n int mid=left+(right-left)/2;\n if(canhavebouquets(bloomday,mid,m,k)){\n result=mid;\n right=mid-1;\n }\n else{\n left=mid+1;\n }\n }\n return result;\n }\n};", "memory": "37308" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool f(int mid, vector<int>& bloomDay, int m, int k){\n vector<bool>flag(bloomDay.size(),false); int cnt = 0;\n for(int i=0;i<bloomDay.size();i++){\n if(bloomDay[i]<=mid) flag[i] = true;\n }\n for(int i=0;i<flag.size();i++){\n if(flag[i]==true){\n cnt++;\n if(cnt==k){\n m--;\n if(m==0) return true;\n cnt=0;\n }\n }else{\n cnt=0;\n }\n }\n return false;\n } \npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int maxi = bloomDay[0];\n for(int i=1;i<bloomDay.size();i++){\n maxi = max(maxi,bloomDay[i]);\n }\n\n int s = 1; int e = maxi; int ans = INT_MAX;\n while(s<=e){\n int mid = s + (e-s)/2;\n if(f(mid,bloomDay,m,k)){\n ans = min(ans,mid);\n e = mid-1;\n }else{\n s = mid+1;\n }\n }\n if(ans==INT_MAX) return -1;\n return ans;\n }\n};", "memory": "38719" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int ma(vector<int> bloomDay){\n int maxi = INT_MIN;\n for(int i = 0;i < bloomDay.size();i++){\n maxi = max(maxi,bloomDay[i]);\n }\n return maxi;\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size();\n if(n / k < m) return -1;\n int low = 1,high,mid,ans=INT_MAX;\n high = ma(bloomDay);\n while(low <= high){\n mid = (low + high)/2;\n int x = 0,adj = 0;\n for(int i = 0;i < n;i++){\n if(bloomDay[i] <= mid){\n x++;\n }\n else{\n x = 0;\n }\n if(x == k){\n adj++;\n x = 0;\n }\n }\n\n if(adj >= m){\n ans = min(mid,ans);\n high = mid - 1;\n\n }\n else{\n low = mid + 1;\n }\n }\n if(ans == INT_MAX){\n return -1;\n }\n return ans;\n }\n};", "memory": "40130" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fun(vector<int>& arr,vector<int>& a,int m,int k,int mid){\n int cn=0;\n int val=0;\n for(int i=0;i<a.size();i++){\n if(a[i]<=mid){\n arr[i]=1;\n }\n else{\n arr[i]=0;\n }\n\n }\n for(int i=0;i<arr.size();i++){\n if(arr[i]==1){\n cn++;\n }\n else{\n cn=0;\n }\n if(cn==k){\n // cout<<cn<<\" \";\n val++;\n cn=0;\n }\n }\n return val;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int maxi=0;\n int s=bloomDay.size();\n if((long long)m*k>s){\n return -1;\n }\n // vector<int> ans;\n vector<int> vec(s,0);\n // ans=bloomDay;\n // sort(ans.begin(),ans.end());\n for(int i=0;i<s;i++){\n maxi=max(maxi,bloomDay[i]);\n }\n int low=1,high=maxi;\n // for(int j=0;j<s;j++){\n // if(ans[i]==bloomDay[j]){\n // vec[j]=1;\n // }\n // }\n // if(fun(vec,m,k)==1){\n // return ans[i];\n // }\n int ans=INT_MAX;\n while(low<=high){\n int mid=(low+high)/2;\n int call=fun(vec,bloomDay,m,k,mid);\n if(call>=m){\n ans=mid;\n high=mid-1;\n }\n else{\n low=mid+1;\n }\n }\n return ans;\n // int tc=0;\n // for(int i=0;i<s;i++){\n // if(bloomDay[i]<=ans){\n // tc=max(tc,bloomDay[i]);\n // }\n // }\n // return tc;\n }\n};", "memory": "41541" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int >& b, int min, int k) {\n long long l = 0, r = 1e9 + 1, n = b.size();\n if (n < (long long )min * k)\n return -1;\n while (r - l > 1) {\n long long m = l + (r - l) / 2;\n vector<bool> bloomed(n);\n for (long long i = 0; i < n; i++) {\n if (b[i] <= m)\n bloomed[i] = true;\n else\n bloomed[i] = false;\n }\n long long bouquets = 0;\n for (long long i = 0; i < n; i++) {\n if (bloomed[i] == true) {\n long long cnt = 0;\n for (long long st = i; st < n; st++) {\n i = st;\n if (bloomed[st])\n cnt++;\n else\n break;\n }\n bouquets += cnt / k;\n }\n }\n if (bouquets >= min) {\n r = m;\n } else {\n l = m;\n }\n cout << l << \" \" << r << \" \" << bouquets << endl;\n }\n return r;\n }\n};", "memory": "42953" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxi(vector<int> nums) {\n int large = 0;\n for(int i=0; i<nums.size(); i++) {\n if(nums[i] > large) \n large = nums[i];\n }\n return large;\n }\n\n int find(vector<int>& nums, int mid, int k) {\n int cnt = 0;\n int bouquets = 0;\n\n for (int num : nums) {\n if (num <= mid) {\n cnt++;\n if (cnt == k) {\n bouquets++;\n cnt = 0;\n }\n } else {\n cnt = 0;\n }\n }\n\n return bouquets;\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n long long n = (long long)m*(long long)k;\n if(n > bloomDay.size()) \n return -1;\n\n int low = 1, high = maxi(bloomDay), ans =0;\n\n while(low <= high) {\n int mid = low + (high - low) / 2;\n int buckets = find(bloomDay,mid,k);\n\n if(buckets >= m) {\n ans = mid;\n high = mid-1;\n }\n else {\n low = mid+1;\n }\n }return ans;\n }\n};", "memory": "44364" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMax(vector<int> v) {\n int maxi = INT_MIN;\n for (int i = 0; i < v.size(); i++) {\n maxi = max(maxi, v[i]);\n }\n return maxi;\n }\n\n int minDays(vector<int>& arr, int m, int k) {\n int n = arr.size();\n if ((long long)m*k > n)\n return -1;\n\n long long low = 0, high = findMax(arr);\n long long ans = -1;\n\n while (low <= high) {\n int count = 0, totalCount = 0;\n long mid = (low + high) / 2;\n\n for (int i = 0; i < n; i++) {\n if (mid >= arr[i]) {\n count++;\n if (count == k) {\n totalCount++;\n count = 0;\n }\n } else {\n count = 0;\n }\n }\n\n if (totalCount >= m) {\n ans = mid;\n high = mid - 1;\n } else {\n low = mid + 1;\n }\n }\n\n return ans;\n }\n};", "memory": "45775" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "bool possible(vector <int> &bloomDay,int day,int m,int k){\n int count =0;\n int noOfB=0;\n for(int i=0;i<bloomDay.size();i++){\n if(bloomDay[i]<=day){\n count++;\n }\n else{\n noOfB+=count/k;\n count=0;\n }\n }\n noOfB+=count/k;\n return noOfB>=m;\n}\n\nclass Solution {\npublic:\n int minDays(vector<int> bloomDay, int r, int b) {\n long long val=r*1LL*b*1LL;\n if(val>bloomDay.size()){\n return -1;\n }\n int mini=INT_MAX;\n int maxi=INT_MIN;\n for(int i=0;i<bloomDay.size();i++){\n mini=min(mini,bloomDay[i]);\n maxi=max(maxi,bloomDay[i]);\n }\n int low=mini;\n int high=maxi;\n while(low<=high){\n int mid=(low+high)/2;\n if(possible(bloomDay,mid,r,b)){\n high=mid-1;\n }\n else{\n low=mid+1;\n }\n }\n return low;\n }\n};", "memory": "47186" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // int minDays(vector<int>& bloomDay, int m, int k) {\n \n // }\n bool possible(vector<int> &arr, int day, int m, int k) {\n int n = arr.size(); //size of the array\n int cnt = 0;\n int noOfB = 0;\n // count the number of bouquets:\n for (int i = 0; i < n; i++) {\n if (arr[i] <= day) {\n cnt++;\n }\n else {\n noOfB += (cnt / k);\n cnt = 0;\n }\n }\n noOfB += (cnt / k);\n return noOfB >= m;\n}\nint minDays(vector<int> arr, int m, int k) {\n long long val = m * 1ll * k * 1ll;\n int n = arr.size(); //size of the array\n if (val > n) return -1; //impossible case.\n //find maximum and minimum:\n int mini = INT_MAX, maxi = INT_MIN;\n for (int i = 0; i < n; i++) {\n mini = min(mini, arr[i]);\n maxi = max(maxi, arr[i]);\n }\n\n //apply binary search:\n int low = mini, high = maxi;\n while (low <= high) {\n int mid = (low + high) / 2;\n if (possible(arr, mid, m, k)) {\n high = mid - 1;\n }\n else low = mid + 1;\n }\n return low;\n}\n};", "memory": "48598" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "#include<bits/stdc++.h>\nclass Solution {\npublic:\n int minDays(vector<int>& a, int m, int k) {\n int l = 0, r= 1e9, ans=-1;\n map<int, int> s;\n while(l<=r){\n s.clear();\n int mid = (l+r) / 2;\n int ct = 0,tmp=0;\n for(auto i:a){\n if(i<=mid) ++ct;\n else {\n s[-ct]++;\n ct = 0;\n }\n }\n if(ct>0) s[-ct]++;\n for(auto i: s){\n tmp += ((-i.first) / k)*(i.second);\n }\n // for(auto i: s) cout<<i<<\" \";\n // cout<<endl<<tmp<<endl;\n if(tmp>=m){\n ans = mid;\n r= mid-1;\n } \n else l= mid+1;\n } \n return ans;\n }\n};", "memory": "50009" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool good(int mm,vector<int>& b,int k,int m){\n int boucoup=0,adj=0;\n int n=b.size();\n vector<bool> flowers(n+1,true);\n for(int i=0;i<n;i++){\n if(mm/b[i]>=1 && flowers[i]) adj++;\n else adj=0;\n if(adj>=k) {\n boucoup++;\n for(int j=i;j>=0;j--){\n if(adj--) break;\n flowers[i]=false;\n }\n adj=0;\n }\n }\n // cout<<boucoup<<\" \";\n if(boucoup>=m) return true;\n else return false;\n }\n int minDays(vector<int>& b, int m, int k) {\n int n=b.size();\n if(1LL*m*1LL*k>1LL*n) return -1;\n int l=0,r=1e9;\n while(l+1<r){\n int mm=(l+r)/2;\n if(good(mm,b,k,m)){\n r=mm;\n }\n else{\n l=mm;\n }\n cout<<mm<<\" \";\n }\n return r;\n }\n};", "memory": "51420" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(vector<int>& bloomDay, int m, int k, int d){\n int t=0;\n int n=bloomDay.size();\n for(int i=0; i<n; i++){\n int count=0;\n while(i<n && count<k && bloomDay[i]<=d){\n count++;\n i++;\n }\n if(count==k){\n t++;\n i--;\n }\n if(t>=m){\n return true;\n }\n }\n return false;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n=bloomDay.size();\n if((long long)k*m>n){\n return -1;\n }\n vector<int>temp=bloomDay;\n sort(temp.begin(), temp.end());\n int l=temp[0], h=temp[n-1], ans;\n while(l<=h){\n int mid=(l+h)/2;\n if(solve(bloomDay, m, k, mid)){\n h=mid-1;\n }\n else{\n l=mid+1;\n }\n }\n return l;\n }\n};", "memory": "52831" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\n bool check(vector<int>&a ,int m ,int k,int day){\n int cnt=0;\n for(int i=0;i<a.size();i++){\n if(a[i]<=day) cnt++;\n else cnt=0;\n if(cnt==k) {m--; cnt=0;}\n if(m==0) return true;\n }\n return false;\n }\npublic:\n int minDays(vector<int>& a, int m, int k) {\nint n=a.size();\n if(1LL*m*k>a.size()*1LL) return -1;\n vector<int>v=a;\n sort(v.begin(),v.end());\n int low=0;\n int high=n-1;\n int ans;\n while(low<=high){\n int mid=(low+high)/2;\n if(!check(a,m,k,v[mid])) low=mid+1;\n else {\n ans=v[mid];\n high=mid-1;\n }\n }\nreturn ans;\n }\n};", "memory": "54243" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n bool isPossible(vector<int>& bloomDay, int m, int k,int mid){\n int n = bloomDay.size();\n int flowers = 0;\n int boquets = 0;\n vector<bool> v(n,0);\n for(int i = 0;i<n;i++){\n if(bloomDay[i]<=mid){\n v[i] = 1;\n }\n }\n for(int i = 0;i<n;i++){\n if(v[i] == 1){\n flowers++;\n if(flowers%k == 0){\n boquets++;\n flowers = 0;\n }\n }\n if(v[i] == 0){\n flowers = 0;\n }\n }\n if(boquets>=m){\n return 1;\n }\n else{\n return 0;\n }\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int s = 0;\n int e = 1e9;\n int mid = 0;\n int ans = -1;\n while(s<=e){\n mid = (s+e)/2;\n if(isPossible(bloomDay,m,k,mid)){\n ans = mid;\n e = mid - 1;\n }\n else{\n s = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "55654" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n bool isPossible(vector<int>& bloomDay, int m, int k,int mid){\n int n = bloomDay.size();\n int flowers = 0;\n int boquets = 0;\n vector<bool> v(n,0);\n for(int i = 0;i<n;i++){\n if(bloomDay[i]<=mid){\n v[i] = 1;\n }\n }\n for(int i = 0;i<n;i++){\n if(v[i] == 1){\n flowers++;\n if(flowers%k == 0){\n boquets++;\n flowers = 0;\n }\n }\n if(v[i] == 0){\n flowers = 0;\n }\n }\n if(boquets>=m){\n return 1;\n }\n else{\n return 0;\n }\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int s = 0;\n int e = 1e9;\n int mid = 0;\n int ans = -1;\n while(s<=e){\n mid = (s+e)/2;\n if(isPossible(bloomDay,m,k,mid)){\n ans = mid;\n e = mid - 1;\n }\n else{\n s = mid + 1;\n }\n }\n return ans;\n }\n};", "memory": "55654" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n if (bloomDay.size() < static_cast<long long>(m) * static_cast<long long>(k)) return -1;\n\n int max_day = 0;\n for (int i = 0; i < bloomDay.size(); i++) max_day = max(max_day, bloomDay[i]);\n\n vector<int> sorted_bloomDay = bloomDay;\n sort(sorted_bloomDay.begin(), sorted_bloomDay.end());\n sorted_bloomDay.erase(unique(sorted_bloomDay.begin(), sorted_bloomDay.end()), sorted_bloomDay.end());\n\n int left = 0, right = sorted_bloomDay.size();\n while (left < right) {\n int mid = left + (right - left) / 2;\n if (make_bouquets(sorted_bloomDay[mid], bloomDay, k) < m) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n\n return sorted_bloomDay[left];\n }\n\nprivate:\n int make_bouquets(int day, vector<int>& bloomDay, int k) {\n int bouquet = 0;\n int adjacent_flower = 0;\n for (int i = 0; i < bloomDay.size(); i++) {\n if (day >= bloomDay[i]) {\n adjacent_flower++;\n\n if (adjacent_flower == k) {\n bouquet++;\n adjacent_flower = 0;\n }\n } else {\n adjacent_flower = 0;\n }\n }\n\n return bouquet;\n }\n};", "memory": "57065" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int checkPossible(vector<int>& bloomDay,int day,int k){\n int count = 0;\n int ans = 0;\n for(int i : bloomDay){\n if(i<=day) count++;\n else count = 0;\n if(count == k) ans++,count=0;\n }\n return ans;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n vector<int> sortedBloomDay = bloomDay;\n sort(sortedBloomDay.begin(),sortedBloomDay.end());\n int n = sortedBloomDay.size();\n int low = 0;\n int high = sortedBloomDay.size()-1;\n while(low <= high){\n int mid = low + (high - low)/2;\n if(checkPossible(bloomDay,sortedBloomDay[mid],k) >= m){\n high = mid-1;\n }\n else{\n low = mid+1;\n }\n }\n return low == n ? -1 : sortedBloomDay[low];\n }\n};", "memory": "58476" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nbool solve(vector<int>& bloom, int m, int k,int mid){\n int x=0;\n int y=0;\n for(int i=0; i<bloom.size(); i++){\n if(mid>=bloom[i]){\n y++;\n if(y==k){\n y=0;\n x++;\n }\n if(x==m){\n return true;\n }\n }\n else{\n y=0;\n }\n }\n return false;\n}\n int minDays(vector<int>& bloom, int m, int k) {\n int n=bloom.size();\n vector<int>v(n);\n v=bloom;\n if(n/k<m){\n return -1;\n }\n if(n==1){\n return bloom[0];\n }\n sort(v.begin(),v.end());\n int high=v[n-1];\n int low=v[m*k-1];\n int ans=-1;\n int mid;\n while(low<=high){\n mid=(low+high)/2;\n if(solve(bloom,m,k,mid)){\n ans=mid;\n high=mid-1;\n }\n else{\n low=mid+1;\n }\n }\n return ans;\n }\n};", "memory": "59888" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int checkPossible(vector<int>& bloomDay,int day,int k){\n int count = 0;\n int ans = 0;\n for(int i : bloomDay){\n if(i<=day) count++;\n else count = 0;\n if(count == k) ans++,count=0;\n }\n return ans;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n vector<int> sortedBloomDay = bloomDay;\n sort(sortedBloomDay.begin(),sortedBloomDay.end());\n int n = sortedBloomDay.size();\n int low = 0;\n int high = sortedBloomDay.size()-1;\n while(low <= high){\n int mid = low + (high - low)/2;\n if(checkPossible(bloomDay,sortedBloomDay[mid],k) >= m){\n high = mid-1;\n }\n else{\n low = mid+1;\n }\n }\n return low == n ? -1 : sortedBloomDay[low];\n }\n};", "memory": "59888" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n// bd = bloomDay\n int minDays(vector<int>& bloomDay, int m, int k) {\n // set<int> st;\n // for(int x:bd){\n // st.insert(x);\n // }\n vector<int> bd;\n bd=bloomDay;\n \n sort(bloomDay.begin(),bloomDay.end());\n\n int n=bloomDay.size();\n int start=bloomDay[0];\n int end=bloomDay[n-1];\n int ans=INT_MAX;\n\n while(start<=end){\n int mid=start + (end-start)/2;\n int x=mid;\n int temp_k=0;\n int temp_m=0;\n for(int i=0;i<bd.size();i++){\n if(bd[i]<=x){\n temp_k++;\n }\n else{\n temp_k=0;\n }\n if(temp_k==k){\n temp_k=0;\n temp_m++;\n if(temp_m==m){\n ans=min(ans,x);\n break;\n }\n }\n }\n \n if(temp_m<m){\n start=mid+1;\n }\n else{\n end=mid-1;\n }\n }\n return (ans == INT_MAX) ? -1 : ans;\n }\n};", "memory": "61299" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloo, int m, int k) {\n if((long long)m * (long long)k > bloo.size()) return -1;\n int mi = INT_MAX;\n int ma = 0;\n\n for(long i = 0 ; i < bloo.size() ; i++){\n ma = max(ma,bloo[i]);\n mi = min(mi,bloo[i]);\n }\n long l = mi;\n long r = ma;\n long mid;\n\n vector<long> copy(bloo.size());\n\n while(l <= r){\n mid = l +(r-l)/2;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(bloo[i] <= mid) copy[i] = 1; // 1 means bloomed\n else copy[i] = 0; // not bloomed yet \n }\n int count = 0;\n int bouq = 0;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(copy[i] == 1){\n count++;\n if(count % k == 0) {\n bouq++;\n count =0;\n }\n }\n if(copy[i] == 0) count = 0;\n }\n if(bouq >= m){\n r = mid-1;\n }\n else if(bouq < m){\n l = mid+1;\n }\n }\n return l;\n }\n};", "memory": "62710" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloo, int m, int k) {\n if((long long)m * (long long)k > bloo.size()) return -1;\n int mi = INT_MAX;\n int ma = 0;\n\n for(long i = 0 ; i < bloo.size() ; i++){\n ma = max(ma,bloo[i]);\n mi = min(mi,bloo[i]);\n }\n long l = mi;\n long r = ma;\n long mid;\n\n vector<long> copy(bloo.size());\n\n while(l <= r){\n mid = l +(r-l)/2;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(bloo[i] <= mid) copy[i] = 1; // 1 means bloomed\n else copy[i] = 0; // not bloomed yet \n }\n int count = 0;\n int bouq = 0;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(copy[i] == 1){\n count++;\n if(count % k == 0) {\n bouq++;\n count =0;\n }\n }\n if(copy[i] == 0) count = 0;\n }\n if(bouq >= m){\n r = mid-1;\n }\n else if(bouq < m){\n l = mid+1;\n }\n }\n return l;\n }\n};", "memory": "64121" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloo, int m, int k) {\n if((long long)m * (long long)k > bloo.size()) return -1;\n int mi = INT_MAX;\n int ma = 0;\n\n for(long i = 0 ; i < bloo.size() ; i++){\n ma = max(ma,bloo[i]);\n mi = min(mi,bloo[i]);\n }\n long l = mi;\n long r = ma;\n long mid;\n\n vector<long> copy(bloo.size());\n\n while(l <= r){\n mid = l +(r-l)/2;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(bloo[i] <= mid) copy[i] = 1; // 1 means bloomed\n else copy[i] = 0; // not bloomed yet \n }\n int count = 0;\n int bouq = 0;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(copy[i] == 1){\n count++;\n if(count % k == 0) {\n bouq++;\n count =0;\n }\n }\n if(copy[i] == 0) count = 0;\n }\n if(bouq >= m){\n r = mid-1;\n }\n else if(bouq < m){\n l = mid+1;\n }\n }\n return l;\n }\n};", "memory": "64121" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<int, int> maxii(vector<int> a) {\n\n int maxi = INT_MIN;\n int mini = INT_MAX;\n for (int i = 0; i < a.size(); i++) {\n maxi = max(maxi, a[i]);\n mini = min(mini, a[i]);\n }\n return {mini, maxi};\n }\n int boquet(vector<int> a, int m, int k) {\n pair<int, int> result = maxii(a);\n int low = result.first;\n int high = result.second;\n\n while (low <= high)\n {\n\n int mid = (low + high) / 2;\n int cnt = 0;\n int boCount = 0;\n for (int i = 0; i < a.size(); i++)\n {\n if (mid >= a[i])\n {\n cnt++;\n }\n else\n { \n boCount += cnt / k;\n cnt = 0;\n }\n }\n boCount += cnt / k;\n\n if (boCount >= m)\n {\n high = mid - 1;\n }\n else \n {\n low = mid + 1;\n }\n }\n\n return low;\n}\n \n\n int minDays(vector<int>& a, int m, int k) { \n long long tt = double(m)*(k);\n if(a.size()<tt) return -1;\n return boquet(a, m, k); }\n};", "memory": "65533" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloo, int m, int k) {\n if((long long)m * (long long)k > bloo.size()) return -1;\n int mi = INT_MAX;\n int ma = 0;\n\n for(long i = 0 ; i < bloo.size() ; i++){\n ma = max(ma,bloo[i]);\n mi = min(mi,bloo[i]);\n }\n long l = mi;\n long r = ma;\n long mid;\n\n vector<long> copy(bloo.size());\n\n while(l <= r){\n mid = l +(r-l)/2;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(bloo[i] <= mid) copy[i] = 1; // 1 means bloomed\n else copy[i] = 0; // not bloomed yet \n }\n int count = 0;\n int bouq = 0;\n for(int i = 0 ; i < bloo.size() ; i++){\n if(copy[i] == 1){\n count++;\n if(count % k == 0) {\n bouq++;\n count =0;\n }\n }\n if(copy[i] == 0) count = 0;\n }\n if(bouq >= m){\n r = mid-1;\n }\n else if(bouq < m){\n l = mid+1;\n }\n }\n return l;\n }\n};", "memory": "66944" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool possible(vector<int>& arr,int day ,int m, int k){\n int cnt=0;\n int bouq=0;\n for(int i=0;i<=arr.size()-1;i++){\n if(arr[i]<=day) cnt++;\n else{\n bouq+=(cnt/k);\n cnt=0;\n }\n }\n bouq+=(cnt/k);\n if(bouq>=m) return true;\n else return false;\n }\n\n int findMin(vector<int>arr, int n) {\n int minElement = arr[0]; // Assume the first element is the minimum\n\n for (int i = 1; i < n; ++i) {\n if (arr[i] < minElement) {\n minElement = arr[i]; // Update minElement if a smaller element is found\n }\n }\n\n return minElement;\n}\n\nint findMax(vector<int>arr, int n) {\n int maxElement = arr[0]; // Assume the first element is the maximum\n\n for (int i = 1; i < n; ++i) {\n if (arr[i] > maxElement) {\n maxElement = arr[i]; // Update maxElement if a larger element is found\n }\n }\n\n return maxElement;\n}\n int minDays(vector<int>& bloomDay, int m, int k) {\n if(bloomDay.size()<(long long)m*k) return -1;\n int low=findMin(bloomDay,bloomDay.size());\n int high=findMax(bloomDay,bloomDay.size());\n int ans;\n while(low<=high){\n int mid=(low+high)/2;\n if(possible(bloomDay,mid,m,k)==true) {\n ans= bloomDay[mid];\n high=mid-1;\n }\n else low=mid+1;\n }\n return low; \n }\n};", "memory": "68355" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool possible(vector<int>& arr,int day ,int m, int k){\n int cnt=0;\n int bouq=0;\n for(int i=0;i<=arr.size()-1;i++){\n if(arr[i]<=day) cnt++;\n else{\n bouq+=(cnt/k);\n cnt=0;\n }\n }\n bouq+=(cnt/k);\n if(bouq>=m) return true;\n else return false;\n }\n\n int findMin(vector<int>arr, int n) {\n int minElement = arr[0]; // Assume the first element is the minimum\n\n for (int i = 1; i < n; ++i) {\n if (arr[i] < minElement) {\n minElement = arr[i]; // Update minElement if a smaller element is found\n }\n }\n\n return minElement;\n}\n\nint findMax(vector<int>arr, int n) {\n int maxElement = arr[0]; // Assume the first element is the maximum\n\n for (int i = 1; i < n; ++i) {\n if (arr[i] > maxElement) {\n maxElement = arr[i]; // Update maxElement if a larger element is found\n }\n }\n\n return maxElement;\n}\n int minDays(vector<int>& bloomDay, int m, int k) {\n if(bloomDay.size()<(long long)m*k) return -1;\n int low=findMin(bloomDay,bloomDay.size());\n int high=findMax(bloomDay,bloomDay.size());\n int ans;\n while(low<=high){\n int mid=(low+high)/2;\n if(possible(bloomDay,mid,m,k)==true) {\n ans= bloomDay[mid];\n high=mid-1;\n }\n else low=mid+1;\n }\n return low; \n }\n};", "memory": "68355" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& a, int m, int k) {\n int n = a.size(), ans = -1;\n if (m > n/k) {\n return -1;\n }\n\n int maxi = 0;\n for (int i = 0; i < n; i++) {\n maxi = max(maxi, a[i]);\n }\n\n int l = 1, r = maxi; // Adjusted upper bound to 'maxi'\n while (l <= r) {\n int mid = l + (r - l) / 2;\n vector<int> f; // Clear or initialize the vector here\n int sum = 0;\n\n // Count consecutive days <= mid\n for (int i = 0; i < n; i++) {\n if (a[i] <= mid) {\n sum++;\n } else {\n if (sum != 0) f.push_back(sum);\n sum = 0;\n }\n }\n // Add the last sum if it hasn't been added yet\n if (sum != 0) f.push_back(sum);\n\n int sum1 = 0;\n for (int i = 0; i < f.size(); i++) {\n sum1 += (f[i] / k);\n }\n\n if (sum1 >= m) {\n ans = mid;\n r = mid - 1;\n } else {\n l = mid + 1;\n }\n }\n\n return ans;\n }\n};\n", "memory": "69766" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxi(vector<int>arr){\n int ans = INT_MIN;\n for (int i = 0; i < arr.size(); i++)\n {\n ans = max(ans, arr[i]);\n }\n return ans; \n }\n\n int mini(vector<int>arr){\n int ans = INT_MAX;\n for (int i = 0; i < arr.size(); i++)\n {\n ans = min(ans, arr[i]);\n }\n return ans; \n }\n\n int bouqetCount(vector<int>& bloomDay, int m, int k, int day){\n int cnt = 0;\n int cnt2 = 0;\n for(int i=0; i<bloomDay.size(); i++){\n if(bloomDay[i]<=day) cnt++;\n else{\n cnt2 += (cnt/k);\n cnt=0;\n }\n }\n cnt2 += (cnt/k);\n return cnt2 >= m;\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n int low = mini(bloomDay);\n int high = maxi(bloomDay);\n int n = bloomDay.size();\n\n long long val = m*1LL*k*1LL;\n if(n<val) return -1;\n\n while(low<=high){\n int mid = (low+high)/2;\n if(bouqetCount(bloomDay,m,k,mid)){\n high = mid-1;\n }\n else{\n low = mid+1;\n }\n }\n return low;\n }\n};", "memory": "71178" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n//check for the min bloom day according to the binary search\nbool possible(vector<int>&nums, int day, int m, int k){\n int count = 0;\n int bouquets = 0;\n for(int i = 0 ; i < nums.size();i++){\n if (nums[i]<=day) count++;\n else {\n bouquets+= count/k;\n count =0;\n }\n }\n bouquets+= count/k;\n return bouquets>=m;\n}\n//taking the minimum element for the intialisation of the start point for binary search\nint minelement(vector<int>bloomday){\n int minn =0; \n for(int i =0;i<bloomday.size();i++){\n minn = min(bloomday[i],minn);\n }\n return minn; \n }\n//taking the maximum element for the intialisation of the end point for binary search\nint maxelement(vector<int>bloomday){\n int maxi =0; \n for(int i =0;i<bloomday.size();i++){\n maxi = max(bloomday[i],maxi);\n }\n return maxi; \n }\n//the main function where the possible funvtion is called for the values in this function\n int minDays(vector<int>& bloomDay, int m, int k) {\n int start = minelement(bloomDay), end = maxelement(bloomDay);\n int ans = end;\n if(bloomDay.size()<(long long)m*k){\n return -1; \n }\n while(start<= end){\n int mid = (start+ end)/2;\n if(possible(bloomDay,mid,m,k)==1){\n ans= mid;\n end = mid-1;\n }\n else start = mid+1;\n }\n return ans;\n }\n};", "memory": "72589" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\n private:\n int mini(vector<int>a,int n){\n int mi=a[0];\n \n for(int i=0;i<n;i++){\n \n if(a[i]<mi)mi=a[i];\n }\n return mi;\n }\n int maxi(vector<int>a,int n){\n \n int ma=a[0];\n for(int i=0;i<n;i++){\n if(a[i]>ma)ma=a[i];\n \n }\n return ma;\n }\n \n bool fun(vector<int>&a,int m,int k, int l){\n int n=a.size();\n int count=0;\n int b=0;\n for(int i=0;i<n;i++){\n if(a[i]<=l)count++;\n else{\n b+=count/k;\n count=0;\n }\n }\n b+=count/k;\n\n if(b>=m)return true;\n else return false;\n }\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n=bloomDay.size();\n long long val=m*1LL*k*1LL;\n if(k>n)return -1;\n int low=mini(bloomDay,n);\n int high=maxi(bloomDay,n);\n int ans=-1;\n while(low<=high){\n int mid=low+(high-low)/2;\n \n if(fun(bloomDay,m,k,mid)==true){\n ans=mid;\n high=mid-1;\n }\n else\n low=mid+1;\n\n }\n return ans;\n }\n};", "memory": "74000" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool can_make_bouquet(vector<int> &arr, int m, int k, int day) {\n int n=arr.size();\n\n vector<bool> bloomed;\n for(int &val: arr)\n bloomed.push_back(val <= day);\n\n int seq=0, cnt=0;\n for(int i=0; i<n; ++i) {\n\n if(bloomed[i]) {\n ++seq;\n } else {\n cnt += seq/k;\n seq = 0;\n\n if(cnt >= m)\n return true;\n } \n }\n\n cnt += seq/k;\n return cnt >= m;\n }\n\n int minDays(vector<int> &arr, int m, int k) {\n int n=arr.size();\n\n if((long long)m*k > n)\n return -1;\n \n int l=INT_MAX, r=INT_MIN;\n for(int &val: arr) {\n l=min(l, val);\n r=max(r, val);\n }\n\n while(l<=r) {\n int mid=l+(r-l)/2;\n\n if(can_make_bouquet(arr, m, k, mid))\n r=mid-1;\n else\n l=mid+1;\n }\n\n return l;\n }\n};\n", "memory": "75411" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isPossible(vector<int>& bloomDay, int m, int k, int& days)\n {\n vector<bool>active;\n for(int i=0;i<bloomDay.size();i++)\n {\n active.push_back(bloomDay[i] <= days);\n }\n int i = 0, count = 0, bouq = 0;\n while(i<active.size())\n {\n while(i<active.size() && active[i])\n {\n count++;\n i++;\n }\n bouq += (count/k);\n i++;\n count = 0;\n }\n\n return bouq >= m;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n = bloomDay.size(), lo = 0, hi = *max_element(bloomDay.begin(), bloomDay.end());\n int res = -1;\n while(lo <= hi)\n {\n int mid = lo + (hi-lo)/2;\n if(isPossible(bloomDay, m, k, mid))\n {\n res = mid;\n hi = mid-1;\n }\n else lo = mid+1;\n }\n\n return res;\n }\n};", "memory": "75411" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& v, int m, int k) {\n int n = v.size() ;\n long long int m1 = m;\n long long int k1 = k;\n long long int mk = m1*k1 ;\n if(mk>n)\n return -1;\n vector<int> t;\n for(int i =0 ; i<n ;i++)\n t.push_back(v[i]) ;\n sort(t.begin(), t.end()) ;\n int i = 0 , j =n-1 , mid ;\n cout<<i<<j<<endl ;\n while(i<=j){\n mid = i+ (j-i)/2 ;\n int f =0;\n int r =0;\n for(int l=0 ; l<n ; l++){\n if(v[l]<=t[mid]){\n r++;\n }else r =0;\n // cout<<l<<\" \"<<r<<endl ;\n if(r==k){\n f++ ;\n r =0;\n }\n }\n //cout<<mid<<\" \"<<f<<endl ;\n if(f<m)\n i = mid+1 ;\n else j =mid-1;\n }\n return t[i] ;\n }\n \n};", "memory": "76823" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDays(vector<int>& a, int m, int k) {\n if ((long long)m * k > a.size()) {\n return -1;\n }\n vector<int>vec;\n long long n=a.size();\n for(int i=0 ; i<n ; i++){\n vec.push_back(a[i]);\n }\n \n sort(vec.begin(),vec.end());\n int l=1 ,r=vec[n-1];\n while(l<r){\n int ans=-1;\n int i=l+(r-l)/2;\n int count=0,ancount=0;\n for(int j=0 ; j<n ; j++){\n if(a[j]<=i){\n count++;\n if(count==k){\n ancount++;\n if(ancount==m){\n ans=i;\n break;\n }\n count=0;\n }\n }else{\n count=0;\n }\n }\n if(ans!=-1){\n r=i;\n }else{\n l=i+1;\n }\n }\n return l;\n }\n};", "memory": "76823" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n vector<int> arr;\n for(int i = 0;i<bloomDay.size();i++){\n arr.push_back(bloomDay[i]);\n }\n sort(arr.begin(),arr.end());\n\n int n = arr.size();\n int s = arr[0];\n int e = arr[n-1];\n int ans = -1;\n\n int mid = (s+e)/2;\n\n while(s<=e){\n if(isPossible(bloomDay,mid,m,k)){\n ans = mid;\n e = mid - 1;\n }\n else{\n s = mid + 1;\n }\n mid = (s+e)/2;\n }\n return ans;\n \n }\n\n bool isPossible(vector<int>& nums,int mid,int m,int k){\n int nof = 0;\n int nob = 0;\n\n for(int i =0;i<nums.size();i++){\n if(nums[i]<=mid){\n nof++;\n if(nof == k){\n nob++;\n nof = 0;\n }\n }\n else{\n nof = 0;\n }\n }\n\n if(nob>=m){\n return true;\n }\n else{\n return false;\n }\n }\n};", "memory": "78234" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool check(vector<int> &bloomDay, int minDays, int b, int k){\n vector<bool> F(bloomDay.size(), false);\n for(int i=0;i<F.size();i++){\n if(bloomDay[i]<=minDays) F[i]=true;\n }\n bool flag=false;\n vector<int> V; \n int count=0;\n for(int i=0;i<F.size();i++){\n if(F[i]==false){\n if(count>0){\n V.push_back(count);\n count=0;\n }\n flag=false;\n }\n else{\n if(flag==true){\n count++;\n }\n else{\n flag=true;\n count=1;\n }\n }\n }\n if(count>0) V.push_back(count);\n // for(auto x:V) cout<<x<<endl;\n int total=0;\n for(auto x:V) total+=x/k;\n //cout<<minDays<<\" - \"<<total<<endl;\n //cout<<(total>=b)<<endl;\n return total>=b;\n }\n int minDays(vector<int>& bloomDay, int m, int k) {\n int l=1,r=0, ans=-1;\n for(auto x:bloomDay) r=max(r, x);\n // check(bloomDay, 3, 3, 1);\n while(l<=r){\n int mid=l+(r-l)/2;\n if(check(bloomDay, mid, m, k)) ans=mid, r=mid-1;\n else l=mid+1;\n }\n return ans;\n }\n};", "memory": "78234" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n int bqCount(vector<int>&bloomDay , int maxday , int k){\n vector<int>larger;\n\n for(int i = 0;i<bloomDay.size();i++){\n if(bloomDay[i] > maxday) larger.push_back(i);\n }\n\n\n int n =larger.size();\n int prev = -1;\n int bqcnt= 0;\n for(int i = 0;i<n;i++){\n int index = larger[i];\n int diff = index - prev -1;\n bqcnt += diff/k;\n prev = index;\n }\n\n \n\n if(larger.size() > 0)\n bqcnt += (bloomDay.size() - larger[n-1] -1)/k;\n else bqcnt += bloomDay.size()/k;\n return bqcnt;\n\n return 0;\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n\n int n = bloomDay.size();\n // long long fcnt =m*k;\n long long int m1 = m;\n long long int k1 = k;\n\n if(n < m1*k1) return -1;\n\n\n int maxi = 0;\n int mini = 1e9+1;\n\n for(auto i : bloomDay){\n maxi = max(maxi,i);\n mini = min(mini,i);\n }\n\n\n\n int st = mini;\n int end = maxi;\n int ans = 0;\n\n int mid = 1e9+1;\n while(st <= end){\n mid = st+(end-st)/2;\n\n int bqcnt = bqCount(bloomDay,mid,k);\n\n if(bqcnt >= m){\n end = mid-1;\n ans= mid;\n }else {\n st = mid+1;\n }\n }\n\n return ans;\n\n }\n};", "memory": "79645" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int check(int mid,vector<int>& bloomDay, int m, int k){\n vector<int>v;\n int cnt=0;\n for(int i=0;i<bloomDay.size();i++){\n if(bloomDay[i]<=mid){\n cnt++;\n }\n else{\n v.push_back(cnt);\n cnt=0;\n }\n }\n if(cnt!=0)\n v.push_back(cnt);\n int ans=0;\n for(auto it:v){\n ans+=(it/k);\n }\n if(ans>=m)\n return 1;\n return 0;\n }\n\n int minDays(vector<int>& bloomDay, int m, int k) {\n if(m*1LL*k>bloomDay.size())\n return -1;\n int low=*min_element(bloomDay.begin(),bloomDay.end());\n int high=*max_element(bloomDay.begin(),bloomDay.end());\n while(low<=high){\n int mid=(low+high)/2;\n if(check(mid,bloomDay,m,k)){\n high=mid-1;\n }\n else{\n low=mid+1;\n }\n }\n return low;\n }\n};", "memory": "81056" }
1,605
<p>You are given an integer array <code>bloomDay</code>, an integer <code>m</code> and an integer <code>k</code>.</p> <p>You want to make <code>m</code> bouquets. To make a bouquet, you need to use <code>k</code> <strong>adjacent flowers</strong> from the garden.</p> <p>The garden consists of <code>n</code> flowers, the <code>i<sup>th</sup></code> flower will bloom in the <code>bloomDay[i]</code> and then can be used in <strong>exactly one</strong> bouquet.</p> <p>Return <em>the minimum number of days you need to wait to be able to make </em><code>m</code><em> bouquets from the garden</em>. If it is impossible to make m bouquets return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2 <strong>Output:</strong> -1 <strong>Explanation:</strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers. Here is the garden after the 7 and 12 days: After day 7: [x, x, x, x, _, x, x] We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent. After day 12: [x, x, x, x, x, x, x] It is obvious that we can make two bouquets in different ways. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>bloomDay.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= bloomDay[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
3
{ "code": "class Solution {\n private:\n bool isPossible(vector<int>& arr,int mid,int k,int m)\n {\n int i=0;\n int cnt=0;\n while(i<arr.size())\n {\n if(arr[i]<=mid)\n {\n cnt++;\n i+=k;\n }\n else\n {\n i++;\n }\n }\n return cnt>=m;\n }\n vector<int> getMaxArr(vector<int>& arr,int k)\n {\n priority_queue<pair<int,int>>pq;\n int n=arr.size();\n for(int i=0;i<n && i<k;i++)\n {\n pq.push({arr[i],i});\n }\n vector<int>ans;\n for(int i=k;i<n;i++)\n {\n ans.push_back(pq.top().first);\n while(!pq.empty() && i-pq.top().second>=k)\n {\n pq.pop();\n }\n pq.push({arr[i],i});\n }\n ans.push_back(pq.top().first);\n return ans;\n }\npublic:\n int minDays(vector<int>& bloomDay, int m, int k) {\n int n=bloomDay.size();\n vector<int>arr=getMaxArr(bloomDay,k);\n long long s=0,sum=0;\n for(int i=0;i<n;i++)\n {\n sum+=bloomDay[i];\n }\n long long e=sum,ans=-1;\n while(s<=e)\n {\n long long mid=(s+e)>>1;\n if(isPossible(arr,mid,k,m))\n {\n ans=mid;\n e=mid-1;\n }\n else\n s=mid+1;\n }\n return (int)ans;\n }\n};", "memory": "82468" }