leetcode

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

commit 785e23460e5e020b75b806f6a3d44ba5c870293a
parent 83b9b76710d7b75eb1ddb8d93996b16604f4517c
Author: Jack Sangdahl <jack@pngu.org>
Date:   Thu, 16 Apr 2026 20:59:04 -0600

3761. minimum absolute distance between mirror pairs

Diffstat:
Amin_abs_dist_mirror_pair.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amin_abs_dist_mirror_pair.py | 27+++++++++++++++++++++++++++
2 files changed, 126 insertions(+), 0 deletions(-)

diff --git a/min_abs_dist_mirror_pair.c b/min_abs_dist_mirror_pair.c @@ -0,0 +1,99 @@ +// 3761. Minimum Absolute Distance Between Mirror Pairs +#include "leetcode.h" + +/* + * you are given an integer array 'nums'. a mirror pair is a pair of indices + * '(i, j)' such that '0 <= i < j < nums.length' and 'reverse(nums[i]) == + * nums[j]'. return the minimum absolute distance between the indices of any + * mirror pair. the absolute distance between indices is 'i' and 'j' is 'abs(i - + * j)'. if no mirror pairs exists, return -1. + */ + +struct hash_item { + int key; + int val; + UT_hash_handle hh; +}; + +struct hash_item *hash_find_item(struct hash_item **obj, int key) { + struct hash_item *pEntry = NULL; + HASH_FIND_INT(*obj, &key, pEntry); + return pEntry; +} + +bool hash_add_item(struct hash_item **obj, int key, int val) { + if (hash_find_item(obj, key)) { + return false; + } + struct hash_item *pEntry = + (struct hash_item *)malloc(sizeof(struct hash_item)); + pEntry->key = key; + pEntry->val = val; + HASH_ADD_INT(*obj, key, pEntry); + return true; +} + +bool hash_set_item(struct hash_item **obj, int key, int val) { + struct hash_item *pEntry = hash_find_item(obj, key); + if (!pEntry) { + hash_add_item(obj, key, val); + } else { + pEntry->val = val; + } + return true; +} + +int hash_get_item(struct hash_item **obj, int key, int defaultVal) { + struct hash_item *pEntry = hash_find_item(obj, key); + if (!pEntry) { + return defaultVal; + } + return pEntry->val; +} + +void hash_free(struct hash_item **obj) { + struct hash_item *curr = NULL, *tmp = NULL; + HASH_ITER(hh, *obj, curr, tmp) { + HASH_DEL(*obj, curr); + free(curr); + } +} + +int reverse(int x) { + int y = 0; + while (x > 0) { + y = y * 10 + x % 10; + x /= 10; + } + return y; +} + +int minMirrorPairDistance(int *nums, int numsSize) { + struct hash_item *prev = NULL; + int ans = numsSize + 1; + for (int i = 0; i < numsSize; i++) { + int x = nums[i]; + int prevIndex = hash_get_item(&prev, x, -1); + if (prevIndex != -1) { + int distance = i - prevIndex; + if (distance < ans) + ans = distance; + } + hash_set_item(&prev, reverse(x), i); + } + hash_free(&prev); + return ans == numsSize + 1 ? -1 : ans; +} + +int main() { + int n1[] = {12, 21, 45, 33, 54}, n2[] = {120, 21}, n3[] = {21, 120}; + int r1 = minMirrorPairDistance(n1, ARRAY_SIZE(n1)); + int r2 = minMirrorPairDistance(n2, ARRAY_SIZE(n2)); + int r3 = minMirrorPairDistance(n3, ARRAY_SIZE(n3)); + printf("%d\n", r1); + assert(r1 == 1); + printf("%d\n", r2); + assert(r2 == 1); + printf("%d\n", r3); + assert(r3 == -1); +} diff --git a/min_abs_dist_mirror_pair.py b/min_abs_dist_mirror_pair.py @@ -0,0 +1,27 @@ +# 3761. Minimum Absolute Distance Between Mirror Pairs + +""" +you are given an integer array 'nums'. a mirror pair is a pair of indices +'(i, j)' such that '0 <= i < j < nums.length' and 'reverse(nums[i]) == +nums[j]'. return the minimum absolute distance between the indices of any +mirror pair. the absolute distance between indices is 'i' and 'j' is 'abs(i - +j)'. if no mirror pairs exists, return -1. +""" + + +class Solution: + def minMirrorPairDistance(self, nums: List[int]) -> int: + ans = 100000 + vis = {} + for i, j in enumerate(nums): + if j in vis: + ans = min(ans, i - vis[j]) + vis[int(str(j)[::-1])] = i + return -(ans == 100000) | ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.minMirrorPairDistance(nums=[12, 21, 45, 33, 54])) + print(obj.minMirrorPairDistance(nums=[120, 21])) + print(obj.minMirrorPairDistance(nums=[21, 120]))