Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab1ebe1

Browse files
D. J.:
- Added the leetcode problem and solution for 401
1 parent 0a5943e commit ab1ebe1

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
- [393 UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/description/)
240240
- [394 Decode String](https://leetcode.com/problems/decode-string/description/)
241241
- [399 Evaluate Division](https://leetcode.com/problems/evaluate-division/description/)
242+
- [401 Binary Watch](https://leetcode.com/problems/binary-watch/description/)
242243
- [405 Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/)
243244
- [412 Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/)
244245
- [415 Add Strings](https://leetcode.com/problems/add-strings/description/)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def readBinaryWatch(self, turnedOn: int) -> List[str]:
8+
"""
9+
A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs
10+
on the bottom to represent the minutes (0-59). Each LED represents a zero or
11+
one, with the least significant bit on the right.
12+
- For example, the below binary watch reads "4:51".
13+
14+
Given an integer turnedOn which represents the number of LEDs that are currently
15+
on (ignoring the PM), return all possible times the watch could represent. You
16+
may return the answer in any order.
17+
18+
The hour must not contain a leading zero.
19+
- For example, "01:00" is not valid. It should be "1:00".
20+
21+
The minute must consist of two digits and may contain a leading zero.
22+
- For example, "10:2" is not valid. It should be "10:02".
23+
"""
24+
if 0 < turnedOn >= 9:
25+
return []
26+
27+
queue = [(0, 0, 0, {8, 4, 2, 1}, {32, 16, 8, 4, 2, 1})]
28+
visited = set()
29+
result = []
30+
while queue:
31+
# Get data from queue
32+
(
33+
cur_hour,
34+
cur_minutes,
35+
cur_turnedOn,
36+
cur_hour_missing,
37+
cur_minutes_missing,
38+
) = queue.pop()
39+
40+
# Check if goal state is reached
41+
if turnedOn == cur_turnedOn:
42+
result.append((cur_hour, cur_minutes))
43+
continue
44+
45+
# Iterate over all valid hour options
46+
for hour in cur_hour_missing:
47+
# Next state
48+
next_hour = cur_hour + hour
49+
next_minutes = cur_minutes
50+
next_turnedOn = cur_turnedOn + 1
51+
next_hour_missing = cur_hour_missing - {hour}
52+
next_minutes_missing = cur_minutes_missing
53+
54+
# Check if next time is valid and is not visited before
55+
if next_hour <= 11 and (next_hour, next_minutes) not in visited:
56+
visited.add((next_hour, next_minutes))
57+
queue.append(
58+
(
59+
next_hour,
60+
next_minutes,
61+
next_turnedOn,
62+
next_hour_missing,
63+
next_minutes_missing,
64+
)
65+
)
66+
67+
# Iterate over all valid minutes options
68+
for minutes in cur_minutes_missing:
69+
# Next state
70+
next_hour = cur_hour
71+
next_minutes = cur_minutes + minutes
72+
next_turnedOn = cur_turnedOn + 1
73+
next_hour_missing = cur_hour_missing
74+
next_minutes_missing = cur_minutes_missing - {minutes}
75+
76+
# Check if next time is valid and is not visited before
77+
if next_minutes <= 59 and (next_hour, next_minutes) not in visited:
78+
visited.add((next_hour, next_minutes))
79+
queue.append(
80+
(
81+
next_hour,
82+
next_minutes,
83+
next_turnedOn,
84+
next_hour_missing,
85+
next_minutes_missing,
86+
)
87+
)
88+
return list(map(lambda item: f"{item[0]}:{item[1]:02d}", result))

‎tests/test_401_binary_watch.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._401_binary_watch import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["turnedOn", "expected"],
10+
argvalues=[
11+
(
12+
1,
13+
[
14+
"0:08",
15+
"0:04",
16+
"0:16",
17+
"0:02",
18+
"0:01",
19+
"0:32",
20+
"4:00",
21+
"2:00",
22+
"1:00",
23+
"8:00",
24+
],
25+
),
26+
(9, []),
27+
(0, ["0:00"]),
28+
],
29+
)
30+
def test_func(turnedOn: int, expected: List[str]):
31+
"""Tests the solution of a LeetCode problem."""
32+
possible_times = Solution().readBinaryWatch(turnedOn)
33+
assert possible_times == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /