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 [ ...
随机推荐
- CMC 实例管理
有人问我,用户用的BW-QUERY看报表挺快的,用了BO发现很慢. 我心想,不会是什么高级优化吧,我可不会. 发现用WEBI时看报表很慢.那这个还是好解决的. 前面说那种情况,解决方法我只知道一种上H ...
- ionic2——学习指引-学习资源汇总
Ionic2 官网............................官网的文档非常好,超级全,一定要细心看中文文档.....................比较简单 Angular 2 官网.. ...
- Eclipse插件开发_学习_01_Maven+Tycho 构建RCP程序
二.参考资料 1.用Tycho来构建你的RCP程序(一) —— Plugin
- php实现word在线浏览功能。
http://laoniangke.com/php/2012/10/08/php-doc-webview.html
- Java 输入参数并求和
题目要求: 输入若干的参数,编写一个程序求出参数的和: 程序流程: 代码: 例子:
- swing之记事本的简单实现
package gui1; import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JButton ...
- rabbitmq的vhost与用户管理
当我们在创建用户时,会指定用户能访问一个虚拟机,并且该用户只能访问该虚拟机下的队列和交换机,如果没有指定,默认的是”/”;一个rabbitmq服务器上可以运行多个vhost,以便于适用不同的业务需要, ...
- navicat链接远程数据库
1.之前使用的是常规的连接方式 学习源头: https://jingyan.baidu.com/article/0aa2237573c1e688cc0d6427.html 这里的ip地址是服务器的ip ...
- openTSDB+HBase+ZK遇到的坑汇总
1.zookeeper返回的hbase地址是hostname,外网如何访问? 如果需要直接访问zk获取hbase地址进而访问,目前需要本机配置host ip hostname 如果是要长期解决方法, ...
- POJ2831(次小生成树问题)
Can We Build This One? Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1475 Accepted: ...