-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path167. Two Sum II - Input Array Is Sorted.cpp
54 lines (51 loc) · 1.43 KB
/
167. Two Sum II - Input Array Is Sorted.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
47
48
49
50
51
52
53
54
/**
20:32 - 20:42
just a two pointer scan.
for loop in the left enum.
pointer in the right enum, move to left, and become less.
- once less than 0, it's time to move left point to right, which will make the sum bigger.
*/
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<vector<int>> edge(n, vector<int>());
vector<int> du(n, 0);
for (int i = 0; i < n; i++) {
if (i > 0 && ratings[i - 1] < ratings[i]) {
edge[i - 1].push_back(i);
du[i]++;
}
if (i + 1 < n && ratings[i] > ratings[i + 1]) {
edge[i + 1].push_back(i);
du[i]++;
}
}
queue<int> que;
vector<int> ans(n, 0);
for (int i = 0; i < n; i++) {
if (du[i] == 0) {
que.push(i);
ans[i] = 1;
}
}
while (!que.empty()) {
int cur = que.front();
que.pop();
for (int i = 0; i < edge[cur].size(); i++) {
int tar = edge[cur][i];
ans[tar] = max(ans[tar], ans[cur] + 1);
du[tar]--;
if (du[tar] == 0) {
que.push(tar);
}
}
edge[cur].clear();
}
int resp = 0;
for (auto as : ans) {
resp += as;
}
return resp;
}
};