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

题目标签:Array

  题目给了我们一个 2d array,让我们求2个数字之间的最大差值。2个数字不能来自于同一行。

  一开始想了一种方法,因为所有的array 都是 排序过的,那么只要在所有的array 的第一个数字里,找到一个min;在最后一个数字里,找到一个max,相减就是最大距离。

  但是这样,有可能出现一种情况,就是2个数字来自于同一行。不符合要求。那么这样的话,只要多维护一个min2, 和max2, 还有minRowNum 和 maxRowNum就可以了,当min 和max 的row num是相等的话,比较max - min2 和 max2 - min就可以。

  通过是没有问题,但是不够简洁。

  下面这种方法更简洁。

  设一个 maxDistance, min 和max。

  为了避免2个数字来自于同一行,我们只要先设定max 和 min 是第一行的数字。对于后面的每一行的 tempMax 和tempMin,  只要在 max - tempMin 和 tempMax - min 里取大的那一个 和 maxDistance 比较一下,留下大的。

  这样的话,每一次maxDistance 的2个数字,都是来自于不同行的。

Java Solution:

Runtime beats 97.61%

完成日期:10/15/2017

关键词:Array

关键点:利用排序的array,取第一个数字和最后一个数字 维护min ,max,和maxDistance

 class Solution
{
public int maxDistance(List<List<Integer>> arrays)
{
int min = arrays.get(0).get(0);
int max = arrays.get(0).get(arrays.get(0).size() - 1);
int maxDistance = Integer.MIN_VALUE; for(int i=1; i<arrays.size(); i++)
{
int tempMin = arrays.get(i).get(0);
int tempMax = arrays.get(i).get(arrays.get(i).size() - 1); maxDistance = Math.max(maxDistance, Math.max(max - tempMin, tempMax - min)); min = Math.min(min, tempMin);
max = Math.max(max, tempMax);
} return maxDistance;
}
}

参考资料:

https://discuss.leetcode.com/topic/92859/java-solution-min-and-max

LeetCode 题目列表 - LeetCode Questions List

LeetCode 624. Maximum Distance in Arrays (在数组中的最大距离)$的更多相关文章

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

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

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

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

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

  4. 624. Maximum Distance in Arrays

    Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...

  5. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

  6. [LeetCode每日一题]80. 删除有序数组中的重复项 II

    [LeetCode每日一题]80. 删除有序数组中的重复项 II 问题 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使用额外 ...

  7. 【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2

    [python]Leetcode每日一题-寻找旋转排序数组中的最小元素2 [题目描述] 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组nums ...

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

  9. LeetCode Maximum Distance in Arrays

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

随机推荐

  1. 超简单的js评价小星星

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Java中迭代器Iterator的使用

    Java集合类中Map接口下的相关类并没有像Collection接口的相关类一样实现get()方法,因此在要实现遍历输出的场景中没法直接用get()方法来取得对象中的数据,但Java本身提供了另一种遍 ...

  3. oracle pl/sql 函数

    函数用于返回特定的数据,当建立函数时,在函数头部必须包含return子句.而在函数体内必须包含return语句返回的数据.我们可以使用create function来建立函数. 1).接下来通过一个案 ...

  4. 编译httpd细节

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  5. QT的安装及环境配置

    QT的安装及环境配置 一.windows的下QT的安装及环境配置 (一)从框架安装程序中安装 步骤: 准备:下载QT库,下载指定版本的MINGW,QT IDE 1.下载QT安装文件如:qt-win-o ...

  6. bzoj4198 荷马史诗 哈夫曼编码

    逐影子的人,自己就是影子. --荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛>和&l ...

  7. [SDOI2011]工作安排

    Description 你的公司接到了一批订单.订单要求你的公司提供n类产品,产品被编号为1~n,其中第i类产品共需要Ci件.公司共有m名员工,员工被编号为1~m员工能够制造的产品种类有所区别.一件产 ...

  8. NOIP2017SummerTraining0717

    个人感受:自己水平是真的差劲,和他们不是一个档次的,第二题,如果不是陈载元暴力过了,我也不会那么早去A了第二题,第一题真的是无语,以前做到过,还想到了每个对应值a[i]-i,但是没想出来,真的是 可惜 ...

  9. servlet自动获取前端页面提交数据

    servlet自动获取前端页面jsp提交数据 以下是本人在学习过程中,因前端页面提交参数过多,后台servlet封装实体类过于麻烦而写的一个工具类,应用于jsp/servlet数据提交后,基于MVC+ ...

  10. PHP,Mysql根据经纬度计算距离并排序

    网上给出的计算公式是这样的: Lng1表示A点纬度和经度,Lat2 Lng2 表示B点纬度和经度 a = Lat1 – Lat2为两点纬度之差 b = Lng1 -Lng2 为两点经度之差 6378. ...