forked from shishirRsiam/LeetCode-Solve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path135. Candy.cpp
More file actions
55 lines (52 loc) · 1.5 KB
/
135. Candy.cpp
File metadata and controls
55 lines (52 loc) · 1.5 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
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
bool checkIsSorted(vector<int> ratings)
{
int n = ratings.size();
bool flag1 = true, flag2 = true;
for(int i = 0; i < n - 1; i++)
{
if(ratings[i] >= ratings[i + 1])
{
flag1 = false;
break;
}
}
for(int i = 0; i < n - 1; i++)
{
if(ratings[i] <= ratings[i + 1])
{
flag2 = false;
break;
}
}
return flag1 or flag2;
}
int candy(vector<int>& ratings)
{
int n = ratings.size();
if(checkIsSorted(ratings))
return long(n * (n + 1)) / 2;
bool flag = true;
vector<int> storeCandy(n, 1);
while(flag)
{
flag = false;
for(int i = 1; i < n - 1; i++)
{
if(storeCandy[i] <= storeCandy[i + 1] and ratings[i] > ratings[i + 1])
storeCandy[i] += 1, flag = true;
else if(storeCandy[i] <= storeCandy[i - 1] and ratings[i] > ratings[i - 1])
storeCandy[i] += 1, flag = true;
}
if(n > 1 and ratings[0] > ratings[1])
storeCandy[0] = storeCandy[1] + 1;
if(n > 1 and ratings[n - 1] > ratings[n - 2])
storeCandy[n - 1] = storeCandy[n - 2] + 1;
}
int ans = 0;
for(auto candy : storeCandy)
ans += candy;
return ans;
}
};