zoukankan      html  css  js  c++  java
  • 624. Maximum Distance in Arrays

    Problem statement

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

    Example 1:

    Input: 
    [[1,2,3],
     [4,5],
     [1,2,3]]
    Output: 4
    Explanation: 
    One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

    Note:

    1. Each given array will have at least 1 number. There will be at least two non-empty arrays.
    2. The total number of the integers in all the m arrays will be in the range of [2, 10000].
    3. The integers in the m arrays will be in the range of [-10000, 10000].

    Solution

    This is the first question in leetcode weekly contest 37, the key issue is we need to find the maximum distance among two different arrays. 

    My solution: 

    The general idea is as follows:

    1. Two vectors, store all min and max value with their array index in the format of pair.
    2. Sort these two vectors.
    3. Return values: if min and max values come from different arrays, return their difference directly. Otherwise, do more process.

    Time complexity is O(nlgn), space complexity is O(n). n is the number of arrays.

    class Solution {
    public:
        int maxDistance(vector<vector<int>>& arrays) {
            vector<pair<int, int>> minv;
            vector<pair<int, int>> maxv;
            // add each value with it`s array index to a vector
            for(int i = 0; i < arrays.size(); i++){
                minv.push_back({arrays[i][0], i});
                maxv.push_back({arrays[i].back(), i});
            }
            // sort the array by incrasing order
            sort(minv.begin(), minv.end());
            sort(maxv.begin(), maxv.end());
            // return directly if the min and max come from different arrays
            if(minv[0].second != maxv.back().second){
                return maxv.back().first - minv[0].first;
            } else {
                // otherwise
                return maxv.back().first - minv[1].first > maxv[maxv.size() - 2].first - minv[0].first ?
                   maxv.back().first - minv[1].first :  maxv[maxv.size() - 2].first - minv[0].first;
            }
        }
    };

    The solution from leetcode

    This solution is more intuitive and is the most concise version.

    Since the max difference of min and max can not come from same array, we store the min and max value in previous processed arrays and compare with current min and max.

    Time complexity is O(n), space complexity is O(1).

    class Solution {
    public:
        int maxDistance(vector<vector<int>>& arrays) {
            int minv = arrays[0][0];
            int maxv = arrays[0].back();
            int max_dis = INT_MIN;
            for(int i = 1; i < arrays.size(); i++){
                max_dis = max(max_dis, max(abs(maxv - arrays[i][0]), abs(arrays[i].back() - minv)));
                maxv = max(maxv, arrays[i].back());
                minv = min(minv, arrays[i][0]);
            }
            return max_dis;
        }
    };
  • 相关阅读:
    HEOI2016 题解
    数据结构考前复习【已弃】
    BZOJ 3993: [SDOI2015]星际战争 [二分答案 二分图]
    BZOJ 4698: Sdoi2008 Sandy的卡片 [后缀自动机]
    BZOJ 3990: [SDOI2015]排序 [搜索]
    字符串考前总结
    BZOJ 3881: [Coci2015]Divljak [AC自动机 树链的并]
    CF698C. LRU [容斥原理 概率]
    BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]
    BZOJ 1444: [Jsoi2009]有趣的游戏 [AC自动机 高斯消元]
  • 原文地址:https://www.cnblogs.com/wdw828/p/7057580.html
Copyright © 2011-2022 走看看