File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed
2025-06-June-LeetCoding-Challenge Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 | [ ] ( ) | | |
4141| --- | --- | --- | --- |
4242| Easy | 0 | 0 | 0 |
4343| Medium | 1 | 0 | 1 |
44- | Hard | 0 | 0 | 0 |
44+ | Hard | 1 | 1 | 0 |
You can’t perform that action at this time.
0 commit comments