Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 539 #629

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions leetcode/501-600/0539.Minimum-Time-Difference/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
44 changes: 42 additions & 2 deletions leetcode/501-600/0539.Minimum-Time-Difference/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 7 additions & 7 deletions leetcode/501-600/0539.Minimum-Time-Difference/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading