forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1042. Flower Planting With No Adjacent.cpp
46 lines (40 loc) · 1.64 KB
/
1042. Flower Planting With No Adjacent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//https://leetcode.com/problems/flower-planting-with-no-adjacent/discuss/290858/JavaC%2B%2BPython-Greedily-Paint
//Runtime: 180 ms, faster than 65.71% of C++ online submissions for Flower Planting With No Adjacent.
//Memory Usage: 38.8 MB, less than 100.00% of C++ online submissions for Flower Planting With No Adjacent.
class Solution {
public:
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
vector<int> ans(N);
vector<vector<int>> graph(N);
for(vector<int>& path : paths){
int x = path[0], y = path[1];
graph[x-1].push_back(y-1);
graph[y-1].push_back(x-1);
}
for(int i = 0; i < N; i++){
vector<int> colors(5); //4+1
//(i, j) forms an edge
for(int j : graph[i]){
//ans[j] : (j+1)th graden's color index
//this means "ans[j]"th color is used
colors[ans[j]] = 1;
// cout << i << "->" << j << ", " << ans[j] << endl;
}
// cout << colors[0] << " " << colors[1] << " " << colors[2] << " " << colors[3] << " " << colors[4] << endl;
for(int c = 1; c <= 4; c++){
// for(int c = 4; c >= 1; c--){
// cout << c << ", " << colors[c] << endl;
//pick the first unused color
if(!colors[c]){
ans[i] = c;
break;
}
}
// for(int i = 0; i < N; i++){
// cout << ans[i] << " ";
// }
// cout << endl;
}
return ans;
}
};