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 949 #994

Merged
merged 1 commit into from
Oct 30, 2024
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
27 changes: 13 additions & 14 deletions leetcode/901-1000/0949.Largest-Time-for-Given-Digits/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [949.Largest Time for Given Digits][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 an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit **exactly once**.

24-hour times are formatted as `"HH:MM"`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`.

Return the latest 24-hour time in `"HH:MM"` format. If no valid time can be made, return an empty string.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: arr = [1,2,3,4]
Output: "23:41"
Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Largest Time for Given Digits
```go
```

Input: arr = [5,5,5,5]
Output: ""
Explanation: There are no valid 24-hour times as "55:55" is not valid.
```

## 结语

Expand Down
36 changes: 34 additions & 2 deletions leetcode/901-1000/0949.Largest-Time-for-Given-Digits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package Solution

func Solution(x bool) bool {
return x
import "fmt"

func Solution(arr []int) string {
in := make(map[int]int)
for _, n := range arr {
in[n]++
}
var a, b, c, d int
for i := 23; i >= 0; i-- {
a, b = i/10, i%10
if a == b {
if in[a] < 2 {
continue
}
} else if in[a] < 1 || in[b] < 1 {
continue
}
in[a]--
in[b]--

for j := 59; j >= 0; j-- {
c, d = j/10, j%10
if c == d {
if in[c] >= 2 {
return fmt.Sprintf("%d%d:%d%d", a, b, c, d)
}
} else if in[c] > 0 && in[d] > 0 {
return fmt.Sprintf("%d%d:%d%d", a, b, c, d)
}
}
in[a]++
in[b]++
}
return ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 3, 4}, "23:41"},
{"TestCase2", []int{5, 5, 5, 5}, ""},
}

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

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

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