forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
41 lines (30 loc) · 807 Bytes
/
main.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
/// Source : https://leetcode.com/problems/candy/
/// Author : liuyubobobo
/// Time : 2018-12-10
#include <iostream>
#include <vector>
using namespace std;
/// Two arrays
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> left(n, 1);
for(int i = 1; i < n; i ++)
if(ratings[i] > ratings[i - 1])
left[i] = left[i - 1] + 1;
vector<int> right(n, 1);
for(int i = n - 2; i >= 0; i --)
if(ratings[i] > ratings[i + 1])
right[i] = right[i + 1] + 1;
int res = 0;
for(int i = 0; i < n; i ++)
res += max(left[i], right[i]);
return res;
}
};
int main() {
return 0;
}