leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit a3293e7b8ec4cb65724351b4ccba5dfeeaf5f46c
parent 8dd4c7adb21c48504df2ab1cc5847d311616d10d
Author: Jack Sangdahl <jack@pngu.org>
Date:   Fri, 10 Apr 2026 19:56:53 -0600

3741. minimum distance between three equal elements 2

Diffstat:
Amin_dist_betw_3eq_elem2.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amin_dist_betw_3eq_elem2.py | 27+++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/min_dist_betw_3eq_elem2.c b/min_dist_betw_3eq_elem2.c @@ -0,0 +1,57 @@ +// 3741. Minimum Distance Between Three Equal Elements II +#include "leetcode.h" + +/* + * you are given an integer array 'nums'. a tuple '(i, j, k)' of 3 distinct + * indices is good if 'nums[i] == nums[j] == nums[k]'. the distance of a good + * tuple is 'abs(i - j) + abs(j - k) + abs(k - i)' where 'abs(x)' is the + * absolute value of 'x'. return an integer denoting the minimum possible + * distance of a good tuple. if no good tuples exist, return -1. + */ + +struct h_elem { + int key; + int val; + UT_hash_handle hh; +}; + +int minimumDistance(int *nums, int numsSize) { + int ans = INT_MAX; + int *next = (int *)malloc(numsSize * sizeof(int)); + memset(next, -1, numsSize * sizeof(int)); + struct h_elem *map = NULL, *tmp; + for (int i = numsSize - 1; i >= 0; i--) { + HASH_FIND_INT(map, &nums[i], tmp); + if (tmp) + next[i] = tmp->val; + else { + tmp = (struct h_elem *)malloc(sizeof(struct h_elem)); + tmp->key = nums[i]; + tmp->val = -1; + HASH_ADD_INT(map, key, tmp); + } + tmp->val = i; + } + for (int i = 0; i < numsSize; i++) { + int second = next[i]; + if (second != -1) { + int third = next[second]; + if (third != -1) + ans = third - i; + } + } + return ans == INT_MAX ? -1 : ans * 2; +} + +int main() { + int n1[] = {1, 2, 1, 1, 3}, n2[] = {1, 1, 2, 3, 2, 1, 2}, n3[] = {1}; + int r1 = minimumDistance(n1, ARRAY_SIZE(n1)); + int r2 = minimumDistance(n2, ARRAY_SIZE(n2)); + int r3 = minimumDistance(n3, ARRAY_SIZE(n3)); + printf("%d\n", r1); + assert(r1 == 6); + printf("%d\n", r2); + assert(r2 == 8); + printf("%d\n", r3); + assert(r3 == -1); +} diff --git a/min_dist_betw_3eq_elem2.py b/min_dist_betw_3eq_elem2.py @@ -0,0 +1,27 @@ +# 3741. Minimum Distance Between Three Equal Elements II +from typing import List + +""" +you are given an integer array 'nums'. a tuple '(i, j, k)' of 3 distinct +indices is good if 'nums[i] == nums[j] == nums[k]'. the distance of a good +tuple is 'abs(i - j) + abs(j - k) + abs(k - i)' where 'abs(x)' is the +absolute value of 'x'. return an integer denoting the minimum possible +distance of a good tuple. if no good tuples exist, return -1. +""" + + +class Solution: + def minimumDistance(self, nums: List[int]) -> int: + ans = float("inf") + nums = sorted((nums[i], i) for i in range(len(nums))) + for i in range(len(nums) - 2): + if nums[i][0] == nums[i + 2][0]: + ans = min(ans, (nums[i + 2][1] - nums[i][1]) * 2) + return ans if ans < float("inf") else -1 + + +if __name__ == "__main__": + obj = Solution() + print(obj.minimumDistance(nums=[1, 2, 1, 1, 3])) + print(obj.minimumDistance(nums=[1, 1, 2, 3, 2, 1, 2])) + print(obj.minimumDistance(nums=[1]))