Skip to content
Open
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
66 changes: 54 additions & 12 deletions leetcode/3501-3600/3583.Count-Special-Triplets/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
# [3583.Count Special Triplets][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
You are given an integer array `nums`.

A **special triplet** is defined as a triplet of indices `(i, j, k)` such that:

- `0 <= i < j < k < n`, where `n = nums.length`
- `nums[i] == nums[j] * 2`
- `nums[k] == nums[j] * 2`

Return the total number of **special triplets** in the array.

Since the answer may be large, return it **modulo** `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [6,3,6]

Output: 1

Explanation:

The only special triplet is (i, j, k) = (0, 1, 2), where:

nums[0] = 6, nums[1] = 3, nums[2] = 6
nums[0] = nums[1] * 2 = 3 * 2 = 6
nums[2] = nums[1] * 2 = 3 * 2 = 6
```

**Example 2:**

```
Input: nums = [0,1,0,0]

Output: 1

Explanation:

The only special triplet is (i, j, k) = (0, 2, 3), where:

## 题意
> ...
nums[0] = 0, nums[2] = 0, nums[3] = 0
nums[0] = nums[2] * 2 = 0 * 2 = 0
nums[3] = nums[2] * 2 = 0 * 2 = 0
```

## 题解
**Example 3:**

### 思路1
> ...
Count Special Triplets
```go
```
Input: nums = [8,4,2,8,4]

Output: 2

Explanation:

There are exactly two special triplets:

(i, j, k) = (0, 1, 3)
nums[0] = 8, nums[1] = 4, nums[3] = 8
nums[0] = nums[1] * 2 = 4 * 2 = 8
nums[3] = nums[1] * 2 = 4 * 2 = 8
(i, j, k) = (1, 2, 4)
nums[1] = 4, nums[2] = 2, nums[4] = 4
nums[1] = nums[2] * 2 = 2 * 2 = 4
nums[4] = nums[2] * 2 = 2 * 2 = 4
```

## 结语

Expand Down
22 changes: 20 additions & 2 deletions leetcode/3501-3600/3583.Count-Special-Triplets/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
const MOD = 1000000007
numCnt := make(map[int]int)
numPartialCnt := make(map[int]int)

for _, v := range nums {
numCnt[v]++
}

ans := 0
for _, v := range nums {
target := v * 2
lCnt := numPartialCnt[target]
numPartialCnt[v]++
rCnt := numCnt[target] - numPartialCnt[target]

ans = (ans + lCnt*rCnt) % MOD
}

return ans
}
14 changes: 7 additions & 7 deletions leetcode/3501-3600/3583.Count-Special-Triplets/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 []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{6, 3, 6}, 1},
{"TestCase2", []int{0, 1, 0, 0}, 1},
{"TestCase3", []int{8, 4, 2, 8, 4}, 2},
}

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

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

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