Java Solution O(n)


  • 0
    V
    public class Solution {
        public int maxDistance(int[][] arrays) {
            if(arrays==null|| arrays.length==0)
            return -1;
            int max = Integer.MIN_VALUE;
            int min=Integer.MAX_VALUE;
            int idx=-1,next_min=Integer.MAX_VALUE;
            int absVal=0;
            for(int i=0;i<arrays.length;i++)
            {
                if(arrays[i].length!=0)
                {
                    min=Math.min(min,arrays[i][0]);
                    if(min==arrays[i][0])
                    idx=i;
                }
            }
            int next_max=arrays[idx][arrays[idx].length-1];
            for(int j=0;j<arrays.length;j++)
            {
                if(arrays[j].length!=0)
                {
                    if(j!=idx)
                    {
                        max=Math.max(max,arrays[j][arrays[j].length-1]);
                        next_min=Math.min(next_min,arrays[j][0]);
                    }
                }
            }
           
            absVal=Math.max(Math.abs(max-min),Math.abs(next_max-next_min));
            return absVal;
        }
    }
    

Log in to reply
 

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