The trick here is to name the state correctly, then let the implementation follow. For 1042. Flower Planting With No Adjacent, the solution in this repository is mainly a straightforward implementation 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: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/flower-planting-with-no-adjacent/discuss/290858/JavaC%2B%2BPython-Greedily-Paint
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 gardenNoAdj, colors.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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//https://leetcode.com/problems/flower-planting-with-no-adjacent/discuss/290858/JavaC%2B%2BPython-Greedily-Paint
02//Runtime: 180 ms, faster than 65.71% of C++ online submissions for Flower Planting With No Adjacent.
03//Memory Usage: 38.8 MB, less than 100.00% of C++ online submissions for Flower Planting With No Adjacent.
04
05class Solution {
06public:
07 vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
08 vector<int> ans(N);
09 vector<vector<int>> graph(N);
10
11 for(vector<int>& path : paths){
12 int x = path[0], y = path[1];
13 graph[x-1].push_back(y-1);
14 graph[y-1].push_back(x-1);
15 }
16
17 for(int i = 0; i < N; i++){
18 vector<int> colors(5); //4+1
19
20 //(i, j) forms an edge
21 for(int j : graph[i]){
22 //ans[j] : (j+1)th graden's color index
23 //this means "ans[j]"th color is used
24 colors[ans[j]] = 1;
25 // cout << i << "->" << j << ", " << ans[j] << endl;
26 }
27
28 // cout << colors[0] << " " << colors[1] << " " << colors[2] << " " << colors[3] << " " << colors[4] << endl;
29 for(int c = 1; c <= 4; c++){
30 // for(int c = 4; c >= 1; c--){
31 // cout << c << ", " << colors[c] << endl;
32 //pick the first unused color
33 if(!colors[c]){
34 ans[i] = c;
35 break;
36 }
37 }
38 // for(int i = 0; i < N; i++){
39 // cout << ans[i] << " ";
40 // }
41 // cout << endl;
42 }
43
44 return ans;
45 }
46};
Cost