Java solution: especially easy to understand


  • 0
    C
    public class Solution {
        public int maxDistance(int[][] arrays) {
            int res=0, min=arrays[0][0],max=arrays[0][arrays[0].length-1];
            for(int i=1;i<arrays.length;i++){
                //find min and max for each array
                int tmin=arrays[i][0],tmax=arrays[i][arrays[i].length-1];
                //update the result
                res=Math.max(res,tmax-min);
                res=Math.max(res,max-tmin);
                //update min and max
                min=Math.min(min,tmin);
                max=Math.max(max,tmax);
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.