Skip to content

Commit 7b523ba

Browse files
committed
Time: 0 ms (100%), Space: 22.8 MB (59.49%) - LeetHub
1 parent 3688464 commit 7b523ba

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

0135-candy/0135-candy.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int candy(vector<int>& v) {
4+
int n = v.size();
5+
vector<int> ans(n, 1);
6+
for (int i = 1; i < n; i++) {
7+
if (v[i] > v[i - 1]) {
8+
ans[i] = max(ans[i], ans[i - 1] + 1);
9+
}
10+
}
11+
for (int i = n - 2; i >= 0; i--) {
12+
if (v[i] > v[i + 1]) {
13+
ans[i] = max(ans[i], ans[i + 1] + 1);
14+
}
15+
}
16+
int cnt = 0;
17+
for (auto& i : ans) cnt += i;
18+
return cnt;
19+
}
20+
};

0 commit comments

Comments
 (0)