LeetCode 624: Maximum Distance in Arrays Solution

Master LeetCode problem 624 (Maximum Distance in Arrays), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

624. Maximum Distance in Arrays

Problem Explanation

Explanation

To find the maximum distance between two numbers, we need to consider the minimum and maximum values from the arrays. We can do this by keeping track of the minimum and maximum values encountered so far in the arrays as we iterate through them. At each step, we update the maximum distance by comparing it with the absolute difference between the current element and the current minimum or maximum value.

Solution Code

class Solution {
    public int maxDistance(List<List<Integer>> arrays) {
        int result = 0;
        int minVal = arrays.get(0).get(0);
        int maxVal = arrays.get(0).get(arrays.get(0).size() - 1);
        
        for (int i = 1; i < arrays.size(); i++) {
            List<Integer> currentArray = arrays.get(i);
            int currentMin = currentArray.get(0);
            int currentMax = currentArray.get(currentArray.size() - 1);
            
            result = Math.max(result, Math.abs(currentMax - minVal));
            result = Math.max(result, Math.abs(maxVal - currentMin));
            
            minVal = Math.min(minVal, currentMin);
            maxVal = Math.max(maxVal, currentMax);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 624 (Maximum Distance in Arrays)?

This page provides optimized solutions for LeetCode problem 624 (Maximum Distance in Arrays) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 624 (Maximum Distance in Arrays)?

The time complexity for LeetCode 624 (Maximum Distance in Arrays) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 624 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 624 in Java, C++, or Python.

Back to LeetCode Solutions