-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path135.cpp
32 lines (28 loc) · 803 Bytes
/
135.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
// 135. Candy - https://leetcode.com/problems/candy
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int candy(vector<int>& ratings) {
int n = (int)ratings.size();
vector<int> candies(n, 1);
// L -> R
for (int idx = 1; idx < n; ++idx) {
if (ratings[idx] > ratings[idx - 1]) {
candies[idx] = candies[idx - 1] + 1;
}
}
// R -> L
for (int idx = n - 2; idx >= 0; --idx) {
if (ratings[idx] > ratings[idx + 1]) {
candies[idx] = max(candies[idx], candies[idx + 1] + 1);
}
}
int result = accumulate(candies.begin(), candies.end(), 0);
return result;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}