Skip to content

Commit 513138f

Browse files
chore: add LeetCode daily solution
1 parent d11f0b8 commit 513138f

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Count Special Triplets (Medium)
2+
3+
**Problem ID:** 3583
4+
**Date:** 2025-12-09
5+
**Link:** https://leetcode.com/problems/count-special-triplets/
6+
7+
## Approach
8+
9+
To solve the problem of counting special triplets in the given integer array `nums`, we can adopt a systematic approach that leverages counting and efficient iteration instead of brute force, which would be computationally expensive.
10+
11+
### Main Idea:
12+
The essence of the problem lies in identifying triplets `(i, j, k)` such that:
13+
1. `0 <= i < j < k < n`
14+
2. `nums[i] == nums[j] * 2`
15+
3. `nums[k] == nums[j] * 2`
16+
17+
Given that `nums[i]` and `nums[k]` must both equal `2 * nums[j]`, we can simplify our approach by focusing on the middle element `nums[j]` and counting how many valid `i` and `k` indices exist for each `j`.
18+
19+
### Approach:
20+
1. **Count Occurrences**: Use a frequency array to count occurrences of each possible number in `nums`. This helps in quickly determining how many valid `i` and `k` can be formed for each `j`.
21+
22+
2. **Iterate Through `j`**: For each index `j`, calculate the value `target = 2 * nums[j]`. This value represents the numbers we need to find for both `i` and `k`.
23+
24+
3. **Count Valid Indices**:
25+
- **For `i`**: Count how many indices `i` exist before `j` where `nums[i] == target`. This can be done by maintaining a cumulative count of occurrences as we iterate through the array.
26+
- **For `k`**: Count how many indices `k` exist after `j` where `nums[k] == target`. This can be achieved by using the frequency array and adjusting counts as we process each `j`.
27+
28+
4. **Calculate Triplets**: For each valid `j`, the number of special triplets can be calculated as the product of the counts of valid `i` and `k`. Sum these products for all valid `j` to get the final result.
29+
30+
5. **Modulo Operation**: Since the result can be large, ensure to take results modulo `10^9 + 7` at each step of accumulation.
31+
32+
### Data Structures:
33+
- A frequency array (or dictionary) to store counts of each number in `nums`.
34+
- A cumulative count array to keep track of how many times each number has been seen as we iterate through `j`.
35+
36+
### Complexity:
37+
- **Time Complexity**: O(n), where n is the length of `nums`. We make a single pass to build the frequency array and then another pass to count triplets.
38+
- **Space Complexity**: O(m), where m is the range of possible values in `nums` (up to 100,000), which is manageable within the problem constraints.
39+
40+
By following this structured approach, we efficiently count the special triplets without the need for nested loops, making the solution scalable for the upper limits of input size.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public int countTriplets(int[] nums) {
3+
int mod = 1000000007;
4+
int n = nums.length;
5+
long count = 0;
6+
7+
// Count occurrences of each number
8+
int[] freq = new int[100001];
9+
for (int num : nums) {
10+
freq[num]++;
11+
}
12+
13+
// Iterate through each possible value of nums[j]
14+
for (int j = 0; j < n; j++) {
15+
int numJ = nums[j];
16+
int numI = numJ * 2;
17+
int numK = numJ * 2;
18+
19+
// If nums[j] * 2 is within bounds
20+
if (numI < freq.length) {
21+
long countI = 0;
22+
long countK = 0;
23+
24+
// Count valid i's
25+
for (int i = 0; i < j; i++) {
26+
if (nums[i] == numI) {
27+
countI++;
28+
}
29+
}
30+
31+
// Count valid k's
32+
for (int k = j + 1; k < n; k++) {
33+
if (nums[k] == numK) {
34+
countK++;
35+
}
36+
}
37+
38+
count = (count + (countI * countK) % mod) % mod;
39+
}
40+
}
41+
42+
return (int) count;
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var countTriplets = function(nums) {
2+
const MOD = 1e9 + 7;
3+
const count = new Map();
4+
let result = 0;
5+
6+
for (const num of nums) {
7+
count.set(num, (count.get(num) || 0) + 1);
8+
}
9+
10+
for (let j = 0; j < nums.length; j++) {
11+
const numJ = nums[j];
12+
const numI = numJ * 2;
13+
const numK = numJ * 2;
14+
15+
if (count.has(numI) && count.has(numK)) {
16+
let countI = 0;
17+
let countK = 0;
18+
19+
for (let i = 0; i < j; i++) {
20+
if (nums[i] === numI) countI++;
21+
}
22+
23+
for (let k = j + 1; k < nums.length; k++) {
24+
if (nums[k] === numK) countK++;
25+
}
26+
27+
result = (result + countI * countK) % MOD;
28+
}
29+
}
30+
31+
return result;
32+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def countTriplets(self, nums: List[int]) -> int:
3+
count = 0
4+
mod = 10**9 + 7
5+
n = len(nums)
6+
7+
# Count occurrences of each number
8+
freq = Counter(nums)
9+
10+
for j in range(n):
11+
if nums[j] * 2 in freq:
12+
count += freq[nums[j] * 2] * (j - sum(1 for i in range(j) if nums[i] == nums[j] * 2))
13+
14+
return count % mod

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
299299
- 2025-12-06 — [Count Partitions With Max-Min Difference at Most K](https://leetcode.com/problems/count-partitions-with-max-min-difference-at-most-k/) (Medium) → `Medium/2025-12-06-3578-Count-Partitions-With-Max-Min-Difference-at-Most-K`
300300
- 2025-12-07 — [Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/) (Easy) → `Easy/2025-12-07-1523-Count-Odd-Numbers-in-an-Interval-Range`
301301
- 2025-12-08 — [Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/) (Easy) → `Easy/2025-12-08-1925-Count-Square-Sum-Triples`
302+
- 2025-12-09 — [Count Special Triplets](https://leetcode.com/problems/count-special-triplets/) (Medium) → `Medium/2025-12-09-3583-Count-Special-Triplets`

0 commit comments

Comments
 (0)