[Leetcode] Maximum the maximum distance in the Distance in Arrays array

Source: Internet
Author: User

Given m arrays, and each of the array is 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 and to be their absolute difference a b |a-b| . Your task is to find the maximum distance.

Example 1:

Input: [[1], [4,5], [1,2,3]]output:4explanation:one-to reach the maximum distance 4 are to pick in the first or Third array and pick 5 in the second array.

Note:

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

This problem gives us some arrays, each array is ordered, let us take a number from different arrays, so that the absolute value of the difference between the two numbers is the largest, let us ask for this maximum. So we think, since the array is ordered, then the absolute value of the largest two numbers are definitely located in the first and the end of the array, note that the topic is to be taken from different arrays, so even if a number of the end of the group is a large gap, also not. Bloggers first consider the heap to do, a maximum heap, a minimum heap, the largest pile of each array of tail elements, the smallest to store the first element of each array, because the largest number and the smallest number may come from the same array, so we in the heap when the number is stored in the current number of arrays in the sequence number, Finally we have to take two numbers respectively in the maximum heap and the smallest heap, if the largest number and the smallest number is not an array, then return the absolute difference between the two, if so, then return the second big number and the minimum number absolute difference with the largest number and the second small number of absolute difference in the larger value, See the code below:

Solution One:

classSolution { Public:    intMaxDistance (vector<vector<int>>&arrays) {Priority_queue<pair<int,int>>MX, MN;  for(inti =0; I < arrays.size (); ++i) {Mn.push ({-arrays[i][0], i});        Mx.push ({arrays[i].back (), i}); } Auto A1=mx.top (); Mx.pop (); Auto B1=mn.top (); Mn.pop (); if(A1.second! = B1.second)returnA1.first +B1.first; returnMax (A1.first + mn.top (). First, Mx.top (). First +B1.first); }};

The following method is still very good, and does not use the heap, but with two variables start and end to represent the current traversed array of the smallest first element, and the largest tail element, so every time we traverse to a new array, we only need to calculate the new array tail element and start absolute difference, With the absolute difference between the end and the first element of the new array, take a larger value between the two to update the result res, see the code below:

Solution Two:

classSolution { Public:    intMaxDistance (vector<vector<int>>&arrays) {        intres =0, start = arrays[0][0], end = arrays[0].back ();  for(inti =1; I < arrays.size (); ++i) {res= Max (res, MAX (ABS (Arrays[i].back ()-start), ABS (end-arrays[i][0]))); Start= Min (Start, arrays[i][0]); End=Max (end, Arrays[i].back ()); }        returnRes; }};

Resources:

Https://discuss.leetcode.com/topic/92858/c-o-n

Https://discuss.leetcode.com/topic/92859/java-solution-min-and-max

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Maximum the maximum distance in the Distance in Arrays array

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.