Skip to content

Commit 7ff3a17

Browse files
committed
Add solution and test-cases for problem 3583
1 parent 7ce34f6 commit 7ff3a17

File tree

3 files changed

+81
-21
lines changed

3 files changed

+81
-21
lines changed

leetcode/3501-3600/3583.Count-Special-Triplets/README.md

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
11
# [3583.Count Special Triplets][title]
22

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-algorithm)
5-
63
## Description
4+
You are given an integer array `nums`.
5+
6+
A **special triplet** is defined as a triplet of indices `(i, j, k)` such that:
7+
8+
- `0 <= i < j < k < n`, where `n = nums.length`
9+
- `nums[i] == nums[j] * 2`
10+
- `nums[k] == nums[j] * 2`
11+
12+
Return the total number of **special triplets** in the array.
13+
14+
Since the answer may be large, return it **modulo** `10^9 + 7`.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: nums = [6,3,6]
20+
21+
Output: 1
22+
23+
Explanation:
24+
25+
The only special triplet is (i, j, k) = (0, 1, 2), where:
26+
27+
nums[0] = 6, nums[1] = 3, nums[2] = 6
28+
nums[0] = nums[1] * 2 = 3 * 2 = 6
29+
nums[2] = nums[1] * 2 = 3 * 2 = 6
30+
```
31+
32+
**Example 2:**
33+
1334
```
35+
Input: nums = [0,1,0,0]
36+
37+
Output: 1
38+
39+
Explanation:
40+
41+
The only special triplet is (i, j, k) = (0, 2, 3), where:
1442
15-
## 题意
16-
> ...
43+
nums[0] = 0, nums[2] = 0, nums[3] = 0
44+
nums[0] = nums[2] * 2 = 0 * 2 = 0
45+
nums[3] = nums[2] * 2 = 0 * 2 = 0
46+
```
1747

18-
## 题解
48+
**Example 3:**
1949

20-
### 思路1
21-
> ...
22-
Count Special Triplets
23-
```go
2450
```
51+
Input: nums = [8,4,2,8,4]
52+
53+
Output: 2
54+
55+
Explanation:
2556
57+
There are exactly two special triplets:
58+
59+
(i, j, k) = (0, 1, 3)
60+
nums[0] = 8, nums[1] = 4, nums[3] = 8
61+
nums[0] = nums[1] * 2 = 4 * 2 = 8
62+
nums[3] = nums[1] * 2 = 4 * 2 = 8
63+
(i, j, k) = (1, 2, 4)
64+
nums[1] = 4, nums[2] = 2, nums[4] = 4
65+
nums[1] = nums[2] * 2 = 2 * 2 = 4
66+
nums[4] = nums[2] * 2 = 2 * 2 = 4
67+
```
2668

2769
## 结语
2870

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
const MOD = 1000000007
5+
numCnt := make(map[int]int)
6+
numPartialCnt := make(map[int]int)
7+
8+
for _, v := range nums {
9+
numCnt[v]++
10+
}
11+
12+
ans := 0
13+
for _, v := range nums {
14+
target := v * 2
15+
lCnt := numPartialCnt[target]
16+
numPartialCnt[v]++
17+
rCnt := numCnt[target] - numPartialCnt[target]
18+
19+
ans = (ans + lCnt*rCnt) % MOD
20+
}
21+
22+
return ans
523
}

leetcode/3501-3600/3583.Count-Special-Triplets/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{6, 3, 6}, 1},
17+
{"TestCase2", []int{0, 1, 0, 0}, 1},
18+
{"TestCase3", []int{8, 4, 2, 8, 4}, 2},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)