leetcode

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

commit e7fdd7d96208bf27d83bbedf8d8d3fba13274e2f
parent 31e66beff952415ea1327954d90b45afabe6438e
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sat, 18 Apr 2026 19:24:04 -0600

1855. maximum distance between a pair of values

Diffstat:
Amax_dist_between_val_pair.c | 36++++++++++++++++++++++++++++++++++++
Amax_dist_between_val_pair.py | 28++++++++++++++++++++++++++++
2 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/max_dist_between_val_pair.c b/max_dist_between_val_pair.c @@ -0,0 +1,36 @@ +// 1855. Maximum Distance Between a Pair of Values +#include "leetcode.h" + +/* + * you are given two non-increasing 0-indexed integer arrays 'nums1' and + * 'nums2'. a pair of indices '(i, j)' where '0 <= i < nums1.lenth' and '0 <= j + * < nums2.lenth' is valid if both 'i <= j' and 'nums1[i] <= nums2[j]'. the + * distance of the pair is 'j - i'. return the maximum distance of any valid + * pair '(i, j)'. if there are no valid pairs, return 0. + */ + +int maxDistance(int *nums1, int nums1Size, int *nums2, int nums2Size) { + int i = 0, j = 0, ans = 0; + while (i < nums1Size && j < nums2Size) { + if (nums1[i] > nums2[j]) + ++i; + else + ans = fmax(ans, j++ - i); + } + return ans; +} + +int main() { + int n11[] = {55, 30, 5, 4, 2}, n21[] = {100, 20, 10, 10, 5}; + int n12[] = {2, 2, 2}, n22[] = {10, 10, 1}; + int n13[] = {30, 29, 19, 5}, n23[] = {25, 25, 25, 25, 25}; + int r1 = maxDistance(n11, ARRAY_SIZE(n11), n21, ARRAY_SIZE(n21)); + int r2 = maxDistance(n12, ARRAY_SIZE(n12), n22, ARRAY_SIZE(n22)); + int r3 = maxDistance(n13, ARRAY_SIZE(n13), n23, ARRAY_SIZE(n23)); + printf("%d\n", r1); + assert(r1 == 2); + printf("%d\n", r2); + assert(r2 == 1); + printf("%d\n", r3); + assert(r3 == 2); +} diff --git a/max_dist_between_val_pair.py b/max_dist_between_val_pair.py @@ -0,0 +1,28 @@ +# 1855. Maximum Distance Between a Pair of Values + +""" +you are given two non-increasing 0-indexed integer arrays 'nums1' and +'nums2'. a pair of indices '(i, j)' where '0 <= i < nums1.lenth' and '0 <= j +< nums2.lenth' is valid if both 'i <= j' and 'nums1[i] <= nums2[j]'. the +distance of the pair is 'j - i'. return the maximum distance of any valid +pair '(i, j)'. if there are no valid pairs, return 0. +""" + + +class Solution: + def maxDistance(self, nums1: List[int], nums2: List[int]) -> int: + i, j, ans = 0, 0, 0 + while i < len(nums1) and j < len(nums2): + if nums1[i] > nums2[j]: + i += 1 + else: + ans = max(ans, j - i) + j += 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxDistance(nums1=[55, 30, 5, 4, 2], nums2=[100, 20, 10, 10, 5])) + print(obj.maxDistance(nums1=[2, 2, 2], nums2=[10, 10, 1])) + print(obj.maxDistance(nums1=[30, 29, 19, 5], nums2=[25, 25, 25, 25, 25]))