|
| 1 | +--- |
| 2 | +title: 3740.三个相等元素之间的最小距离 I:今日先暴力,"明日"再哈希 |
| 3 | +date: 2026-04-11 18:43:10 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 暴力, 模拟, 遍历] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3740.三个相等元素之间的最小距离 I:今日先暴力,"明日"再哈希 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/minimum-distance-between-three-equal-elements-i/](https://leetcode.cn/problems/minimum-distance-between-three-equal-elements-i/) |
| 11 | + |
| 12 | +<p>给你一个整数数组 <code>nums</code>。</p> |
| 13 | + |
| 14 | +<p>如果满足 <code>nums[i] == nums[j] == nums[k]</code>,且 <code>(i, j, k)</code> 是 3 个 <strong>不同 </strong>下标,那么三元组 <code>(i, j, k)</code> 被称为 <strong>有效三元组 </strong>。</p> |
| 15 | + |
| 16 | +<p><strong>有效三元组 </strong>的 <strong>距离 </strong>被定义为 <code>abs(i - j) + abs(j - k) + abs(k - i)</code>,其中 <code>abs(x)</code> 表示 <code>x</code> 的 <strong>绝对值 </strong>。</p> |
| 17 | + |
| 18 | +<p>返回一个整数,表示 <strong>有效三元组 </strong>的 <strong>最小 </strong>可能距离。如果不存在 <strong>有效三元组 </strong>,返回 <code>-1</code>。</p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | + |
| 22 | +<p><strong class="example">示例 1:</strong></p> |
| 23 | + |
| 24 | +<div class="example-block"> |
| 25 | +<p><strong>输入:</strong> <span class="example-io">nums = [1,2,1,1,3]</span></p> |
| 26 | + |
| 27 | +<p><strong>输出:</strong> <span class="example-io">6</span></p> |
| 28 | + |
| 29 | +<p><strong>解释:</strong></p> |
| 30 | + |
| 31 | +<p>最小距离对应的有效三元组是 <code>(0, 2, 3)</code> 。</p> |
| 32 | + |
| 33 | +<p><code>(0, 2, 3)</code> 是一个有效三元组,因为 <code>nums[0] == nums[2] == nums[3] == 1</code>。它的距离为 <code>abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6</code>。</p> |
| 34 | +</div> |
| 35 | + |
| 36 | +<p><strong class="example">示例 2:</strong></p> |
| 37 | + |
| 38 | +<div class="example-block"> |
| 39 | +<p><strong>输入:</strong> <span class="example-io">nums = [1,1,2,3,2,1,2]</span></p> |
| 40 | + |
| 41 | +<p><strong>输出:</strong> <span class="example-io">8</span></p> |
| 42 | + |
| 43 | +<p><strong>解释:</strong></p> |
| 44 | + |
| 45 | +<p>最小距离对应的有效三元组是 <code>(2, 4, 6)</code> 。</p> |
| 46 | + |
| 47 | +<p><code>(2, 4, 6)</code> 是一个有效三元组,因为 <code>nums[2] == nums[4] == nums[6] == 2</code>。它的距离为 <code>abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8</code>。</p> |
| 48 | +</div> |
| 49 | + |
| 50 | +<p><strong class="example">示例 3:</strong></p> |
| 51 | + |
| 52 | +<div class="example-block"> |
| 53 | +<p><strong>输入:</strong> <span class="example-io">nums = [1]</span></p> |
| 54 | + |
| 55 | +<p><strong>输出:</strong> <span class="example-io">-1</span></p> |
| 56 | + |
| 57 | +<p><strong>解释:</strong></p> |
| 58 | + |
| 59 | +<p>不存在有效三元组,因此答案为 -1。</p> |
| 60 | +</div> |
| 61 | + |
| 62 | +<p> </p> |
| 63 | + |
| 64 | +<p><strong>提示:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li><code>1 <= n == nums.length <= 100</code></li> |
| 68 | + <li><code>1 <= nums[i] <= n</code></li> |
| 69 | +</ul> |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +## 解题方法:暴力模拟 |
| 74 | + |
| 75 | +三层遍历,第一层使用$i$从$0$遍历到$n-1$,第二层使用$j$从$i+1$到$n-1$遍历,第三层使用$k$从$j+1$到$n-1$遍历。如果$nums[i]$、$nums[j]$、$nums[k]$都相等,则更新答案最小值。 |
| 76 | + |
| 77 | +Tips:由于遍历过程中$i$小于$j$小于$k$,所以有`abs(i - j) + abs(j - k) + abs(k - i) = 2 * (k - i)`。 |
| 78 | + |
| 79 | ++ 时间复杂度$O(n^3)$,其中$n=len(nums)$ |
| 80 | ++ 空间复杂度$O(1)$ |
| 81 | + |
| 82 | +### AC代码 |
| 83 | + |
| 84 | +#### C++ |
| 85 | + |
| 86 | +```cpp |
| 87 | +/* |
| 88 | + * @LastEditTime: 2026-04-10 23:25:26 |
| 89 | + */ |
| 90 | +class Solution { |
| 91 | +public: |
| 92 | + int minimumDistance(vector<int>& nums) { |
| 93 | + int ans = 201; |
| 94 | + for (int i = 0, n = nums.size(); i < n; i++) { |
| 95 | + for (int j = i + 1; j < n; j++) { |
| 96 | + if (nums[j] != nums[i]) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + for (int k = j + 1; k < n; k++) { |
| 100 | + if (nums[k] == nums[i]) { |
| 101 | + ans = min(ans, 2 * (k - i)); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return ans == 201 ? -1 : ans; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +``` |
| 111 | +
|
| 112 | +#### Python |
| 113 | +
|
| 114 | +```python |
| 115 | +''' |
| 116 | +LastEditTime: 2026-04-11 10:31:48 |
| 117 | +''' |
| 118 | +from typing import List |
| 119 | +
|
| 120 | +class Solution: |
| 121 | + def minimumDistance(self, nums: List[int]) -> int: |
| 122 | + ans = 201 |
| 123 | + for i, a in enumerate(nums): |
| 124 | + for j in range(i + 1, len(nums)): |
| 125 | + if a != nums[j]: |
| 126 | + continue |
| 127 | + for k in range(j + 1, len(nums)): |
| 128 | + if a == nums[k]: |
| 129 | + ans = min(ans, 2 * (k - i)) |
| 130 | + return -1 if ans == 201 else ans |
| 131 | +
|
| 132 | +``` |
| 133 | + |
| 134 | +> 今天要是就使用哈希表了,明天就没得写了[Doge]。 |
| 135 | + |
| 136 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/160058233)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/04/10/LeetCode%203740.%E4%B8%89%E4%B8%AA%E7%9B%B8%E7%AD%89%E5%85%83%E7%B4%A0%E4%B9%8B%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%9D%E7%A6%BBI/)哦~ |
| 137 | +> |
| 138 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments