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. Jquery基础添加删除内容

    直入主题,工作中比较常用的功能在input框内添加内容,不白话了,上代码! 布局: <div id="content"> <input type="te ...

  2. Vue-cli创建项目从单页面到多页面

    vue-cli创建项目从单页面到多页面 对于某些项目来说,单页面不能很好的满足需求,所以需要将vue-cli创建的单页面项目改为多页面项目. 需要修改以下几个文件: 1.下载依赖glob $npm i ...

  3. Mybatis源码解析-DynamicSqlSource和RawSqlSource的区别

    XMLLanguageDriver是ibatis的默认解析sql节点帮助类,其中的方法其会调用生成DynamicSqlSource和RawSqlSource这两个帮助类,本文将对此作下简单的简析 应用 ...

  4. 导出含有图片的Java项目,图片不显示

    项目的一些图片资源文件在导出成JAR包后,无法正确读取虽然Java项目还是可以运行,但原来的图片资源全不见了,于是你可以打开JAR包看看里面的东西,确实是有图片在里面,就是无法读取. 其实是因为我们在 ...

  5. shell二位数组——终端字符下降动画

    猜想:Shell支持关联数组,可以利用关联数组模拟二维数组. [验证猜想] #!/bin/bash array[1,1]=1 array[2,1]=2 array[3,1]=3 for i in `s ...

  6. JavaWeb(一)Servlet中的ServletConfig与ServletContext

    前言 前面我介绍了一下什么是servlet,它的生命周期,执行过程和它的原理.这里我们做一个简单的回顾! 什么是Servlet? servlet 是运行在 Web 服务器中的小型 Java 程序(即: ...

  7. ssh项目访问路径及url请求书写

    在ssh项目中配置好Struts后,一般可以采用两种方式进行后台请求: 1.html形式,包括a标签,form表单,ajax等.此时的访问链接必须写全路径,可以是相对路径,也可以是绝对路径 相对路径方 ...

  8. C#中System.DateTime.Now.ToString()用法

    //Asp.net中的日期处理函数     //2008年4月24日     System.DateTime.Now.ToString("D");     //2008-4-24  ...

  9. VisualStudio快捷键大全

    Ctrl+m+Crtr+o折叠所有大纲Ctrl+M+Crtr+P: 停止大纲显示Ctrl+K+Crtr+C: 注释选定内容Ctrl+K+Crtr+U: 取消选定注释内容Ctrl+J : 列出成员 智能 ...

  10. install xdebug

    安装准备 安排php的xdebug扩展,在php.ini上配置xdebug.通过phpinfo或者php-m 查看 [Xdebug] zend_extension ="D:\upupw7\P ...