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 ...
随机推荐
- postman断言分析
最近测试中用到postman,使用后就简单总结下常用的断言,下面带图的自己最常用的,其他的没怎么用. postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可. 断言 ...
- jQuery选择器之基本筛选选择器
<h2>基本筛选器</h2> <h3>:first/:last/:even/:odd</h3> <div class="left&quo ...
- android studio 导入jar包
或者还可以这么导入: 1.首先先去下载需要的jar包2.将jar包复制到Project下的app–>libs目录下(没有libs目录就新建一个)如下图所示位置: 3.点击工具栏中的Project ...
- Junit测试集锦
Junit测试集锦 前言: 一个程序从设计很好的状态开始,随着新的功能不断地加入,程序逐渐地失去了原有的结构,最终变成了一团乱麻.所以在开发过程中,对于程序员来说,测试是非常重要的.言归正传,开始Ju ...
- selelinum+PhantomJS 爬取拉钩网职位
使用selenium+PhantomJS爬取拉钩网职位信息,保存在csv文件至本地磁盘 拉钩网的职位页面,点击下一页,职位信息加载,但是浏览器的url的不变,说明数据不是发送get请求得到的. 我们不 ...
- SVM-支持向量机 学习 1
参考 https://zhuanlan.zhihu.com/p/42334376 https://blog.csdn.net/liugan528/article/details/79448379 ht ...
- 摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用
摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用 <!DOCTYPE html> <html lang="en&quo ...
- easyui前端框架01
一. 三大前端框架的特点 1.easyui=jquery+html4 优点:快速开发.功能齐全 .免费 缺点:不好看.不支持相应式开发 2.bootstrap=jquery+html5 优点: 功能强 ...
- Visual Studio 2017部署 webStrom开发的nodejs项目
vs点击文件--新建--项目--JavaScript--Node.js--通过现有Node.js代码 wxxcx为nodejs项目根目录,然后右击整个项目--属性:1.启动目录2.默认打开的链接3.设 ...
- [BZOJ3207]:花神的嘲讽(分块解法)
题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...