[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

差的绝对值最大有两种情况:最大减最小、最小减最大

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 应该理解下:max min都是全组共享的,所以max = Math.max(max, a)很常用

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

忘记链表的get方法怎么写了

[关键模板化代码]:

min = Math.min(min, arrays.get(i).get(0));
max = Math.max(max, arrays.get(i).get(arrays.get(i).size() - 1));

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

class Solution {
public int maxDistance(List<List<Integer>> arrays) {
//cc
if (arrays == null || arrays.size() == 0) {
return 0;
} //ini: max min res
int res = Integer.MIN_VALUE;
int min = arrays.get(0).get(0);
int max = arrays.get(0).get(arrays.get(0).size() - 1); //for loop, update max min
for (int i = 1; i < arrays.size(); i++) {
res = Math.max(res, Math.abs(arrays.get(i).get(0) - max));
res = Math.max(res, Math.abs(arrays.get(i).get(arrays.get(i).size() - 1) - min)); min = Math.min(min, arrays.get(i).get(0));
max = Math.max(max, arrays.get(i).get(arrays.get(i).size() - 1));
} //return res
return res;
}
}

[代码风格] :

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. 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. BAT面试题 - 找一个无序实数数组中的最大差值

    题目描写叙述: 一个无序的实数数组a[i].要求求里面大小相邻的实数的差的最大值.比方 double a[]={1,5,4,0.2,100} 这个无序的数组,相邻的数的最大差值为100-5=95. 题 ...

  6. 灵魂拷问:如何检查Java数组中是否包含某个值 ?

    在逛 programcreek 的时候,我发现了一些专注细节但价值连城的主题.比如说:如何检查Java数组中是否包含某个值 ?像这类灵魂拷问的主题,非常值得深入地研究一下. 另外,我想要告诉大家的是, ...

  7. php usort 按照数组中的某个键值排序

    //php usort 按照数组中的某个键值排序 如果第一个参数小于第二个参数 -> 返回小于0的整数如果第一个参数等于于第二个参数 -> 返回等于0的整数如果第一个参数大于于第二个参数 ...

  8. in_array 查询数组中是否存在某个值

    (PHP 4, PHP 5) in_array — 检查数组中是否存在某个值 说明 bool in_array ( mixed $needle , array $haystack [, bool $s ...

  9. PHP使用in_array函数检查数组中是否存在某个值

    PHP使用 in_array() 函数检查数组中是否存在某个值,如果存在则返回 TRUE ,否则返回 FALSE. bool in_array( mixed needle, array array [ ...

随机推荐

  1. LINUX CENTOS关机与重启命令详解

    Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.s ...

  2. oracle for loop 简单

    declare i NUMBER; begin loop INSERT INTO emp VALUES(i,i); end LOOP; END;

  3. Asp.Net MVC session跨域

    目的 在公司项目的某个特定场景中,需要在站点B的后端伪造请求,获取站点A的登录状态,抓取站点A的页面内容,因此要用实现session的跨域.以注册功能为例. 步骤 原理 简单地说,对于ASP.Net应 ...

  4. (转)根据ImageView的大小来压缩Bitmap,避免OOM

    本文转载于:http://www.cnblogs.com/tianzhijiexian/p/4254110.html Bitmap是引起OOM的罪魁祸首之一,当我们从网络上下载图片的时候无法知道网络图 ...

  5. LeetCode OJ:Subsets(子集)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  6. SQL使用指南(1)—— 数据定义语言(DDL)

    1.使用create 语句创建表 CREATE TABLE table_name (column_name datatype[null|not null], column_name datatype[ ...

  7. 每天一个linux命令(14):less命令

    版权声明更新:2017-05-18博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...

  8. BZOJ4861 [Beijing2017]魔法咒语

    题意 Chandra 是一个魔法天才.从一岁时接受火之教会洗礼之后, Chandra 就显示出对火元素无与伦比的亲和力,轻而易举地学会种种晦涩难解的法术.这也多亏 Chandra 有着常人难以企及的语 ...

  9. 算法之python创建链表实现cache

    算法之python创建链表实现cache 本节内容 问题由来 解决思路 实现代码 总结 1. 问题由来 问题起因于朋友的一次面试题,面试公司直接给出两道题,要求四十八小时之内做出来,语言不限,做出来之 ...

  10. C# Message 消息处理

    一.消息概述 Windows下应用程序的执行是通过消息驱动的.消息是整个应用程序的工作引擎,我们需要理解掌握我们使用的编程语言是如何封装消息的原理.C#自定义消息通信往往采用事件驱动的方式实现,但有时 ...