-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0681-NextClosestTime.cs
54 lines (48 loc) · 1.98 KB
/
0681-NextClosestTime.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0681_NextClosestTime
{
public string NextClosestTime(string time)
{
var startTime = 60 * int.Parse(time.Substring(0, 2));
startTime += int.Parse(time.Substring(3));
var allowedCharacters = new HashSet<int>();
foreach (var ch in time)
if (ch != ':') allowedCharacters.Add(ch - '0');
const int ONE_DAY = 24 * 60;
var result_hour = 0;
var result_minute = 0;
var offset = int.MaxValue;
foreach (var h1 in allowedCharacters)
foreach (var h2 in allowedCharacters)
{
var hour = h1 * 10 + h2;
if (hour < 24)
foreach (var m1 in allowedCharacters)
foreach (var m2 in allowedCharacters)
{
var minute = m1 * 10 + m2;
if (minute < 60)
{
int currentTime = 60 * hour + minute;
if (currentTime <= startTime) currentTime += ONE_DAY;
var new_offset = currentTime - startTime;
if (new_offset < offset)
{
offset = new_offset;
result_hour = hour;
result_minute = minute;
}
}
}
}
return $"{result_hour:D2}:{result_minute:D2}";
}
}
}