Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: arrays = [[1,2,3],[4,5],[1,2,3]] Output: 4 Explanation: One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
Example 2:
Input: arrays = [[1],[1]] Output: 0
Constraints:
m == arrays.length2 <= m <= 1051 <= arrays[i].length <= 500-104 <= arrays[i][j] <= 104arrays[i] is sorted in ascending order.105 integers in all the arrays.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[[1,2,3],[4,5],[1,2,3]]
[[1],[1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #624: Maximum Distance in Arrays
class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int ans = 0;
int mi = arrays.get(0).get(0);
int mx = arrays.get(0).get(arrays.get(0).size() - 1);
for (int i = 1; i < arrays.size(); ++i) {
var arr = arrays.get(i);
int a = Math.abs(arr.get(0) - mx);
int b = Math.abs(arr.get(arr.size() - 1) - mi);
ans = Math.max(ans, Math.max(a, b));
mi = Math.min(mi, arr.get(0));
mx = Math.max(mx, arr.get(arr.size() - 1));
}
return ans;
}
}
// Accepted solution for LeetCode #624: Maximum Distance in Arrays
func maxDistance(arrays [][]int) (ans int) {
mi, mx := arrays[0][0], arrays[0][len(arrays[0])-1]
for _, arr := range arrays[1:] {
a, b := abs(arr[0]-mx), abs(arr[len(arr)-1]-mi)
ans = max(ans, max(a, b))
mi = min(mi, arr[0])
mx = max(mx, arr[len(arr)-1])
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #624: Maximum Distance in Arrays
class Solution:
def maxDistance(self, arrays: List[List[int]]) -> int:
ans = 0
mi, mx = arrays[0][0], arrays[0][-1]
for arr in arrays[1:]:
a, b = abs(arr[0] - mx), abs(arr[-1] - mi)
ans = max(ans, a, b)
mi = min(mi, arr[0])
mx = max(mx, arr[-1])
return ans
// Accepted solution for LeetCode #624: Maximum Distance in Arrays
impl Solution {
pub fn max_distance(arrays: Vec<Vec<i32>>) -> i32 {
let mut ans = 0;
let mut mi = arrays[0][0];
let mut mx = arrays[0][arrays[0].len() - 1];
for i in 1..arrays.len() {
let arr = &arrays[i];
let a = (arr[0] - mx).abs();
let b = (arr[arr.len() - 1] - mi).abs();
ans = ans.max(a).max(b);
mi = mi.min(arr[0]);
mx = mx.max(arr[arr.len() - 1]);
}
ans
}
}
// Accepted solution for LeetCode #624: Maximum Distance in Arrays
function maxDistance(arrays: number[][]): number {
let ans = 0;
let [mi, mx] = [arrays[0][0], arrays[0].at(-1)!];
for (let i = 1; i < arrays.length; ++i) {
const arr = arrays[i];
const a = Math.abs(arr[0] - mx);
const b = Math.abs(arr.at(-1)! - mi);
ans = Math.max(ans, a, b);
mi = Math.min(mi, arr[0]);
mx = Math.max(mx, arr.at(-1)!);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.