Skip to content

Commit 47f4009

Browse files
chore: add LeetCode daily solution
1 parent 1462a2c commit 47f4009

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Minimum Distance Between Three Equal Elements II (Medium)
2+
3+
**Problem ID:** 3741
4+
**Date:** 2026-04-11
5+
**Link:** https://leetcode.com/problems/minimum-distance-between-three-equal-elements-ii/
6+
7+
## Approach
8+
9+
To solve the problem of finding the minimum distance between three equal elements in an array, we can adopt the following approach:
10+
11+
### Main Idea
12+
The goal is to find three distinct indices \(i\), \(j\), and \(k\) such that the elements at these indices are equal, and we want to minimize the distance defined as \( \text{abs}(i - j) + \text{abs}(j - k) + \text{abs}(k - i) \). This distance can be simplified to \( \text{max}(i, j, k) - \text{min}(i, j, k) \) multiplied by 2, since the absolute differences can be rearranged.
13+
14+
### Approach
15+
1. **Index Tracking**: Use a dictionary to map each unique number in the array to a list of its indices. This allows us to quickly access the positions of any number.
16+
17+
2. **Distance Calculation**: For each number that appears at least three times (since we need three indices), retrieve its list of indices. We can then calculate the distance for every triplet of indices using a sliding window approach:
18+
- For a list of indices, iterate through the list and for each triplet of indices \( (i, j, k) \), compute the distance using the formula mentioned above.
19+
- Since the indices are sorted, we can efficiently compute the distance by just considering the first three indices in the list and sliding through the list.
20+
21+
3. **Minimization**: Track the minimum distance encountered during the iteration. If no valid triplet is found, return -1.
22+
23+
### Data Structures
24+
- A dictionary (or hashmap) to store lists of indices for each unique number.
25+
- A variable to keep track of the minimum distance found.
26+
27+
### Complexity
28+
- **Time Complexity**: The overall time complexity is \(O(n)\) for traversing the array to populate the dictionary and \(O(k)\) for processing each unique number, where \(k\) is the number of unique elements. In the worst case, this is still linear relative to the size of the input.
29+
- **Space Complexity**: The space complexity is \(O(n)\) in the worst case, where all elements are unique and stored in the dictionary.
30+
31+
This approach is efficient and leverages the properties of indices and distances to find the solution without the need for a brute-force search, making it suitable for the problem's constraints.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int minimumDistance(int[] nums) {
5+
Map<Integer, List<Integer>> indexMap = new HashMap<>();
6+
7+
for (int i = 0; i < nums.length; i++) {
8+
indexMap.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
9+
}
10+
11+
int minDistance = Integer.MAX_VALUE;
12+
boolean found = false;
13+
14+
for (List<Integer> indices : indexMap.values()) {
15+
if (indices.size() >= 3) {
16+
for (int i = 0; i < indices.size(); i++) {
17+
for (int j = i + 1; j < indices.size(); j++) {
18+
for (int k = j + 1; k < indices.size(); k++) {
19+
int distance = Math.abs(indices.get(i) - indices.get(j)) +
20+
Math.abs(indices.get(j) - indices.get(k)) +
21+
Math.abs(indices.get(k) - indices.get(i));
22+
minDistance = Math.min(minDistance, distance);
23+
found = true;
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
return found ? minDistance : -1;
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var minimumDistance = function(nums) {
2+
const indices = new Map();
3+
let minDistance = Infinity;
4+
5+
for (let i = 0; i < nums.length; i++) {
6+
if (!indices.has(nums[i])) {
7+
indices.set(nums[i], []);
8+
}
9+
indices.get(nums[i]).push(i);
10+
}
11+
12+
for (const indexList of indices.values()) {
13+
if (indexList.length < 3) continue;
14+
15+
for (let i = 0; i < indexList.length - 2; i++) {
16+
for (let j = i + 1; j < indexList.length - 1; j++) {
17+
for (let k = j + 1; k < indexList.length; k++) {
18+
const distance = Math.abs(indexList[i] - indexList[j]) +
19+
Math.abs(indexList[j] - indexList[k]) +
20+
Math.abs(indexList[k] - indexList[i]);
21+
minDistance = Math.min(minDistance, distance);
22+
}
23+
}
24+
}
25+
}
26+
27+
return minDistance === Infinity ? -1 : minDistance;
28+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def minimumDistance(self, nums: List[int]) -> int:
3+
from collections import defaultdict
4+
indices = defaultdict(list)
5+
6+
for i, num in enumerate(nums):
7+
indices[num].append(i)
8+
9+
min_distance = float('inf')
10+
11+
for idx_list in indices.values():
12+
if len(idx_list) < 3:
13+
continue
14+
for i in range(len(idx_list) - 2):
15+
for j in range(i + 1, len(idx_list) - 1):
16+
for k in range(j + 1, len(idx_list)):
17+
dist = abs(idx_list[i] - idx_list[j]) + abs(idx_list[j] - idx_list[k]) + abs(idx_list[k] - idx_list[i])
18+
min_distance = min(min_distance, dist)
19+
20+
return min_distance if min_distance != float('inf') else -1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
420420
- 2026-04-08 — [XOR After Range Multiplication Queries I](https://leetcode.com/problems/xor-after-range-multiplication-queries-i/) (Medium) → `Medium/2026-04-08-3653-XOR-After-Range-Multiplication-Queries-I`
421421
- 2026-04-09 — [XOR After Range Multiplication Queries II](https://leetcode.com/problems/xor-after-range-multiplication-queries-ii/) (Hard) → `Hard/2026-04-09-3655-XOR-After-Range-Multiplication-Queries-II`
422422
- 2026-04-10 — [Minimum Distance Between Three Equal Elements I](https://leetcode.com/problems/minimum-distance-between-three-equal-elements-i/) (Easy) → `Easy/2026-04-10-3740-Minimum-Distance-Between-Three-Equal-Elements-I`
423+
- 2026-04-11 — [Minimum Distance Between Three Equal Elements II](https://leetcode.com/problems/minimum-distance-between-three-equal-elements-ii/) (Medium) → `Medium/2026-04-11-3741-Minimum-Distance-Between-Three-Equal-Elements-II`

0 commit comments

Comments
 (0)