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. Suricata的初始化脚本

    见官网 https://suricata.readthedocs.io/en/latest/initscripts.html

  2. 解决Eclipse自动补全变量名的问题

    原文地址: https://blog.csdn.net/czh500/article/details/53162157

  3. python中一些函数应用

    items将一个字典以列表的形式返回,因为字典是无序的,所以返回的列表也是无序的. 例如:a = {"a":1,"b":2}    a.items  就是 a ...

  4. P3372 【模板】线段树 1 区间查询与区间修改

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

  5. KendoUI Grid Pager部分 Nan-Nan of x Items

    相关问题: http://stackoverflow.com/questions/23941065/pager-error-in-kendo-gridnan-nan-of-1-items http:/ ...

  6. codevs 2600 13号星期几?

    时间限制: 1 s  空间限制: 8000 KB  题目等级 : 黄金 Gold 题目描述 Description 从1900年1月1日(星期一) 开始经过的n年当中,每个月的13号这一天是星期一.星 ...

  7. Windows各个文件夹介绍

    windows文件介绍 总结 ├WINDOWS │ ├-system32(存放Windows的系统文件和硬件驱动程序) │ │ ├-config(用户配置信息和密码信息) │ │ │ └-system ...

  8. 详解nginx.conf文件配置项(包括负载均衡)

    http://www.cnblogs.com/hsapphire/archive/2010/04/08/1707109.html #运行用户 user  nobody nobody; #启动进程 wo ...

  9. C# 递归读取XML菜单数据

    在博客园注册了有4年了,很遗憾至今仍未发表过博客,趁周末有空发表第一篇博客.小生不才,在此献丑了! 最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据 ...

  10. VMware 彻底删除虚拟机操作系统的方法

    方法一 首先,都需要点击左边的虚拟机列表,选中你要删除的操作系统 点击VMwae上方的虚拟机-管理-从硬盘删除. 方法二 右键左侧列表中要删除的系统-移除. 然后在硬盘上找到其所在文件夹,直接按SHI ...