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 ...
随机推荐
- 系统非正常关机启动后出现:an error occurred during the file system
现象描述: 1.系统ssh登录报Too many open files in system,系统登录不进去,就直接强制关机了,开机后出现(2)的错误: 由于文件描述符用完了,需要把fs.file-ma ...
- active admin
activeadmin 1.0.0.pre4 所依赖的库 gem 'jquery-rails', '~> 4.0.4' 4.2版本会出现找不到jquery-ui 的datepicker错误 使用 ...
- HTML系列(3)基本的HTML标签(二)
本节继续介绍HTML的常用标签. (1)input标签之文本域(text和textarea).密码域(password): <!DOCTYPE html> <html ...
- Python编程-多进程一
一.python并发编程之多进程 1.multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyth ...
- Swift中的? ! as as? as!
?: 代表这是个可选类型(optional)的.如下,如果num有就为Int类型的,如果没有值那么就是nil. let num:Int? 当我对number进行显示赋值时那么number就是Int类型 ...
- Java Hibernate 5.3.x
SchemeExport Hibernate根据实体类和实体类映射文件自动生成表结构. 示例代码: <?xml version='1.0' encoding='utf-8'?> <! ...
- 使用shell统计字符串出现的次数,并从大到小进行排序显示
- HttpClient技术
1.什么是HttpClient? 2.HttpClient特点? 特点: 2.1. 基于标准.纯净的Java语言.实现了Http1.0和Http1.1 2.2. 以可扩展的面向对象的结构实现了Http ...
- 为WebBrowser指定IE内核版本(MSIE 7.0)
.Web Browser Control – Specifying the IE Version http://www.west-wind.com/weblog/posts/2011/May/21/W ...
- UVA 11186 Circum Triangle (枚举三角形优化)(转)
题意:圆上有n个点,求出这n个点组成的所有三角形的面积之和 题解: 当我们要求出S(i,j,k)时,我们需要假设k在j的左侧,k在i与j之间,k在i的右侧. 如果k在 j的左侧 那么 S(i,j,k ...