Skip to content

Commit 6c641e7

Browse files
committed
Jun 02
1 parent 7690c57 commit 6c641e7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def candy(self, ratings: list[int]) -> int:
3+
n = len(ratings)
4+
candies = [1] * n # every child must have atleast one candy
5+
# first pass -> going from left to right to satisfy condition
6+
for i in range(0, n - 1):
7+
if ratings[i + 1] > ratings[i]:
8+
candies[i + 1] = max(candies[i + 1], candies[i] + 1)
9+
# second pass -> going from right to left to satisfy condition
10+
for i in range(n - 2, -1, -1):
11+
if ratings[i] > ratings[i + 1]:
12+
candies[i] = max(candies[i], candies[i + 1] + 1)
13+
return sum(candies)
14+
15+
16+
def main():
17+
ratings = [1, 0, 2]
18+
assert Solution().candy(ratings) == 5
19+
20+
ratings = [1, 2, 2]
21+
assert Solution().candy(ratings) == 4
22+
23+
24+
if __name__ == '__main__':
25+
main()

2025-06-June-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| Day | Problem | Level | Status |
66
| --- | --- | --- | --- |
77
| June 01 | [2929. Distribute Candies Among Children II](https://leetcode.com/problems/distribute-candies-among-children-ii/) | Medium | Unsolved |
8-
| June 02 | []() | | |
8+
| June 02 | [135. Candy](https://leetcode.com/problems/candy/) | Hard | Solved |
99
| June 03 | []() | | |
1010
| June 04 | []() | | |
1111
| June 05 | []() | | |
@@ -41,4 +41,4 @@
4141
| --- | --- | --- | --- |
4242
| Easy | 0 | 0 | 0 |
4343
| Medium | 1 | 0 | 1 |
44-
| Hard | 0 | 0 | 0 |
44+
| Hard | 1 | 1 | 0 |

0 commit comments

Comments
 (0)