Gyh's Braindump

681. Next Closest Time

tags
trick
source
leetcode

Edge Cases

Solution 1

  1. get digits array
  2. get sorted digits array
  3. scan from right to left
    1. find next greater number of digits[i] in sorted
    2. new digits array
    3. if valid, this is the answer
    4. if not valid, digits[i] will be sorted[0]

Complexity

  • time: O(4 * 4) = O(1)
  • space: O(8) = O(1)

Solution 2

  1. boolean num[9]
  2. set digits from time in num true
  3. try min
    1. start from min + 1 to 60 if new min is from num, this is the answer
  4. try hour
    1. min is composed by smallest number in 4 digits
    2. start from hour + 1 to 24