leetcode624. Maximum Distance in Arrays

Source: Internet
Author: User
Tags abs arrays
624. Maximum Distance in Arrays

Given m arrays, and each array was sorted in ascending order. Now you can pick up to integers from, different arrays (each array picks one) and calculate the distance. We define the distance between the integers a and B to be their absolute difference |a-b|. Your task is to find the maximum distance.

Example 1:
Input: 
[[[+] [[+],
 [4,5], []
 ]
output:4
Explanation: One to 
reach the Maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

Note:
Each given array would has at least 1 number. There'll is at least, non-empty arrays.
The total number of the integers in all the arrays is in the range of [2, 10000].
The integers in the M arrays is in the range of [-10000, 10000]. Solution

Each list in the list is ordered, so List.get (0) is the smallest, and List.get (List.size ()-1) is the largest.
Subtract min with the maximum value of the current array and subtract max with the minimum value. Updates the maximum and minimum values with the current list.

public class Solution {public
    int maxdistance (list<list<integer>> arrays) {
        if (arrays = null | | arr Ays.size () = = 0) {
            return 0;
        }

        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 (Arrays.get (i). Size () -1)-min);
            result = Math.max (result, Math.Abs (Arrays.get (i). Get (0)-Max));
            max = Math.max (max, Arrays.get (i). Get (Arrays.get (i). Size ()-1));
            min = math.min (min, Arrays.get (i). Get (0));
        }

        return result;
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.