Skip to content

Commit ab0c0b1

Browse files
committed
added problem 539
1 parent 2721a82 commit ab0c0b1

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

problems/problem0539/time_diff.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package problem0539
2+
3+
import (
4+
"sort"
5+
"strconv"
6+
)
7+
8+
/*
9+
Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
10+
*/
11+
12+
func findMinDifference(timePoints []string) int {
13+
var res int = 1440
14+
var cur, prev int
15+
sort.Slice(timePoints, func(i, j int) bool {
16+
return timePoints[i] < timePoints[j]
17+
})
18+
prev = getMinutes(timePoints[len(timePoints)-1]) - 1440
19+
for i := 0; i < len(timePoints); i++ {
20+
cur = getMinutes(timePoints[i])
21+
if diff := abs(cur, prev); diff < res {
22+
res = diff
23+
}
24+
prev = cur
25+
}
26+
return res
27+
}
28+
29+
func getMinutes(input string) int {
30+
hours, _ := strconv.Atoi(input[:2])
31+
minutes, _ := strconv.Atoi(input[3:])
32+
33+
return hours*60 + minutes
34+
}
35+
36+
func abs(a, b int) int {
37+
var res int = a - b
38+
if res < 0 {
39+
return res * -1
40+
}
41+
return res
42+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem0539
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []string
12+
Expected int
13+
}
14+
15+
var Results = []Result{
16+
{[]string{"23:59", "00:00"}, 1},
17+
{[]string{"00:00", "23:59", "00:00"}, 0},
18+
{[]string{"23:59", "01:00"}, 61},
19+
{[]string{"01:00", "02:34"}, 94},
20+
}
21+
22+
func TestMinimumTimeDifference(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
got := findMinDifference(res.Input)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Each problem is in it's own directory, with test files. There are helper package
106106
| 0473 | [Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square) | [My Solution](./problems/problem0473) ||
107107
| 0476 | [Number Complement](https://leetcode.com/problems/number-complement) | [My Solution](./problems/problem0476) ||
108108
| 0509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | [My Solution](./problems/problem0509) ||
109+
| 0539 | [Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference) | [My Solution](./problems/problem0539) ||
109110
| 0542 | [01 Matrix](https://leetcode.com/problems/01-matrix) | [My Solution](./problems/problem0542) ||
110111
| 0557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii) | [My Solution](./problems/problem0557) ||
111112
| 0567 | [Permutation in String](https://leetcode.com/problems/permutation-in-string) | [My Solution](./problems/problem0567) ||

0 commit comments

Comments
 (0)