Skip to content

Commit 1260f9d

Browse files
author
Partho Biswas
committed
681. Next Closest Time
1 parent 303eb95 commit 1260f9d

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ I have solved quite a number of problems from several topics. See the below tabl
202202
|25| [551. Student Attendance Record I](https://tinyurl.com/qu89xt8)| [Python](https://tinyurl.com/wu6rdaw/551_Student_Attendance_Record_I.py)| | Easy | --- |
203203
|26| [242. Valid Anagram](https://tinyurl.com/yguqjw2g)| [Python](https://tinyurl.com/wu6rdaw/242_Valid_Anagram.py), [Swift](https://tinyurl.com/wuja3c4/242_Valid_Anagram.swift) | | Easy | --- |
204204
|27| [1358. Number of Substrings Containing All Three Characters](https://tinyurl.com/ufhkahy)| [Python](https://tinyurl.com/wu6rdaw/1358_Number_of_Substrings_Containing_All_Three_Characters.py), [Swift](https://tinyurl.com/wuja3c4/1358_Number_of_Substrings_Containing_All_Three_Characters.swift) | [Art 1](https://tinyurl.com/sespxql) | Medium | Sliding Window |
205-
|28| **[459. Repeated Substring Pattern](https://tinyurl.com/tg4sjty)** | [Python](https://tinyurl.com/wu6rdaw/459_Repeated_Substring_Pattern.py), [Swift](https://tinyurl.com/wuja3c4/459_Repeated_Substring_Pattern.swift) | [Art 1](https://tinyurl.com/rrqo8rj) | Easy | Interesting problem |
205+
|28| **[459. Repeated Substring Pattern](https://tinyurl.com/tg4sjty)** | [Python](https://tinyurl.com/wu6rdaw/459_Repeated_Substring_Pattern.py), [Swift](https://tinyurl.com/wuja3c4/459_Repeated_Substring_Pattern.swift) | [Art 1](https://tinyurl.com/rrqo8rj) | Easy | I fucking love this problem |
206+
|29| **[681. Next Closest Time](https://tinyurl.com/rdz29j6)** | [Python](https://tinyurl.com/wu6rdaw/681_Next_Closest_Time.py), [Swift](https://tinyurl.com/wuja3c4/681_Next_Closest_Time.swift) | [Art 1](https://tinyurl.com/vupwnhw) | Medium | I feel stupid after trying my first solution |
206207

207208

208209
</p>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import bisect
2+
3+
# My initial solution. Not correct solution
4+
class Solution(object):
5+
def nextClosestTime(self, time):
6+
"""
7+
:type time: str
8+
:rtype: str
9+
"""
10+
digits = list(time)
11+
digits.pop(2)
12+
sortedDigits = sorted(digits)
13+
for idx in range(len(digits) - 1, -1, -1):
14+
digit = digits[idx]
15+
nextPossibleDigitIdx = bisect.bisect_right(sortedDigits, digit)
16+
if nextPossibleDigitIdx >= len(digits):
17+
continue
18+
if idx == 3:
19+
digits[3] = sortedDigits[nextPossibleDigitIdx]
20+
break
21+
elif idx == 2 and int(sortedDigits[nextPossibleDigitIdx]) < 6:
22+
digits[2] = sortedDigits[nextPossibleDigitIdx]
23+
break
24+
elif idx == 1:
25+
if int(digits[0]) < 2:
26+
digits[1] = sortedDigits[nextPossibleDigitIdx]
27+
break
28+
elif int(digits[0]) == 2 and int(sortedDigits[nextPossibleDigitIdx]) < 4:
29+
digits[1] = sortedDigits[nextPossibleDigitIdx]
30+
break
31+
elif idx == 0:
32+
if int(sortedDigits[nextPossibleDigitIdx]) < 3:
33+
digits[0] = sortedDigits[nextPossibleDigitIdx]
34+
break
35+
else:
36+
digits[1] = digits[0]
37+
digits[2] = digits[0]
38+
digits[3] = digits[0]
39+
hours = digits[0:2]
40+
minuites = digits[2:]
41+
return "".join(hours) + ":" + "".join(minuites)
42+
43+
44+
# https://tinyurl.com/vupwnhw
45+
class Solution(object):
46+
def nextClosestTime(self, time):
47+
"""
48+
:type time: str
49+
:rtype: str
50+
"""
51+
hour, minuite = time.split(":")
52+
53+
# Generate all possible 2 digit values
54+
# There are at most 16 sorted values here
55+
digits = sorted(set(hour + minuite))
56+
twoDigitValues = [a+b for a in digits for b in digits]
57+
58+
# Check if the next valid minute is within the hour
59+
minuiteIndex = twoDigitValues.index(minuite)
60+
if minuiteIndex + 1 < len(twoDigitValues) and twoDigitValues[minuiteIndex + 1] < "60":
61+
return hour + ":" + twoDigitValues[minuiteIndex + 1]
62+
63+
# Check if the next valid hour is within the day
64+
hourIndex = twoDigitValues.index(hour)
65+
if hourIndex + 1 < len(twoDigitValues) and twoDigitValues[hourIndex + 1] < "24":
66+
return twoDigitValues[hourIndex + 1] + ":" + twoDigitValues[0]
67+
68+
# Return the earliest time of the next day
69+
return twoDigitValues[0] + ":" + twoDigitValues[0]

0 commit comments

Comments
 (0)