Skip to content

Instantly share code, notes, and snippets.

@Transfusion
Created October 18, 2019 17:53
Show Gist options
  • Select an option

  • Save Transfusion/c5402d96ef2403e51f49568beb25a709 to your computer and use it in GitHub Desktop.

Select an option

Save Transfusion/c5402d96ef2403e51f49568beb25a709 to your computer and use it in GitHub Desktop.
949. Largest Time for Given Digits
class Solution:
def __init__(self):
self.max_hours = -1
self.max_minutes = -1
def A_n_k_pythonic(self, a, used, curr, depth=0): # curr is the current solution
if depth == 2:
hours = curr[0] * 10 + curr[1]
if hours < self.max_hours or hours > 23:
return
elif depth == len(a):
hours = curr[0] * 10 + curr[1]
minutes = curr[2] * 10 + curr[3]
if hours > self.max_hours and minutes < 60:
self.max_hours = hours
self.max_minutes = minutes
elif hours == self.max_hours and minutes < 60 and minutes > self.max_minutes:
self.max_hours = hours
self.max_minutes = minutes
return
for i in range(len(a)):
if not used[i]:
curr.append(a[i])
used[i] = True
self.A_n_k_pythonic(a, used, curr, depth + 1) # if the depth is 3 you only pop twice.
curr.pop()
used[i] = False
return
def largestTimeFromDigits(self, a: List[int]) -> str:
used = [False] * len(a)
self.A_n_k_pythonic(a, used, [])
# print(self.max_hours, self.max_minutes)
if self.max_hours == -1 or self.max_minutes == -1:
return ""
return "%02d:%02d" % (self.max_hours, self.max_minutes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment