study
LeetCode#624
Date: 2024-08-16 04:16
Update: 2024-08-26 14:25
LeetCode#624. Maximum Distance in Arrays
You are given m
arrays
, where each array is sorted in ascending order.
You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a
and b
to be their absolute difference |a - b|
.
Return the maximum distance.
My Solution:
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
//Access the first elements' values
int a = arrays[0].front(), b = arrays[0].back();
int distance = INT_MIN;
//So starting from index 1
for(int i = 1; i < arrays.size(); i++){
distance = max(distance, abs(arrays[i].front() - b));
distance = max(distance, abs(a - arrays[i].back()));
a = min(a, arrays[i].front());
b = max(b, arrays[i].back());
}
return distance;
}
};