Skip to content

Commit 77786c2

Browse files
Sync LeetCode submission Runtime - 347 ms (73.69%), Memory - 347.2 MB (40.20%)
1 parent 80adc1c commit 77786c2

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<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>&nbsp;</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>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>1 &lt;= nums[i] &lt;= n</code></li>
56+
</ul>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int minimumDistance(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<vector<int>> mp(n+1);
6+
for (int i=0; i<n; i++) mp[nums[i]].push_back(i);
7+
int ans = 1e9;
8+
for (vector<int> x : mp) {
9+
int m = x.size();
10+
for (int i=0; i+2<m; i++) ans = min(ans, abs(x[i] - x[i+1]) + abs(x[i] - x[i+2]) + abs(x[i+1] - x[i+2]));
11+
}
12+
return ans < 1e9 ? ans : -1;
13+
}
14+
};

0 commit comments

Comments
 (0)