|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-distance-between-three-equal-elements-i">4115. Minimum Distance Between Three Equal Elements I</a></h2><h3>Easy</h3><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>A tuple <code>(i, j, k)</code> of 3 <strong>distinct</strong> indices is <strong>good</strong> if <code>nums[i] == nums[j] == nums[k]</code>.</p> |
| 4 | + |
| 5 | +<p>The <strong>distance</strong> of a <strong>good</strong> tuple is <code>abs(i - j) + abs(j - k) + abs(k - i)</code>, where <code>abs(x)</code> denotes the <strong>absolute value</strong> of <code>x</code>.</p> |
| 6 | + |
| 7 | +<p>Return an integer denoting the <strong>minimum</strong> possible <strong>distance</strong> of a <strong>good</strong> tuple. If no <strong>good</strong> tuples exist, return <code>-1</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">6</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<p>The minimum distance is achieved by the good tuple <code>(0, 2, 3)</code>.</p> |
| 20 | + |
| 21 | +<p><code>(0, 2, 3)</code> is a good tuple because <code>nums[0] == nums[2] == nums[3] == 1</code>. Its distance is <code>abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6</code>.</p> |
| 22 | +</div> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,3,2,1,2]</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">8</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<p>The minimum distance is achieved by the good tuple <code>(2, 4, 6)</code>.</p> |
| 34 | + |
| 35 | +<p><code>(2, 4, 6)</code> is a good tuple because <code>nums[2] == nums[4] == nums[6] == 2</code>. Its distance is <code>abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8</code>.</p> |
| 36 | +</div> |
| 37 | + |
| 38 | +<p><strong class="example">Example 3:</strong></p> |
| 39 | + |
| 40 | +<div class="example-block"> |
| 41 | +<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 44 | + |
| 45 | +<p><strong>Explanation:</strong></p> |
| 46 | + |
| 47 | +<p>There are no good tuples. Therefore, the answer is -1.</p> |
| 48 | +</div> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= n == nums.length <= 100</code></li> |
| 55 | + <li><code>1 <= nums[i] <= n</code></li> |
| 56 | +</ul> |
0 commit comments