3741. Minimum Distance Between Three Equal Elements II Medium

@problem@discussion
#Array#Hash Table



1/**
2 * [3741] Minimum Distance Between Three Equal Elements II
3 *
4 * You are given an integer array nums.
5 * A tuple (i, j, k) of 3 distinct indices is good if nums[i] == nums[j] == nums[k].
6 * The distance of a good tuple is abs(i - j) + abs(j - k) + abs(k - i), where abs(x) denotes the absolute value of x.
7 * Return an integer denoting the minimum possible distance of a good tuple. If no good tuples exist, return -1.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [1,2,1,1,3]</span>
12 * Output: <span class="example-io">6</span>
13 * Explanation:
14 * The minimum distance is achieved by the good tuple (0, 2, 3).
15 * (0, 2, 3) is a good tuple because nums[0] == nums[2] == nums[3] == 1. Its distance is abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">nums = [1,1,2,3,2,1,2]</span>
20 * Output: <span class="example-io">8</span>
21 * Explanation:
22 * The minimum distance is achieved by the good tuple (2, 4, 6).
23 * (2, 4, 6) is a good tuple because nums[2] == nums[4] == nums[6] == 2. Its distance is abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8.
24 * </div>
25 * <strong class="example">Example 3:
26 * <div class="example-block">
27 * Input: <span class="example-io">nums = [1]</span>
28 * Output: <span class="example-io">-1</span>
29 * Explanation:
30 * There are no good tuples. Therefore, the answer is -1.
31 * </div>
32 *  
33 * Constraints:
34 * 
35 * 	1 <= n == nums.length <= 10^5
36 * 	1 <= nums[i] <= n
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/minimum-distance-between-three-equal-elements-ii/
42// discuss: https://leetcode.com/problems/minimum-distance-between-three-equal-elements-ii/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn minimum_distance(nums: Vec<i32>) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_3741() {
60    }
61}
62

Back
© 2026 bowen.ge All Rights Reserved.