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 = ; 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的更多相关文章

  1. 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 ...

  2. 624. Maximum Distance in Arrays二重数组中的最大差值距离

    [抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...

  3. [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 ...

  4. 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...

  5. [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 ...

  6. LeetCode Maximum Distance in Arrays

    原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...

  7. [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 ...

  8. Avito Cool Challenge 2018:D. Maximum Distance

    D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...

  9. 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 ...

随机推荐

  1. poj3204Ikki's Story I - Road Reconstruction(最大流求割边)

    链接 最大流=最小割  这题是求割边集 dinic求出残余网络 两边dfs分别以源点d找到可达点 再以汇点进行d找到可达汇点的点 如果u,v为割边 那么s->u可达 v->t可达 并且为饱 ...

  2. 访问github.com太慢的解决方法

    修改 c:\windows\system32\drivers\etc\host文件添加 192.30.255.112 github.com 151.101.72.249 github.global.s ...

  3. spring boot 的redis 之初理解

    项目到末尾了快, 这几天安排我结合业务场景给项目加上redis 缓存, 我接到这个任务也是懵逼了一会儿: 问了一句让我自己先想办法,没办法硬着头皮查吧, 要不不得不说spring boot 还是好用, ...

  4. Web前端攻防,一不小心就中招了

    随着各浏览器安全功能的提高,前端防御面临的问题也没有之前那么复杂,但浏览器的防御措施并不能百分百的保证网站的安全. 浏览器的XSS Auditor,使得反射型xss几乎被废:CSP(Content-S ...

  5. java 获取ip地址

    1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...

  6. The lion king 经典句型摘录

    What am I going to do with him? Everything the light touches is our kingdom. But I thought a king ca ...

  7. 无法登录phpmyadmin,报1130错误

    分析过程及解决方案: mysql的1130错误是远程连接的用户无远程权限问题导致.解决方案:在本机登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”loc ...

  8. ABAP的HTTP_GET和Linux的curl

    curl是利用URL语法在命令行方式下工作的开源文件传输工具,广泛应用在Unix,多种Linux发行版中. 在Windows系统下也有移植版. curl尤其被广泛应用在github上众多开源软件和框架 ...

  9. 富通天下(W 笔试)

    纸质算法题目 1.给你一个字符串,找出其中第一个只出现过一次的字符及其位置 正解:一层for循环,循环按序取出字符串中的单个字符,循环体内部使用String类的indexOf(),从当前字符下标往后搜 ...

  10. 数组排序 sort

    数组排序 this.dataShow = this.data.sort((a, b) => { return parseInt(a[this.innerOrderBy]) - parseInt( ...