Skip to content

Commit ea079cb

Browse files
authored
Merge pull request #1476 from ivan1016017/december05
adding algos
2 parents 160756d + 068da0c commit ea079cb

4 files changed

Lines changed: 173 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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def trap(self, height: List[int]) -> int:
6+
"""
7+
Calculate how much water can be trapped after raining.
8+
9+
Key insight: Water trapped at position i = min(max_left, max_right) - height[i]
10+
(if positive)
11+
12+
Strategy: Two-pointer approach
13+
- Use two pointers from both ends
14+
- Track max height seen from left and right
15+
- Move pointer with smaller max height (water level determined by shorter side)
16+
- Add water trapped at current position
17+
18+
Time: O(n), Space: O(1)
19+
"""
20+
21+
if not height or len(height) < 3:
22+
return 0
23+
24+
left, right = 0, len(height) - 1
25+
left_max, right_max = 0, 0
26+
water_trapped = 0
27+
28+
while left < right:
29+
# Update max heights seen so far
30+
left_max = max(left_max, height[left])
31+
right_max = max(right_max, height[right])
32+
33+
# Water level is determined by the shorter side
34+
if left_max < right_max:
35+
# Left side is shorter, so water trapped at left position
36+
# is determined by left_max
37+
water_trapped += left_max - height[left]
38+
left += 1
39+
else:
40+
# Right side is shorter or equal, so water trapped at right position
41+
# is determined by right_max
42+
water_trapped += right_max - height[right]
43+
right -= 1
44+
45+
return water_trapped
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_22\
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)
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_22\
3+
.ex_16_trapping_rain_water import Solution
4+
5+
class TrappingWaterTestCase(unittest.TestCase):
6+
7+
def test_trapping_water_first_case(self):
8+
solution = Solution()
9+
output = solution.trap(height = [0,1,0,2,1,0,1,3,2,1,2,1])
10+
target = 6
11+
self.assertEqual(output, target)
12+
13+
def test_trapping_water_second_case(self):
14+
solution = Solution()
15+
output = solution.trap(height = [4,2,0,3,2,5])
16+
target = 9
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)