-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
40 lines (36 loc) · 1.07 KB
/
solution.h
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> f(n);
iota(f.begin(), f.end(), 0);
sort(f.begin(), f.end(), [&](int x, int y) {
return ratings[x] < ratings[y];
});
vector<int> score(n);
for (int i = 0; i < n; i++) {
int idx = f[i];
score[idx] = 1;
if (idx - 1 >= 0 && ratings[idx - 1] < ratings[idx]) {
score[idx] = max(score[idx], score[idx - 1] + 1);
}
if (idx + 1 < n && ratings[idx + 1] < ratings[idx]) {
score[idx] = max(score[idx], score[idx + 1] + 1);
}
}
debug(score);
int ans = 0;
for (int i = 0; i < n; i++) ans += score[i];
return ans;
}
};