From cb26c275d9d46b5218e0ec43984aa3e6d9e80b3c Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 18 Sep 2023 22:43:11 +0800 Subject: [PATCH] Add solution and test-cases for problem 539 --- .../0539.Minimum-Time-Difference/README.md | 23 ++++------ .../0539.Minimum-Time-Difference/Solution.go | 44 ++++++++++++++++++- .../Solution_test.go | 14 +++--- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/leetcode/501-600/0539.Minimum-Time-Difference/README.md b/leetcode/501-600/0539.Minimum-Time-Difference/README.md index 405876a3b..e357f899b 100644 --- a/leetcode/501-600/0539.Minimum-Time-Difference/README.md +++ b/leetcode/501-600/0539.Minimum-Time-Difference/README.md @@ -1,28 +1,21 @@ # [539.Minimum Time Difference][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description - +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. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: timePoints = ["23:59","00:00"] +Output: 1 ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Minimum Time Difference -```go ``` - +Input: timePoints = ["00:00","23:59","00:00"] +Output: 0 +``` ## 结语 diff --git a/leetcode/501-600/0539.Minimum-Time-Difference/Solution.go b/leetcode/501-600/0539.Minimum-Time-Difference/Solution.go index d115ccf5e..df30fc047 100644 --- a/leetcode/501-600/0539.Minimum-Time-Difference/Solution.go +++ b/leetcode/501-600/0539.Minimum-Time-Difference/Solution.go @@ -1,5 +1,45 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "fmt" + "sort" +) + +func Solution(timePoints []string) int { + hm := make([][]int, len(timePoints)) + var a, b int + for idx, tp := range timePoints { + fmt.Sscanf(tp, "%d:%d", &a, &b) + if a == 0 { + a = 24 + } + hm[idx] = []int{a, b} + } + // "12:12","00:13" + sort.Slice(hm, func(i, j int) bool { + if hm[i][0] == hm[j][0] { + return hm[i][1] > hm[j][1] + } + return hm[i][0] > hm[j][0] + }) + ans := -1 + l := len(hm) + + for idx := 0; idx < l; idx++ { + next := (idx + 1) % l + diffh := hm[idx][0] - hm[next][0] + diffm := hm[idx][1] - hm[next][1] + r := diffh*60 + diffm + if r < 0 { + r = -r + } + if x := 1440 - r; x < r { + r = x + } + if ans == -1 || r < ans { + ans = r + } + } + + return ans } diff --git a/leetcode/501-600/0539.Minimum-Time-Difference/Solution_test.go b/leetcode/501-600/0539.Minimum-Time-Difference/Solution_test.go index 14ff50eb4..764d20a26 100644 --- a/leetcode/501-600/0539.Minimum-Time-Difference/Solution_test.go +++ b/leetcode/501-600/0539.Minimum-Time-Difference/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"23:59", "00:00"}, 1}, + {"TestCase2", []string{"01:39", "10:26", "21:43"}, 236}, + {"TestCase3", []string{"00:00", "23:59", "00:00"}, 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }