Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class Solution:
# Suitable ranges for:
# - HH is 00 to 23
# - MM is 00 to 59
def num_to_digits(self, n):
s = list(map(int, list(str(n))))
if n < 10:
return [0, s[0]]
return s
def is_ok(self, ns):
for n in ns:
if n not in self.nums:
return False
return True
# Returns (m: int, reset: bool)
def next_group(self, m, lower, upper):
for m in range(m+1, upper):
if self.is_ok(self.num_to_digits(m)):
return (m, False)
# Now we have to find the smallest possible at all
for m in range(lower, upper):
if self.is_ok(self.num_to_digits(m)):
return (m, True)
def nextClosestTime(self, time: str) -> str:
# Remove :
time = time.replace(":", "")
hrs = int(time[:2])
mins = int(time[2:])
# Produce a list of integers
time = list(map(int, list(time)))
# Sort the numbers
self.nums = sorted(time)
mins, force = self.next_group(mins, 0, 60)
if force:
hrs, force = self.next_group(hrs, 0, 24)
return "{:02d}:{:02d}".format(hrs, mins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment