原题链接在这里: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. loadrunder之脚本篇——int类型和字符串的相互转换

    字符串转化为int型变量 Action2() { int j = 0; j = atoi("12345");  //将字符串变为整形 lr_output_message(" ...

  2. github资源下载速度慢的解决办法

    xx-net:https://github.com/XX-net/XX-Net

  3. linux创建指定大小的文件

    一.生成文件大小和实际占空间大小一样的文件 dd if=/dev/zero of=50M.file bs=1M count=50 dd if=/dev/zero of=20G.file bs=1G c ...

  4. Qt下TCP编程

    一.服务器 1.声明一个QTcpServer对象 QTcpServer* serverListener; 2.new出对象 this->serverListener = new QTcpServ ...

  5. ios开发在导入环信SDK后运行出现 Reason: image not found 的解决方案

    在导入环信的SDK后,运行出现:

  6. PAT1021. Deepest Root (25)

    之前不知道怎么判断是不是树,参考了 http://blog.csdn.net/eli850934234/article/details/8926263 但是最后有一个测试点有超时,在bfs里我用了数组 ...

  7. UvaLive 5811 概率DP

    题意 : 有54张牌 问抽多少张牌能使每种花色都至少是给定的数字 两张王牌可以被选择为任何花色 高放学长真是太腻害辣! 设置dp[][][][][x][y] 前四维代表四种真的花色的数量 后两维xy代 ...

  8. Java -- JDBC mysql读写大数据,文本 和 二进制文件

    1. 往mysql中读写字符文本 public class Demo1 { /* 创建数据库 create database LOBTest; use LOBTest; create table te ...

  9. JavaWeb -- JSP+JavaBean模式

    SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. JSP+JavaBean模式适合开发业务 ...

  10. pandas通过字典生成dataframe

    1.将一个字典输入: 该字典必须满足:value是一个list类型的元素,且每一个key对应的value长度都相同: (以该字典的key为columns) >>> import pa ...