Leetcode 624: Maximum Distance in Arrays
You are given m sorted arrays. Your task is to pick two integers, each from a different array, and calculate the maximum distance between them, where the distance is defined as the absolute difference of the two numbers.
Problem
Approach
Steps
Complexity
Input: You are given m sorted arrays, each with an integer value.
Example: arrays = [[3,6,9], [1,4,7], [8,10,15]]
Constraints:
• m == arrays.length
• 2 <= m <= 10^5
• 1 <= arrays[i].length <= 500
• -10^4 <= arrays[i][j] <= 10^4
• arrays[i] is sorted in ascending order.
• There will be at most 10^5 integers in all the arrays.
Output: Return the maximum distance that can be achieved by selecting one integer from each of two different arrays.
Example: 14
Constraints:
• Return a single integer representing the maximum distance.
Goal: Find the maximum distance by comparing the largest and smallest elements across arrays.
Steps:
• Initialize variables to track the minimum and maximum values across the arrays.
• For each array, calculate the potential maximum distance using the largest value from the current array and the smallest value from the previous array, as well as vice versa.
• Update the result with the maximum distance found.
Goal: Handle arrays of varying sizes efficiently while ensuring the maximum distance is computed correctly.
Steps:
• The time complexity of the solution should be optimal for large inputs, as there may be up to 10^5 arrays.
Assumptions:
• The input arrays are correctly sorted in ascending order.
• Arrays may vary in size, and elements can be negative or positive.
• Input: arrays = [[3,6,9], [1,4,7], [8,10,15]]
• Explanation: In this case, the maximum distance is obtained by selecting the smallest value from the second array (1) and the largest value from the third array (15), resulting in a distance of 14.
Approach: The approach involves tracking the minimum and maximum values across arrays and calculating the potential distances for each pair of arrays.
Observations:
• We need an efficient way to calculate the maximum distance between two different arrays.
• By keeping track of the smallest and largest values across the arrays, we can calculate the maximum distance in linear time.
Steps:
• 1. Initialize variables to track the minimum and maximum values seen so far.
• 2. Iterate through the arrays to compute the maximum possible distance.
• 3. Update the result with the largest distance found.
Empty Inputs:
• The problem guarantees that there will always be at least two arrays, so empty inputs do not need to be handled.
Large Inputs:
• Ensure the solution works efficiently even for the maximum constraint of 10^5 arrays.
Special Values:
• Handle arrays containing negative and positive numbers.
Constraints:
• The solution must handle arrays of different sizes and values, including edge cases like arrays with only one element.
int maxDistance(vector<vector<int>>& arrays) {
int res = 0, mn = 10000, mx = -10000;
for (auto& a : arrays) {
res = max(res, max(a.back()-mn, mx-a.front()));
mn = min(mn, a.front()), mx = max(mx, a.back());
}
return res;
}
1 : Function Declaration
int maxDistance(vector<vector<int>>& arrays) {
This line declares the function `maxDistance`, which takes a reference to a 2D vector `arrays` and returns an integer representing the maximum distance between the first and last elements of the arrays.
2 : Variable Initialization
int res = 0, mn = 10000, mx = -10000;
Three integer variables are initialized: `res` to store the result, `mn` to track the minimum value encountered, and `mx` to track the maximum value encountered.
3 : Loop Through Arrays
for (auto& a : arrays) {
A `for` loop iterates through each sub-array `a` in the 2D vector `arrays`.
4 : Calculate Maximum Distance for Each Sub-array
res = max(res, max(a.back()-mn, mx-a.front()));
For each sub-array, the function calculates the distance between the current minimum value (`mn`) and the last element of the sub-array (`a.back()`), and the distance between the current maximum value (`mx`) and the first element of the sub-array (`a.front()`). The result is updated with the maximum of these values.
5 : Update Min and Max Values
mn = min(mn, a.front()), mx = max(mx, a.back());
The function updates `mn` to the minimum of the current `mn` and the first element of the sub-array (`a.front()`), and `mx` to the maximum of the current `mx` and the last element of the sub-array (`a.back()`), ensuring that the minimum and maximum values are kept up to date.
6 : Return Result
return res;
The function returns the value of `res`, which contains the maximum distance found between the first and last elements of any sub-array.
Best Case: O(m)
Average Case: O(m)
Worst Case: O(m)
Description: The solution iterates over the arrays once, so the time complexity is linear with respect to the number of arrays.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is constant as we only use a few variables to track the minimum and maximum values.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus