[抄题]:

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;
}
}

[代码风格] :

最新文章

  1. python程序生成平均脸
  2. mux复用 demux解复用
  3. svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
  4. JavaScript - BOM
  5. jquery实现自动滚屏效果,适用用公告新闻等滚屏
  6. Web大文件下载控件更新-Xproer.HttpDownloader
  7. Effective Java 29 Consider typesafe heterogeneous containers
  8. [视频]ARM告诉你物联网怎么玩,mbed 6LoWPan demo
  9. java第一天的疑问
  10. 【转】将 Linux 应用程序移植到 64 位系统上
  11. 微信公众号批量爬取java版
  12. 一个简单的定时表达式(HH:mm:ss)解析
  13. SSM-SpringMVC-08:SpringMVC中以继承AbstractController的方式实现处理器
  14. Maven 打包项目 部署到服务器 重启服务 插件
  15. What’s WOYO PDR-007 Paintless Dent Repair Heat Induction?
  16. Qt532.QString_填充字符
  17. BZOJ 2763 飞行路线 BFS分层
  18. Codeforces Round #489 (Div. 2)
  19. 吴恩达 Deep learning 第一周 深度学习概论
  20. PhysX SDK

热门文章

  1. vue项目打包注意的地方
  2. Spring与RMI集成实现远程访问
  3. 未定义的标示符“RECT”,引入了windows.h头文件也没有用?
  4. HDU - 5306: Gorgeous Sequence (势能线段树)
  5. Unity4.6 UGUI 图片打包设置(小图打包成图集 SpritePacker)
  6. Azure上每个VM多个IP地址
  7. java代码输出1到100的质数
  8. CRC 简介
  9. xcode修改横屏
  10. angular使用代理解决跨域