-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path135.candy.cpp
More file actions
43 lines (41 loc) · 1.07 KB
/
Copy path135.candy.cpp
File metadata and controls
43 lines (41 loc) · 1.07 KB
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
/*
* @lc app=leetcode id=135 lang=cpp
*
* [135] Candy
*/
// @lc code=start
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int candy(vector<int>& ratings) {
// use left and right iteration to guarantee
// 1. more candidates than left if have a higher rating
// 2. more candidates than right if have a higher rating
vector<int> left(ratings.size(), 1);
vector<int> right(ratings.size(), 1);
int ans = 0;
// min candidates to satisfy left
for(int i = 1; i < ratings.size(); ++i)
{
if(ratings[i - 1] < ratings[i])
{
left[i] = left[i - 1] + 1;
}
}
// min candidates to satisfy right
for(int i = ratings.size() - 1; i > 0; --i)
{
if(ratings[i-1] > ratings[i])
{
right[i-1] = right[i] + 1;
}
}
for(int i = 0; i < ratings.size(); ++i)
{
ans += max(left[i] ,right[i]);
}
return ans;
}
};
// @lc code=end