Skip to content

Commit fcd68a2

Browse files
chore: add LeetCode daily solution
1 parent 685d887 commit fcd68a2

5 files changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Maximum Distance Between a Pair of Values (Medium)
2+
3+
**Problem ID:** 1855
4+
**Date:** 2026-04-19
5+
**Link:** https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/
6+
7+
## Approach
8+
9+
To solve the problem of finding the maximum distance between a pair of values from two non-increasing arrays, we can employ a two-pointer technique. Here’s a concise breakdown of the approach:
10+
11+
### Main Idea:
12+
The goal is to find indices \(i\) and \(j\) such that \(0 \leq i < \text{nums1.length}\), \(0 \leq j < \text{nums2.length}\), \(i \leq j\), and \(\text{nums1}[i] \leq \text{nums2}[j]\). The distance is defined as \(j - i\), and we want to maximize this distance.
13+
14+
### Approach:
15+
1. **Initialization**: Start with two pointers, one for each array. Let \(i\) begin at the start of `nums1` and \(j\) at the start of `nums2`.
16+
17+
2. **Iterate with Two Pointers**:
18+
- Use a while loop to iterate through the arrays. For each index \(i\) in `nums1`, increment \(j\) in `nums2` as long as the condition \(\text{nums1}[i] \leq \text{nums2}[j]\) holds true. This is possible because both arrays are non-increasing, meaning if \(\text{nums1}[i] \leq \text{nums2}[j]\), then \(\text{nums1}[i] \leq \text{nums2}[j+1]\) will also hold until \(j\) reaches the end of `nums2`.
19+
20+
3. **Calculate Distance**: For every valid pair found (where \(\text{nums1}[i] \leq \text{nums2}[j]\)), calculate the distance \(j - i\) and update the maximum distance found.
21+
22+
4. **Increment \(i\)**: After processing all valid \(j\) for a given \(i\), increment \(i\) to check for the next element in `nums1`.
23+
24+
5. **Termination**: The process continues until all elements in `nums1` have been considered or \(j\) has reached the end of `nums2`.
25+
26+
### Data Structures:
27+
- Two pointers (integers) for indexing `nums1` and `nums2`.
28+
29+
### Complexity:
30+
- **Time Complexity**: \(O(n + m)\), where \(n\) is the length of `nums1` and \(m\) is the length of `nums2`. Each pointer traverses its respective array at most once.
31+
- **Space Complexity**: \(O(1)\), as we are using a constant amount of extra space.
32+
33+
This efficient approach ensures we can handle the upper limits of the input constraints effectively, yielding the maximum distance between valid pairs in linear time.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int maxDistance(int[] nums1, int[] nums2) {
3+
int maxDist = 0;
4+
int j = 0;
5+
for (int i = 0; i < nums1.length; i++) {
6+
while (j < nums2.length && nums1[i] <= nums2[j]) {
7+
j++;
8+
}
9+
maxDist = Math.max(maxDist, j - 1 - i);
10+
}
11+
return maxDist;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var maxDistance = function(nums1, nums2) {
2+
let maxDist = 0;
3+
let j = 0;
4+
5+
for (let i = 0; i < nums1.length; i++) {
6+
while (j < nums2.length && nums1[i] <= nums2[j]) {
7+
j++;
8+
}
9+
maxDist = Math.max(maxDist, j - 1 - i);
10+
}
11+
12+
return maxDist;
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
3+
max_distance = 0
4+
j = 0
5+
n1, n2 = len(nums1), len(nums2)
6+
7+
for i in range(n1):
8+
while j < n2 and nums1[i] <= nums2[j]:
9+
j += 1
10+
max_distance = max(max_distance, j - 1 - i)
11+
12+
return max_distance

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
428428
- 2026-04-16 — [Closest Equal Element Queries](https://leetcode.com/problems/closest-equal-element-queries/) (Medium) → `Medium/2026-04-16-3488-Closest-Equal-Element-Queries`
429429
- 2026-04-17 — [Minimum Absolute Distance Between Mirror Pairs](https://leetcode.com/problems/minimum-absolute-distance-between-mirror-pairs/) (Medium) → `Medium/2026-04-17-3761-Minimum-Absolute-Distance-Between-Mirror-Pairs`
430430
- 2026-04-18 — [Mirror Distance of an Integer](https://leetcode.com/problems/mirror-distance-of-an-integer/) (Easy) → `Easy/2026-04-18-3783-Mirror-Distance-of-an-Integer`
431+
- 2026-04-19 — [Maximum Distance Between a Pair of Values](https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/) (Medium) → `Medium/2026-04-19-1855-Maximum-Distance-Between-a-Pair-of-Values`

0 commit comments

Comments
 (0)