Java Solution, Min and Max


  • 30
    public class Solution {
        public int maxDistance(int[][] arrays) {
            int result = Integer.MIN_VALUE;
            int max = arrays[0][arrays[0].length - 1];
            int min = arrays[0][0];
            
            for (int i = 1; i < arrays.length; i++) {
                result = Math.max(result, Math.abs(arrays[i][0] - max));
                result = Math.max(result, Math.abs(arrays[i][arrays[i].length - 1] - min));
                max = Math.max(max, arrays[i][arrays[i].length - 1]);
                min = Math.min(min, arrays[i][0]);
            }
            
            return result;
        }
    }
    

    LeetCode updated the input to List.

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

  • 0
    P
    This post is deleted!

  • 4
    S

    Thanks! It looks function arguments has been updated. Here is the solution for new arguments.

    public int maxDistance(List<List<Integer>> arrays) {
        int min = arrays.get(0).get(0);
        int max = arrays.get(0).get(arrays.get(0).size()-1);
        
        int result = Integer.MIN_VALUE;
        
        for(int i=1; i<arrays.size(); i++){
            int curmax = arrays.get(i).get(arrays.get(i).size()-1);
            int curmin = arrays.get(i).get(0);
            result = Math.max(result, Math.abs(max - curmin));
            result = Math.max(result, Math.abs(min - curmax));
            
            max = Math.max(max, curmax);
            min = Math.min(min, curmin);
        }
        
        return result;
    }

  • 0
    F

    There's no need to caculate Math.abs. we could use following code to update result.

    result = Math.max(result, max-arrays.get(i).get(0));
    result = Math.max(result, arrays.get(i).get(arrays.get(i).size() - 1) - min);
    

  • 0
    Y

    @FannyChung it could be negative.


  • 0

    Actually I don't understand why leetcode changed the signature. It does not satisfy in Arrays as said in the title.


  • 2
    Y

    @FannyChung
    We could ignore Math.abs.But we need to add some extra conditions:

    public class Solution {
        public int maxDistance(List<List<Integer>> arrays) {
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;
            int res = 0;
            for(int i = 0;i < arrays.size();i++){
                List<Integer> array = arrays.get(i);
                int curMin = array.get(0);
                int curMax = array.get(array.size()-1);
    //on condition 1: if max <= curMin,it couldn't be the result,because curMin <= curMax&&min <= max,so min <= curMax,
    //---the case would be covered in the second condition
    //Vice versa for the second condition
                if(max > curMin) res = Math.max(res,max - curMin);//condition 1
                if(curMax > min) res = Math.max(res,curMax - min);//condition 2
                min = Math.min(curMin,min);
                max = Math.max(curMax,max);
            }
            return res;
        }
    }

  • 1
    A

    What if arrays.get(0) is a null List?
    In this case max and min will throw out of bounds exception when they are being initialized!

    For this,
    int starting_index=0;
    int result=Integer.MIN_VALUE;
    for(int i=0;i<arrays.size();i++)
    {
    if(arrays.get(i).size()!=0)
    {
    max=arrays.get(i).get(arrays.get(i).size()-1);
    min=arrays.get(i).get(0);
    starting_index=i;
    break;
    }
    }

        for(int i=starting_index+1;i<arrays.size();i++)
        {

  • 0
    I

    Will get(index) be slow if arrays are implemented as LinkedList? Will Iterator be a better choice for the outer layer of List of arrays?


  • 0

    @yin10
    It's ok if it's negative. Because it indicates that the current distance cannot be the solution if it's negative.
    The following solution is accepted and is about 9ms faster.

    public class Solution {
        public int maxDistance(List<List<Integer>> arrays) {
            int maxDist = Integer.MIN_VALUE;
            int min = arrays.get(0).get(0), max = arrays.get(0).get(arrays.get(0).size() - 1);
            for (int i = 1; i < arrays.size(); i++) {
                int currMin = arrays.get(i).get(0);
                int currMax = arrays.get(i).get(arrays.get(i).size() - 1);
                maxDist = Math.max(maxDist, Math.max(currMax - min, max - currMin));
                max = Math.max(max, currMax);
                min = Math.min(min, currMin);
            }
            return maxDist;
        }
    }
    

  • 0

    @yujun Love the logic here, tks for sharing.


  • 1

    Re: [Java Solution](Min and Max): Can anyone explain how is this solution makes sure that min and max doesn't belong to the same list?


  • 0
    L

    Damn, you are so smart.


  • 0
    F

    @28nehasinha-gmail-com Go through the code, you will find the max number and min number of same array will never be calculated the difference. We initialized the min and max from the first interval. What happened next? We calculated the difference between current interval min and previous intervals max, And the difference between current interval max and previous intervals min. Then we update the min and max. So each operation we will only pick min or max number of current interval. So there are no chance to let min and max of same interval to calculate the difference. lol poor English expression. Hope can help you a little bit.


Log in to reply
 

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