From f2c22c74e61b8f8621401bccaa4f78fde13b8a8b Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Sun, 9 May 2021 13:22:03 +0800 Subject: [PATCH] New Problem Solution - "1855. Maximum Distance Between a Pair of Values" --- README.md | 1 + .../MaximumDistanceBetweenAPairOfValues.cpp | 87 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 algorithms/cpp/maximumDistanceBetweenAPairOfValues/MaximumDistanceBetweenAPairOfValues.cpp diff --git a/README.md b/README.md index 25c6cacb..9e77903a 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | +|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| |1854|[Maximum Population Year](https://leetcode.com/problems/maximum-population-year/) | [C++](./algorithms/cpp/maximumPopulationYear/MaximumPopulationYear.cpp)|Easy| |1851|[Minimum Interval to Include Each Query](https://leetcode.com/problems/minimum-interval-to-include-each-query/) | [C++](./algorithms/cpp/minimumIntervalToIncludeEachQuery/MinimumIntervalToIncludeEachQuery.cpp)|Hard| |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| diff --git a/algorithms/cpp/maximumDistanceBetweenAPairOfValues/MaximumDistanceBetweenAPairOfValues.cpp b/algorithms/cpp/maximumDistanceBetweenAPairOfValues/MaximumDistanceBetweenAPairOfValues.cpp new file mode 100644 index 00000000..32891188 --- /dev/null +++ b/algorithms/cpp/maximumDistanceBetweenAPairOfValues/MaximumDistanceBetweenAPairOfValues.cpp @@ -0,0 +1,87 @@ +// Source : https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/ +// Author : Hao Chen +// Date : 2021-05-09 + +/***************************************************************************************************** + * + * You are given two non-increasing 0-indexed integer arrays nums1​​​​​​ and nums2​​​​​​. + * + * A pair of indices (i, j), where 0 <= i < nums1.length and 0 <= j < nums2.length, 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. + * + * An array arr is non-increasing if arr[i-1] >= arr[i] for every 1 <= i < arr.length. + * + * Example 1: + * + * Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] + * Output: 2 + * Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). + * The maximum distance is 2 with pair (2,4). + * + * Example 2: + * + * Input: nums1 = [2,2,2], nums2 = [10,10,1] + * Output: 1 + * Explanation: The valid pairs are (0,0), (0,1), and (1,1). + * The maximum distance is 1 with pair (0,1). + * + * Example 3: + * + * Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] + * Output: 2 + * Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). + * The maximum distance is 2 with pair (2,4). + * + * Example 4: + * + * Input: nums1 = [5,4], nums2 = [3,2] + * Output: 0 + * Explanation: There are no valid pairs, so return 0. + * + * Constraints: + * + * 1 <= nums1.length <= 10^5 + * 1 <= nums2.length <= 10^5 + * 1 <= nums1[i], nums2[j] <= 10^5 + * Both nums1 and nums2 are non-increasing. + ******************************************************************************************************/ + +class Solution { +public: + int maxDistance(vector& nums1, vector& nums2) { + return maxDistance2(nums1, nums2); + return maxDistance1(nums1, nums2); + } + + + int binary_search(vector& nums, int start, int target) { + int end = nums.size() - 1; + while (start <= end) { + int mid = start + (end - start) /2; + if(nums[mid] < target) end = mid - 1; + else start = mid+1; + } + return end; + } + + int maxDistance1(vector& nums1, vector& nums2) { + int mDist=0; + int right = nums2.size() - 1; + for(int i=0; i& nums1, vector& nums2) { + int i=0, j=0, dist = 0; + while (i < nums1.size() && j < nums2.size() ){ + if ( nums1[i] > nums2[j] ) i++; + else dist = max(dist, j++ - i); + } + return dist; + } +};