Skip to content

Commit

Permalink
이슈 #452에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 12, 2025
1 parent 90508c5 commit 8dab5e2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LeetCode/Candy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();

vector<int> candies(n, 1);

for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1])
candies[i] = candies[i - 1] + 1;
}

for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1])
candies[i] = max(candies[i], candies[i + 1] + 1);
}

int totalCandies = 0;
for (int candy : candies) {
totalCandies += candy;
}

return totalCandies;
}
};

0 comments on commit 8dab5e2

Please sign in to comment.