LeetCode Maximum Distance in Arrays
原题链接在这里: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:
- Each given array will have at least 1 number. There will be at least two non-empty arrays.
- The total number of the integers in all the
marrays will be in the range of [2, 10000]. - The integers in the
marrays 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的更多相关文章
- [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 ...
- 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 ...
- 624. Maximum Distance in Arrays二重数组中的最大差值距离
[抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...
- [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] 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 ...
- LeetCode:Maximum Depth of Binary Tree_104
LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...
随机推荐
- 乐思启慧教学系列—Bootstrap布局规则
1外层变化,内层相应变化规则 col-md-6 col-md-4 外层6变成12,扩大了2倍,里面就得缩小2倍(除以2), 只有这样才能保持外部变化了,内部依然对齐 col-md-12 col-md- ...
- 【转】dmesg 时间转换
dmesg 时间转换 dmesg 输出的格式不易查看,可以通过命令进行转换. 记录如下: 时间查看: date -d "1970-01-01 UTC `echo "$(date + ...
- C# Xml Linq XDocument 基本操作 -- 重新学习
person.xml <?xml version="1.0" encoding="utf-8"?> <MyP> <P1> & ...
- $Android自定义控件在不同状态下的属性
在写代码的时候,有时候需要控件在不同状态下显示不同的外观,比如在按钮按下的时候要变颜色,EditText获取焦点时候边框要变颜色等.那么下面就来梳理一下这些是怎么实现的. (一)按钮按下时候变颜色 1 ...
- Linux权限管理 chattr命令、lsattr命令、sudo命令
chattr命令 chattr命令用来修改文件系统的权限属性 chatrr 只有 root 用户可以使用,用来修改文件系统的权限属性,建立凌驾于 rwx 基础权限之上的授权. chatrr 命令格式如 ...
- Python字符串格式转换
转换类型 转换类型 说明 d, i 带符号十进制 b 无符号二进制 o 无符号八进制 u 无符号十进制 x 无符号十六进制(小写) X 无符号十六进制(大写) e 科学计数法表示的浮点数(小写) E ...
- RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 是什么意思?
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENA ...
- tp3.2关联模型 BELONGS_TO
<?php namespace Home\Model; use Think\Model\RelationModel; class AttenModel extends RelationModel ...
- 操作文件和目录【TLCL】
cp – Copy files and directories mv – Move/rename files and directories mkdir – Create directories rm ...
- centos下安装python2.7.9和pip以及数据科学常用的包
以前一直用ubantu下的python,ubantu比较卡.自己倾向于使用centos,但默认的python版本太低,所以重新装了一个python和ipython centos6.5安装python2 ...