624. Maximum Distance in Arrays
Problem statement
Given
marrays, 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 integersaandbto 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:
- Each given array will have at least 1 number. There will be at least two non-empty arrays.
 - The total number of the integers in all the
 marrays will be in the range of [2, 10000].- The integers in the
 marrays 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:
- Two vectors, store all min and max value with their array index in the format of pair.
 - Sort these two vectors.
 - 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 = ; i < arrays.size(); i++){
            minv.push_back({arrays[i][], 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[].second != maxv.back().second){
            return maxv.back().first - minv[].first;
        } else {
            // otherwise
            return maxv.back().first - minv[].first > maxv[maxv.size() - ].first - minv[].first ?
               maxv.back().first - minv[].first :  maxv[maxv.size() - ].first - minv[].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[][];
        int maxv = arrays[].back();
        int max_dis = INT_MIN;
        for(int i = ; i < arrays.size(); i++){
            max_dis = max(max_dis, max(abs(maxv - arrays[i][]), abs(arrays[i].back() - minv)));
            maxv = max(maxv, arrays[i].back());
            minv = min(minv, arrays[i][]);
        }
        return max_dis;
    }
};
624. Maximum Distance in Arrays的更多相关文章
- LeetCode 624. Maximum Distance in Arrays (在数组中的最大距离)$
		
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...
 - 624. Maximum Distance in Arrays二重数组中的最大差值距离
		
[抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...
 - [LeetCode] 624. Maximum Distance in Arrays 数组中的最大距离
		
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...
 - 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...
 - [LeetCode] Maximum Distance in Arrays 数组中的最大距离
		
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...
 - LeetCode Maximum Distance in Arrays
		
原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...
 - [leetcode-624-Maximum Distance in Arrays]
		
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...
 - Avito Cool Challenge 2018:D. Maximum Distance
		
D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...
 - CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
		
https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...
 
随机推荐
- solr 包地址
			
http://archive.apache.org/dist/lucene/solr/6.3.0/
 - shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
			
shift Alt + up(down) copy current line ! ctrl + j show the control # vscode key
 - autoHeight.vue 高度自适应
			
autoHeight.vue 高度自适应 <!-- * @description 自适应高度 * @fileName autoHeight.vue * @author 彭成刚 * @date 2 ...
 - Format a Hard Drive in Csharp
			
Article Author(s): Audric Thevenet All Rights Reserved. Here's how to format hard drives, floppies, ...
 - Java中System.setProperty()用法
			
/* * 设置指定键对值的系统属性 * setProperty (String prop, String value); * * 参数: * prop - 系统属性的名称. * value ...
 - 1.1 Qt入门
			
学习Qt的前提是学好C++. 我刚入门Qt,打算趁着暑假2个月时间来学习<C++ GUI Qt 4>这本书. 现在有Qt4和Qt5,似乎很多公司都还是在使用Qt4,所以我也就选择了学习Qt ...
 - ansible2.7学习笔记系列
			
写在前面:ansible的资料网上很多,本人也是参考网上资料,做总结,如有错误,麻烦指出,谢谢. 所谓学习笔记,就是不断成长的过程,也许一段时间后有更深入理解了,就会继续更新笔记. 笔记定位:目前写的 ...
 - JavaSE-26 Swing
			
学习要点 关于Swing Swing容器组件 Swing布局管理器 Swing组件 Swing菜单组件 关于Swing Swing和AWT都是java开发图形用户界面的工具包. AWT:早期Java版 ...
 - 性能测试,如何得到大量token,并保存在本地文件中
			
需求:性能测试需要大量的token,模拟登陆 设计思路: 1.使用语言:python +request+正则匹配+写入本地 2.jmeter+函数助手+正则或者json/yaml+后置处理器beans ...
 - 洛谷——P3801 红色的幻想乡
			
P3801 红色的幻想乡 推荐阅读 https://blog.csdn.net/qq_41252892/article/details/79035942 非常清楚 线段树单点修改 emmm没什么了 # ...