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 ...
随机推荐
- 正则表达式test match exec search
(1)((2))(3) $1 是第一个括号 $2 是第二个括号 $3 是第二个括号中的括号 $4 是第三个括号 http://www.jb51.net/article/28007. ...
- 我的npm笔记
本文记录一些npm的使用技巧,主要包括自己常用的命令,做个备忘. NPM 是什么? NPM是NodeJS的包管理工具,现在已经进一步发展,致力于为很多其他平台提供包管理工具,其核心思想就是让包的安装更 ...
- 安装MySQL的详细步骤
安装步骤如下: 1.打开网址:http://www.mysql.com/ ↓ 2.选择“Download”->“Windows”(此安装步骤只是在Window10 中进行,如有需要,其他系统可参 ...
- 快速查找文件——Everything
Everything Search Engine Locate files and folders by name instantly. Small installation file Clean a ...
- qt的登录设置(转)
1.下面添加代码来实现使用用户名和密码登录,这里只是简单将用户名和密码设置为了固定的字符串,如果以后学习了数据库,还可以通过读取数据库来获取用户名和密码.到logindialog.cpp文件中将登录按 ...
- MySQLdump导出sql脚本
1.问题描述 通过图形化工具,在查询窗口用select语句按条件查询出所需结果,然后用“导出向导”把查询结果导成sql文件,但是导出来的sql语句不全,没有表名.字段名. 通过图形化工具,试了好多次都 ...
- Luogu-4774 [NOI2018]屠龙勇士
这题好像只要会用set/平衡树以及裸的\(Excrt\)就能A啊...然而当时我虽然看出是\(Excrt\)却并不会...今天又学了一遍\(Excrt\),趁机把这个坑给填了吧 现预处理一下,找出每条 ...
- 操作文件和目录【TLCL】
cp – Copy files and directories mv – Move/rename files and directories mkdir – Create directories rm ...
- MySQL操作的相关命令
拷贝表,并且复制两条数据到新表中 create table t_comments_sample2 like t_comments_sample; #拷贝表结构 ,;#复制两条数据 MySQL Work ...
- DataX-MySQL(读写)
DataX操作MySQL 一. 从MySQL读取 介绍 MysqlReader插件实现了从Mysql读取数据.在底层实现上,MysqlReader通过JDBC连接远程Mysql数据库,并执行相应的sq ...