Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf2c22c7

Browse files
committed
New Problem Solution - "1855. Maximum Distance Between a Pair of Values"
1 parenta78c679 commitf2c22c7

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| #| Title| Solution| Difficulty|
1111
|---| -----| --------| ----------|
12+
|1855|[Maximum Distance Between a Pair of Values](https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/)|[C++](./algorithms/cpp/maximumDistanceBetweenAPairOfValues/MaximumDistanceBetweenAPairOfValues.cpp)|Medium|
1213
|1854|[Maximum Population Year](https://leetcode.com/problems/maximum-population-year/)|[C++](./algorithms/cpp/maximumPopulationYear/MaximumPopulationYear.cpp)|Easy|
1314
|1851|[Minimum Interval to Include Each Query](https://leetcode.com/problems/minimum-interval-to-include-each-query/)|[C++](./algorithms/cpp/minimumIntervalToIncludeEachQuery/MinimumIntervalToIncludeEachQuery.cpp)|Hard|
1415
|1850|[Minimum Adjacent Swaps to Reach the Kth Smallest Number](https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/)|[C++](./algorithms/cpp/minimumAdjacentSwapsToReachTheKthSmallestNumber/MinimumAdjacentSwapsToReachTheKthSmallestNumber.cpp)|Medium|
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Source : https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/
2+
// Author : Hao Chen
3+
// Date : 2021-05-09
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given two non-increasing 0-indexed integer arrays nums1​​​​​​ and nums2​​​​​​.
8+
*
9+
* A pair of indices (i, j), where 0 <= i < nums1.length and 0 <= j < nums2.length, is valid if both i
10+
* <= j and nums1[i] <= nums2[j]. The distance of the pair is j - i​​​​.
11+
*
12+
* Return the maximum distance of any valid pair (i, j). If there are no valid pairs, return 0.
13+
*
14+
* An array arr is non-increasing if arr[i-1] >= arr[i] for every 1 <= i < arr.length.
15+
*
16+
* Example 1:
17+
*
18+
* Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
19+
* Output: 2
20+
* Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).
21+
* The maximum distance is 2 with pair (2,4).
22+
*
23+
* Example 2:
24+
*
25+
* Input: nums1 = [2,2,2], nums2 = [10,10,1]
26+
* Output: 1
27+
* Explanation: The valid pairs are (0,0), (0,1), and (1,1).
28+
* The maximum distance is 1 with pair (0,1).
29+
*
30+
* Example 3:
31+
*
32+
* Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
33+
* Output: 2
34+
* Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).
35+
* The maximum distance is 2 with pair (2,4).
36+
*
37+
* Example 4:
38+
*
39+
* Input: nums1 = [5,4], nums2 = [3,2]
40+
* Output: 0
41+
* Explanation: There are no valid pairs, so return 0.
42+
*
43+
* Constraints:
44+
*
45+
* 1 <= nums1.length <= 10^5
46+
* 1 <= nums2.length <= 10^5
47+
* 1 <= nums1[i], nums2[j] <= 10^5
48+
* Both nums1 and nums2 are non-increasing.
49+
******************************************************************************************************/
50+
51+
classSolution {
52+
public:
53+
intmaxDistance(vector<int>& nums1, vector<int>& nums2) {
54+
returnmaxDistance2(nums1, nums2);
55+
returnmaxDistance1(nums1, nums2);
56+
}
57+
58+
59+
intbinary_search(vector<int>& nums,int start,int target) {
60+
int end = nums.size() -1;
61+
while (start <= end) {
62+
int mid = start + (end - start) /2;
63+
if(nums[mid] < target) end = mid -1;
64+
else start = mid+1;
65+
}
66+
return end;
67+
}
68+
69+
intmaxDistance1(vector<int>& nums1, vector<int>& nums2) {
70+
intmDist=0;
71+
int right = nums2.size() -1;
72+
for(int i=0; i<nums1.size(); i++) {
73+
int j =binary_search(nums2, i, nums1[i]);
74+
mDist =max(mDist, j-i);
75+
}
76+
returnmDist;
77+
}
78+
79+
intmaxDistance2(vector<int>& nums1, vector<int>& nums2) {
80+
int i=0, j=0, dist =0;
81+
while (i < nums1.size() && j < nums2.size() ){
82+
if ( nums1[i] > nums2[j] ) i++;
83+
else dist =max(dist, j++ - i);
84+
}
85+
return dist;
86+
}
87+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp