Skip to content

Commit

Permalink
Merge pull request #994 from 0xff-dev/949
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 949
  • Loading branch information
6boris authored Oct 30, 2024
2 parents 4a3368d + c907b5b commit b7594f6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
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() {
}

0 comments on commit b7594f6

Please sign in to comment.