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 integersa
andb
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:
- 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
m
arrays will be in the range of [2, 10000].- 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:
- 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 ...
随机推荐
- Centos 6.5安装MySQL-python
报错信息: Using cached MySQL-python-1.2.5.zip Complete output from command python setup.py egg_info: ...
- POJ1150he Last Non-zero Digit(组合)
链接 题意从尾部找第一个非0的数 这样就可以考虑下怎样会形成0 这个都知道 只有因子2和因子5相遇会形成0 那这样可以先把所有的2和5先抽出来,这样就保证了其它的数相乘就不会再出现0了 这样就可以转 ...
- 使用JS移除select的某些选项
var arrvalue = new Array("1", "3", "4", "5", "6", ...
- 用户控件引用Entity Framework
背景: 今天在做软件的时候,出现了问题,我在项目里面添加了Entity Framework,在form的代码里引用没有问题,在userControl里引用就出了问题. 我检查app.config文件 ...
- AJPFX总结抽象类和接口的区别
/* * 抽象类和接口的区别 * 1.成员的区别 * ...
- VBScript(一)
visual basic Script 好像是以个老掉牙的服务器端脚本语言,低版本的IE浏览器支持在浏览器里执行 几个特点 1. 大小写不敏感 2.在服务器端 inputBox, msgBox不被支持 ...
- (转)关于IC设计的想法 Author :Fengzhepianzhou
一.工具的使用 工欲善其事,必先利其器.我们做IC设计的需要掌握的工具:仿真(vcs.modelsim),综合工具(dc.QS.ISE),时序分析(pt.其他的).以及后端的一些工具,比如astro. ...
- js文件下载代码 import downloadjs from 'downloadjs'
function logDownload(contribution_id) { setTimeout(function () { $.ajax({ url: "/ajax/Wallpaper ...
- 【转】C#的版本
这年头啥东东都喜欢过段时间整个啥新版本出来.汽车,手机如此,软件就更是如此了啊.比如啥Iphone 4,Iphone 5,Windows 8,Oracle 12C,SQL Server 2010. 版 ...
- zeromq编译与应用
libzmq是c++语言开发的,正式版本在这里: https://github.com/zeromq/libzmq/releases 到这篇文件发布为止,正式稳定版是4.2.2 1,按照给出的链接下载 ...