Let's make this one less mysterious. For 1482. Minimum Number of Days to Make m Bouquets, the solution in this repository is mainly a binary search solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: binary search, two pointers, sliding window, greedy.
The notes already sitting in the source point us in the right direction:
- sliding window + binary search
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are check, minDays.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//sliding window + binary search
02//Runtime: 1744 ms, faster than 10.00% of C++ online submissions for Minimum Number of Days to Make m Bouquets.
03//Memory Usage: 165.3 MB, less than 10.00% of C++ online submissions for Minimum Number of Days to Make m Bouquets.
04class Solution {
05public:
06 bool check(vector<int>& bloomDay, int m, int k, int day){
07 int start = 0, end = 0;
08
09 for(int i = 0; i < bloomDay.size(); i++){
10 if(bloomDay[i] <= day){
11 if(start == -1 || end+1 != i){
12 //new window
13 start = end = i;
14 }else if(end+1 == i){
15 //extend existing window
16 end++;
17 }
18
19 if(end-start+1 == k){
20 m--;
21 start = end = -1;
22 }
23
24 if(m == 0) return true;
25 }
26 }
27
28 return false;
29 };
30
31 int minDays(vector<int>& bloomDay, int m, int k) {
32 int n = bloomDay.size();
33 if(m*k > n) return -1;
34
35 unordered_map<int, int> mcounter;
36 for(int& day : bloomDay){
37 mcounter[day] += 1;
38 }
39
40 vector<vector<int>> counter;
41 for(auto it = mcounter.begin(); it != mcounter.end(); it++){
42 counter.push_back({it->first, it->second});
43 }
44
45 //smaller day, larger count
46 sort(counter.begin(), counter.end(), [](const vector<int>& v1, const vector<int>& v2){
47 return (v1[0] == v2[0]) ? (v1[1] > v2[1]) : (v1[0] < v2[0]);
48 });
49
50 int left = -1, right = counter.size()-1, mid;
51 int totalCount = 0;
52 for(int i = 0; i < counter.size(); i++){
53 totalCount += counter[i][1];
54 if(totalCount >= m*k){
55 left = i;
56 break;
57 // if(check(bloomDay, m, k, counter[i][0])){
58 // return counter[i][0];
59 // }
60 }
61 }
62
63 if(left == -1) return -1;
64
65 //find left boundary
66 while(left <= right){
67 mid = left + (right-left)/2;
68 if(check(bloomDay, m, k, counter[mid][0])){
69 right = mid-1;
70 }else{
71 left = mid+1;
72 }
73 }
74
75 return counter[left][0];
76 }
77};
78
79//just binary search
80//https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/discuss/686316/JavaC%2B%2BPython-Binary-Search
81//Runtime: 364 ms, faster than 88.50% of C++ online submissions for Minimum Number of Days to Make m Bouquets.
82//Memory Usage: 63.3 MB, less than 10.00% of C++ online submissions for Minimum Number of Days to Make m Bouquets.
83//time: O(log(max(bloomDay))), space: O(1)
84class Solution {
85public:
86 int minDays(vector<int>& bloomDay, int m, int k) {
87 int n = bloomDay.size();
88 if(m*k > n) return -1;
89
90 int left = 1, right = 1e9;
91
92 while(left <= right){
93 int mid = left + (right - left)/2;
94 //window size and window count
95 int wsize = 0, wcount = 0;
96 for(int i = 0; i < bloomDay.size(); i++){
97 if(mid < bloomDay[i]){
98 //the old window is broken
99 wsize = 0;
100 }else if(++wsize >= k){
101 //the window is large enough
102 wcount++;
103 //prepare for next iteration
104 wsize = 0;
105 }
106 }
107
108 if(wcount >= m){
109 right = mid-1;
110 }else{
111 left = mid+1;
112 }
113 }
114 /*
115 we can definitely find a day s.t. we can hold m bouquets,
116 because n >= m*k, we just wait until all flowers blossom
117 */
118 return left;
119 }
120};
Cost