8000 :sparkles: Added solution to 1395 · ninefive/awesome-golang-leetcode@61106b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61106b8

Browse files
author
Sathish Babu
committed
✨ Added solution to 1395
1 parent 38fbc51 commit 61106b8

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# [1362.Closest Divisors][title]
2+
3+
> [!WARNING|style:flat]
4+
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-leetcode)
5+
6+
## Description
7+
8+
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
9+
10+
You have to form a team of 3 soldiers amongst them under the following rules:
11+
12+
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
13+
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
14+
15+
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
16+
17+
**Example 1:**
18+
19+
```
20+
Input: rating = [2,5,3,4,1]
21+
Output: 3
22+
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: rating = [2,1,3]
29+
Output: 0
30+
Explanation: We can't form any team given the conditions.
31+
```
32+
33+
**Example 3:**
34+
35+
```
36+
Input: rating = [1,2,3,4]
37+
Output: 4
38+
```
39+
40+
## 题意
41+
> ...
42+
43+
## 题解
44+
45+
### 思路1
46+
> ...
47+
Closest Divisors
48+
```go
49+
```
50+
51+
52+
## 结语
53+
54+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
55+
56+
[title]: https://leetcode.com/problems/closest-divisors/
57+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Solution
2+
3+
func Solution(rating []int) int {
4+
ans, n := 0, len(rating)
5+
dpG, dpL := make([]int, n), make([]int, n)
6+
for i, r := range rating {
7+
for j := 0; j < i; j++ {
8+
if r > rating[j] {
9+
dpG[i]++
10+
ans += dpG[j]
11+
} else if r < rating[j] {
12+
dpL[i]++
13+
ans += dpL[j]
14+
}
15+
}
16+
}
17+
return ans
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs []int
14+
expect int
15+
}{
16+
{"TestCase", []int{2, 5, 3, 4, 1}, 3},
17+
{"TestCase", []int{2, 1, 4}, 0},
18+
{"TestCase", []int{1, 2, 3, 4}, 4},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := Solution(c.inputs)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27+
c.expect, got, c.inputs)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
36+
}
37+
38+
// 使用案列
39+
func ExampleSolution() {
40+
41+
}

0 commit comments

Comments
 (0)
0