-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_time_difference.go
More file actions
62 lines (47 loc) · 1.08 KB
/
minimum_time_difference.go
File metadata and controls
62 lines (47 loc) · 1.08 KB
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
55
56
57
58
59
60
61
62
package goalgos
import (
"sort"
"strconv"
"strings"
)
func getClosest(times []int) int {
valueOne := times[0]
valueTwo := times[1]
minimumDiff := valueTwo - valueOne
for i := 1; i < len(times)-1; i++ {
if times[i+1]-times[i] < minimumDiff {
valueOne = times[i]
valueTwo = times[i+1]
minimumDiff = valueTwo - valueOne
}
}
result := valueTwo - valueOne
if 0 > result {
return -result
}
return result
}
func TimeDifference(n int, times string) int {
splitedTimes := strings.Split(times, " ")
convertedTimes := make([]int, 0, cap(splitedTimes))
for i := 0; i < n; i++ {
splitedHM := strings.Split(splitedTimes[i], ":")
convertedH, err := strconv.Atoi(splitedHM[0])
if err != nil {
return -1
}
convertedM, err := strconv.Atoi(splitedHM[1])
if err != nil {
return -1
}
if convertedH == 0 {
convertedH = 24
}
convertedTimes = append(convertedTimes, (convertedH*60)+convertedM)
}
sort.Slice(convertedTimes, func(i, j int) bool {
return convertedTimes[i] < convertedTimes[j]
})
getClosest := getClosest(convertedTimes)
return getClosest
}