Let's make this one less mysterious. For 605. Can Place Flowers, the solution in this repository is mainly a binary search solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are canPlaceFlowers.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 20 ms, faster than 91.05% of C++ online submissions for Can Place Flowers.
02//Memory Usage: 10.9 MB, less than 86.42% of C++ online submissions for Can Place Flowers.
03class Solution {
04public:
05 bool canPlaceFlowers(vector<int>& flowerbed, int n) {
06 vector<int> planted;
07 for(int i = 0; i < flowerbed.size(); i++){
08 if(flowerbed[i] == 1) planted.push_back(i);
09 }
10
11 //when the first flower locates at x >= 2, we can plant flower(s) at left
12 planted.insert(planted.begin(), -2);
13 //when the last flower locates at x <= n-3, we can plant flower(s) at right
14 planted.push_back(flowerbed.size()+1);
15
16 int count = 0;
17 // if(planted[0] >= 2) count += planted[i]/2;
18 // if(flowerbed.size()-1-planted.back() >= 2) count += (flowerbed.size()-1-planted.back())/2;
19
20 for(int i = 1; i < planted.size(); i++){
21 if(planted[i] - planted[i-1] >= 4){
22 count += ((planted[i] - planted[i-1])/2-1);
23 }
24 if(count >= n) return true;
25 }
26
27 return false;
28 }
29};
30
31//single scan & optimized
32//Runtime: 20 ms, faster than 91.12% of C++ online submissions for Can Place Flowers.
33//Memory Usage: 10.1 MB, less than 100.00% of C++ online submissions for Can Place Flowers.
34//time: O(n), space: O(1)
35class Solution {
36public:
37 bool canPlaceFlowers(vector<int>& flowerbed, int n) {
38 int count = 0;
39 for(int i = 0; i < flowerbed.size(); i++){
40 if(flowerbed[i] == 0 && (i == 0 || flowerbed[i-1] == 0) && (i == flowerbed.size()-1 || flowerbed[i+1] ==0)) {
41 //case 1: i == 0 && i == flowerbed.size()-1(length 1, empty flowerbed)
42 //case 2: i == 0 && flowerbed[i+1] == 0(0th and 1st places are empty)
43 //case 3: flowerbed[i-1] == 0 && i == flowerbed.size()-1(last and 2nd last places are empty)
44 //case 4: flowerbed[i-1] == 0 && flowerbed[i+1] == 0(middle empty place)
45 flowerbed[i] = 1;
46 count ++;
47 }
48 if(count >= n) return true;
49 }
50 return count >= n;
51 }
52};
Cost