原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/

题目:

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 拿出首尾两值作为min, max value. 接下来的array 中,最大的distance只能从max-array.get(0) 和 array.get(array.size()-1) - min中选较大值, 同时更新min 和 max值. 维护最大的distance在res中.

Time Complexity: O(n). n = arrays.size().

Space: O(1).

AC Java:

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

LeetCode Maximum Distance in Arrays的更多相关文章

  1. [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 ...

  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 数组中的最大距离

    Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from t ...

  4. 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...

  5. 624. Maximum Distance in Arrays

    Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...

  6. 624. Maximum Distance in Arrays二重数组中的最大差值距离

    [抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...

  7. [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 ...

  8. [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  9. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

随机推荐

  1. python之路:进击的小白

    1.hello world print("hello world") 2.变量定义的规则 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声 ...

  2. pyhton3 re模块

    本文转自 AstralWind 的博客:Python正则表达式指南 特来收藏 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有 ...

  3. 使用CoreData存储数据

    - (void)viewDidLoad { [super viewDidLoad]; //获取模型文件的路径 NSString *path=[[NSBundle mainBundle]pathForR ...

  4. 每天一个Linux命令(41)iostat命令

        iostat是I/O statistics(输入/输出统计)的缩写,对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况.     (1)用法:     ...

  5. 计算机网络概述---OSI参考模型

    应用层:所有能产生网络流量的程序,例如:qq等,txt记事本没有产生流量,所以不属于应用层: 表示层:在传输之前对应用层的数据进行加工或处理,例如:加密.压缩.传视频时二进制,传文档时ASCII码 等 ...

  6. [POI2007]立方体大作战tet

    题目 BZOJ 洛谷 做法 很巧妙的题,注意每种颜色只有两个 消除一种颜色,其实就是看中间有多少个没有被消除的块,这种动态距离问题显然能用树状数组解决 洛谷输出方案,暴力往下爬就行 My comple ...

  7. PHP验证是否为图片格式文件

    /** * 判断是否为图片格式(jpg/jpeg/gif/png)文件 * * @param string $filePath * @return bool|string */ function is ...

  8. awk中的常用关于处理字符串的函数

    1.替换字符串中的某一部分. 函数:gensub(/rexpr/,"replace","g","string"),gensub返回一个新的字 ...

  9. 基于Visual c++ 2012的php扩展开发 - 环境搭建

    软件准备 Apache2.4 php-5.6.20-Win32-VC11-x86 php-5.6.20-src mysql-5.5.45-win32 vcredist_x86.exe vs2012旗舰 ...

  10. Docker 数据管理-bind mount

    Use bind mounts Bind mounts have been around since the early days of Docker. Bind mounts have limite ...