Skip to content

Commit 5a2137d

Browse files
chore: add LeetCode daily solution
1 parent f87c4eb commit 5a2137d

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Closest Equal Element Queries (Medium)
2+
3+
**Problem ID:** 3488
4+
**Date:** 2026-04-16
5+
**Link:** https://leetcode.com/problems/closest-equal-element-queries/
6+
7+
## Approach
8+
9+
To solve the problem of finding the minimum distance between a queried index in a circular array and any other index with the same value, we can adopt a systematic approach that leverages efficient data structures and algorithms.
10+
11+
### Main Idea:
12+
The main idea is to preprocess the input array `nums` to store the indices of each unique value. This allows us to quickly retrieve all positions of a queried value and compute the minimum distance in constant time.
13+
14+
### Steps to Approach:
15+
16+
1. **Index Mapping**:
17+
- Create a dictionary (or hashmap) where the keys are the unique values from `nums`, and the values are lists of indices where each unique value occurs. This will help us quickly access all indices for any queried value.
18+
19+
2. **Circular Distance Calculation**:
20+
- For each query, retrieve the value at the queried index and check if it exists in the dictionary.
21+
- If the value is not present, append `-1` to the results.
22+
- If the value exists, compute the distances to all other indices where this value occurs. Since the array is circular, for each index `j`, the distance can be calculated as:
23+
- `distance = abs(i - j)` for linear distance, and
24+
- `circular_distance = len(nums) - distance` for the wrap-around distance.
25+
- The effective distance is the minimum of these two values.
26+
27+
3. **Efficient Calculation**:
28+
- Instead of iterating through all indices for each query, use the pre-stored indices to calculate the minimum distance efficiently. This can be done in linear time relative to the number of occurrences of the queried value.
29+
30+
### Data Structures:
31+
- A dictionary to map values to their indices.
32+
- An array to store the results for each query.
33+
34+
### Complexity:
35+
- **Time Complexity**:
36+
- Preprocessing the array `nums` takes O(n), where n is the length of `nums`.
37+
- Each query can be processed in O(k), where k is the number of occurrences of the queried value. In the worst case, if all values are the same, this could lead to O(n) for each query, but typically, k will be much smaller.
38+
- **Space Complexity**: O(n) for storing the indices in the dictionary.
39+
40+
This approach ensures that we efficiently handle multiple queries against the circular array while minimizing redundant calculations, making it suitable for the given constraints.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int[] closestEqualElementQueries(int[] nums, int[] queries) {
5+
Map<Integer, List<Integer>> indexMap = new HashMap<>();
6+
int n = nums.length;
7+
8+
// Populate the index map with positions of each number
9+
for (int i = 0; i < n; i++) {
10+
indexMap.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
11+
}
12+
13+
int[] answer = new int[queries.length];
14+
15+
for (int i = 0; i < queries.length; i++) {
16+
int queryIndex = queries[i];
17+
int value = nums[queryIndex];
18+
List<Integer> positions = indexMap.get(value);
19+
20+
if (positions.size() <= 1) {
21+
answer[i] = -1;
22+
continue;
23+
}
24+
25+
int minDistance = Integer.MAX_VALUE;
26+
for (int pos : positions) {
27+
if (pos != queryIndex) {
28+
int distance = Math.abs(pos - queryIndex);
29+
distance = Math.min(distance, n - distance); // Circular distance
30+
minDistance = Math.min(minDistance, distance);
31+
}
32+
}
33+
answer[i] = minDistance;
34+
}
35+
36+
return answer;
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var closestEqualElementQueries = function(nums, queries) {
2+
const n = nums.length;
3+
const indexMap = new Map();
4+
const answer = new Array(queries.length).fill(-1);
5+
6+
for (let i = 0; i < n; i++) {
7+
if (!indexMap.has(nums[i])) {
8+
indexMap.set(nums[i], []);
9+
}
10+
indexMap.get(nums[i]).push(i);
11+
}
12+
13+
for (let i = 0; i < queries.length; i++) {
14+
const queryIndex = queries[i];
15+
const value = nums[queryIndex];
16+
const indices = indexMap.get(value);
17+
18+
if (indices.length <= 1) continue;
19+
20+
let minDistance = Infinity;
21+
for (const index of indices) {
22+
if (index !== queryIndex) {
23+
const distance = Math.abs(index - queryIndex);
24+
const circularDistance = n - distance;
25+
minDistance = Math.min(minDistance, Math.min(distance, circularDistance));
26+
}
27+
}
28+
29+
answer[i] = minDistance === Infinity ? -1 : minDistance;
30+
}
31+
32+
return answer;
33+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def closestEqual(self, nums: List[int], queries: List[int]) -> List[int]:
3+
from collections import defaultdict
4+
5+
index_map = defaultdict(list)
6+
n = len(nums)
7+
8+
for i in range(n):
9+
index_map[nums[i]].append(i)
10+
11+
result = []
12+
13+
for query in queries:
14+
value = nums[query]
15+
indices = index_map[value]
16+
if len(indices) <= 1:
17+
result.append(-1)
18+
continue
19+
20+
min_distance = float('inf')
21+
for idx in indices:
22+
distance = abs(idx - query)
23+
circular_distance = n - distance
24+
min_distance = min(min_distance, min(distance, circular_distance))
25+
26+
result.append(min_distance)
27+
28+
return result

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
425425
- 2026-04-13 — [Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element/) (Easy) → `Easy/2026-04-13-1848-Minimum-Distance-to-the-Target-Element`
426426
- 2026-04-14 — [Minimum Total Distance Traveled](https://leetcode.com/problems/minimum-total-distance-traveled/) (Hard) → `Hard/2026-04-14-2463-Minimum-Total-Distance-Traveled`
427427
- 2026-04-15 — [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/) (Easy) → `Easy/2026-04-15-2515-Shortest-Distance-to-Target-String-in-a-Circular-Array`
428+
- 2026-04-16 — [Closest Equal Element Queries](https://leetcode.com/problems/closest-equal-element-queries/) (Medium) → `Medium/2026-04-16-3488-Closest-Equal-Element-Queries`

0 commit comments

Comments
 (0)