624. Maximum Distance in Arrays二重数组中的最大差值距离
[抄题]:
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]:
[思维问题]:
[一句话思路]:
差的绝对值最大有两种情况:最大减最小、最小减最大
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 应该理解下: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二重数组中的最大差值距离的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...
- 624. Maximum Distance in Arrays
Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...
- BAT面试题 - 找一个无序实数数组中的最大差值
题目描写叙述: 一个无序的实数数组a[i].要求求里面大小相邻的实数的差的最大值.比方 double a[]={1,5,4,0.2,100} 这个无序的数组,相邻的数的最大差值为100-5=95. 题 ...
- 灵魂拷问:如何检查Java数组中是否包含某个值 ?
在逛 programcreek 的时候,我发现了一些专注细节但价值连城的主题.比如说:如何检查Java数组中是否包含某个值 ?像这类灵魂拷问的主题,非常值得深入地研究一下. 另外,我想要告诉大家的是, ...
- php usort 按照数组中的某个键值排序
//php usort 按照数组中的某个键值排序 如果第一个参数小于第二个参数 -> 返回小于0的整数如果第一个参数等于于第二个参数 -> 返回等于0的整数如果第一个参数大于于第二个参数 ...
- in_array 查询数组中是否存在某个值
(PHP 4, PHP 5) in_array — 检查数组中是否存在某个值 说明 bool in_array ( mixed $needle , array $haystack [, bool $s ...
- PHP使用in_array函数检查数组中是否存在某个值
PHP使用 in_array() 函数检查数组中是否存在某个值,如果存在则返回 TRUE ,否则返回 FALSE. bool in_array( mixed needle, array array [ ...
随机推荐
- element-ui树结构懒加载
在实际项目中,往往树结构数据量较大,这时树节点必须懒加载 element-ui树的懒加载: <div style="width:100%;height:420px;overflow: ...
- xftp5+xshell5工具安装包分享
身为开发人员,常常为这两个工具安装包找不到资源为难!尴尬了... 这次专门写篇博客,记录资源,并分享给大家. 上链接: 链接:https://pan.baidu.com/s/1mRNxHhr7F2Q_ ...
- 条款49:了解new-handle行为
多线程下的内存管理与单线程下是完全不同的,因为heap是一个可以被全局改动的资源,所以所有的线程都有可能去访问这一资源,这回导致很多的race_conditions. 当operator new未 ...
- javascript.history.go();
转自:http://www.mikebai.com/Article/2009-11/757.html <input type=button value=刷新 onclick="wind ...
- SQL Server数据文件迁移
需求:源SQL Server安装目录及数据目录 与 目标SQL Server安装目录及数据目录 完全不同. 步骤: 1.拷贝源数据目录下需要移植的库文件(rpBrInfo_TA.mdf.rpBrInf ...
- 【转】深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...
- 前端跨域问题,以及ajax,jsonp,json的区别
看了很多网上的资料,小七感觉都没有完全解决我的疑惑以及问题,所以特意拿出通俗易懂的话讲解跨域问题,以及ajax,jsonp,json的区别.首先先说跨域问题什么时候需要跨域?[1]域名不同(即网址不同 ...
- BZOJ2120:数颜色(分块版)
浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- mysql 自增id
在开发的时候遇到了 自增id变成2147483647 莫名其妙 然后发现是自己没把自增id改为 无符号的原因 把无符号勾上就ok了
- GWT中自定义你的"cell"
GWT内部提供了CellTable组件,它允许自由增加column以及cell,在设定column之后就是在其中填充cell了.但GWT所提供的CellTable样式确实不敢恭维,为了解决这一问题,在 ...