Skip to content

Commit 3531e12

Browse files
authored
Merge pull request #1636 from ivanpenaloza/may13
adding updates
2 parents 1cd964a + f84c50b commit 3531e12

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def candy(self, ratings: List[int]) -> int:
6+
"""
7+
Distribute candies to children based on ratings.
8+
9+
Rules:
10+
1. Each child gets at least 1 candy
11+
2. Children with higher rating than neighbors get more candies
12+
13+
Strategy: Two-pass greedy
14+
- Left-to-right: Ensure each child has more candies than left neighbor if rating is higher
15+
- Right-to-left: Ensure each child has more candies than right neighbor if rating is higher
16+
- Take maximum from both passes to satisfy both neighbors
17+
18+
Time: O(n), Space: O(n)
19+
"""
20+
21+
n = len(ratings)
22+
if n == 0:
23+
return 0
24+
25+
# Initialize all children with 1 candy
26+
candies = [1] * n
27+
28+
# Left-to-right pass: Compare with left neighbor
29+
# If current child has higher rating than left neighbor,
30+
# give them one more candy than left neighbor
31+
for i in range(1, n):
32+
if ratings[i] > ratings[i - 1]:
33+
candies[i] = candies[i - 1] + 1
34+
35+
# Right-to-left pass: Compare with right neighbor
36+
# If current child has higher rating than right neighbor,
37+
# ensure they have more candies (take max of current and right+1)
38+
for i in range(n - 2, -1, -1):
39+
if ratings[i] > ratings[i + 1]:
40+
candies[i] = max(candies[i], candies[i + 1] + 1)
41+
42+
# Return total candies needed
43+
return sum(candies)
44+
45+
46+
"""
47+
Example walkthrough for ratings = [1, 0, 2]:
48+
49+
Initial: candies = [1, 1, 1]
50+
51+
Left-to-right pass:
52+
i=1: ratings[1]=0 < ratings[0]=1 → no change → candies = [1, 1, 1]
53+
i=2: ratings[2]=2 > ratings[1]=0 → candies[2] = candies[1] + 1 = 2 → candies = [1, 1, 2]
54+
55+
Right-to-left pass:
56+
i=1: ratings[1]=0 < ratings[2]=2 → no change → candies = [1, 1, 2]
57+
i=0: ratings[0]=1 > ratings[1]=0 → candies[0] = max(1, 1+1) = 2 → candies = [2, 1, 2]
58+
59+
Total: 2 + 1 + 2 = 5
60+
61+
62+
Example walkthrough for ratings = [1, 2, 2]:
63+
64+
Initial: candies = [1, 1, 1]
65+
66+
Left-to-right pass:
67+
i=1: ratings[1]=2 > ratings[0]=1 → candies[1] = 1 + 1 = 2 → candies = [1, 2, 1]
68+
i=2: ratings[2]=2 = ratings[1]=2 → no change → candies = [1, 2, 1]
69+
70+
Right-to-left pass:
71+
i=1: ratings[1]=2 = ratings[2]=2 → no change → candies = [1, 2, 1]
72+
i=0: ratings[0]=1 < ratings[1]=2 → no change → candies = [1, 2, 1]
73+
74+
Total: 1 + 2 + 1 = 4
75+
76+
77+
Example walkthrough for ratings = [1, 3, 2, 2, 1]:
78+
79+
Initial: candies = [1, 1, 1, 1, 1]
80+
81+
Left-to-right:
82+
i=1: 3 > 1 → candies[1] = 2 → [1, 2, 1, 1, 1]
83+
i=2: 2 < 3 → no change → [1, 2, 1, 1, 1]
84+
i=3: 2 = 2 → no change → [1, 2, 1, 1, 1]
85+
i=4: 1 < 2 → no change → [1, 2, 1, 1, 1]
86+
87+
Right-to-left:
88+
i=3: 2 > 1 → candies[3] = max(1, 2) = 2 → [1, 2, 1, 2, 1]
89+
i=2: 2 = 2 → no change → [1, 2, 1, 2, 1]
90+
i=1: 3 > 2 → candies[1] = max(2, 2) = 2 → [1, 2, 1, 2, 1]
91+
i=0: 1 < 3 → no change → [1, 2, 1, 2, 1]
92+
93+
Total: 1 + 2 + 1 + 2 + 1 = 7
94+
"""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function candy(ratings: number[]): number {
2+
const n = ratings.length;
3+
if (n === 0) return 0;
4+
5+
// Initialize all children with 1 candy
6+
const candies: number[] = new Array(n).fill(1);
7+
8+
// Left-to-right pass: Compare with left neighbor
9+
for (let i = 1; i < n; i++) {
10+
if (ratings[i] > ratings[i - 1]) {
11+
candies[i] = candies[i - 1] + 1;
12+
}
13+
}
14+
15+
// Right-to-left pass: Compare with right neighbor
16+
for (let i = n - 2; i >= 0; i--) {
17+
if (ratings[i] > ratings[i + 1]) {
18+
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
19+
}
20+
}
21+
22+
return candies.reduce((sum, c) => sum + c, 0);
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_23\
3+
.ex_15_candy import Solution
4+
5+
class CandyTestCase(unittest.TestCase):
6+
7+
def test_candy_first_case(self):
8+
solution = Solution()
9+
output = solution.candy(ratings = [1,0,2])
10+
target = 5
11+
self.assertEqual(output, target)
12+
13+
def test_candy_second_case(self):
14+
solution = Solution()
15+
output = solution.candy(ratings = [1,2,2])
16+
target = 4
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)