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. python基础之元组,集合

    一.元组 为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(,[,],,)) #t=tuple((,[,],,))) print(type(t)) 元组可以作为字典的key d={(,, ...

  2. Servlet第二篇【Servlet调用图、Servlet细节、ServletConfig、ServletContext】

    Servlet的调用图 前面我们已经学过了Servlet的生命周期了,我们根据Servlet的生命周期画出Servlet的调用图加深理解 Servlet的细节 一个已经注册的Servlet可以被多次映 ...

  3. ubuntu下程序员常用命令大全

    一.ubuntu下用命令查询系统版本 1.在终端中执行下列指令: cat /etc/issue 该命令可查看当前正在运行的ubuntu的版本号. 效果如图: 2.使用 lsb_release 命令也可 ...

  4. [js高手之路] 设计模式系列课程 - 迭代器(1)

    迭代器是指通过一种形式依次遍历数组,对象,或者类数组结构中的每个元素. 常见的有jquery中的each方法, ES5自带的forEach方法. 下面我们就来自定义一个类似jquery或者ES5的迭代 ...

  5. C的函数指针与指针函数

    1.函数指针 指向函数的指针.本质是一个指针. 指针变量可以指向变量的地址.数组.字符串.动态分配地址,同时也可指向一个函数,每个函数在编译的时候,系统会分配给该函数一个入口地址,函数名表示这个入口地 ...

  6. 深入理解计算机系统chapter6

    1. 2. 3. 存储器山

  7. SQL Server 2016 Alwayson新增功能

    标签:SQL SERVER/MSSQL SERVER/数据库/DBA/ 概述 SQLServer2016发布版本到现在已有一年多的时间了,目前最新的稳定版本是SP1版本.接下来就开看看2016在Alw ...

  8. Spring3.2不支持jdk8

    解决方案: http://stackoverflow.com/questions/24128045/spring-context-initialization-failed-with-java-lan ...

  9. 利用python多线程实现多个客户端与单个服务端的远程ssh

    本次实验设计两个方面的代码,第一个是客户端,代码如下: import os from socket import * c = socket(AF_INET,SOCK_STREAM) c.connect ...

  10. Ansible(三) - playbook简介

    Ⅰ. Playbook介绍 Playbook其实就是ansible的一个任务列表,各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个.在顺序运行某playb ...