[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 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].
给m个数组, 每个数组按升序排列。现在你可以从2个不同的数组中各取1个数字,计算他们的距离,距离是两个数值差的绝对值。任务是找出最大距离。
注意要从不同数组中取数,那么即使某个数组的首尾差距很大,也不行。
解法1:最大堆和最小堆。空间复杂度高。
解法2:用min_val和max_val表示当前遍历到的数组中最小的首元素和最大的尾元素,当遍历到一个新的数组时,计算新数组尾元素和min_val绝对差以及max_val和新数组首元素的绝对差,取较大值和result比较来更新结果,最后返回result。
Java:
public class Solution {
public int maxDistance(int[][] list) {
int res = 0, min_val = list[0][0], max_val = list[0][list[0].length - 1];
for (int i = 1; i < list.length; i++) {
res = Math.max(res, Math.max(Math.abs(list[i][list[i].length - 1] - min_val), Math.abs(max_val - list[i][0])));
min_val = Math.min(min_val, list[i][0]);
max_val = Math.max(max_val, list[i][list[i].length - 1]);
}
return res;
}
}
Python:
# Time: O(n)
# Space: O(1)
class Solution(object):
def maxDistance(self, arrays):
"""
:type arrays: List[List[int]]
:rtype: int
"""
result, min_val, max_val = 0, arrays[0][0], arrays[0][-1]
for i in xrange(1, len(arrays)):
result = max(result, max(max_val - arrays[i][0], arrays[i][-1] - min_val))
min_val = min(min_val, arrays[i][0])
max_val = max(max_val, arrays[i][-1])
return result
C++:
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
priority_queue<pair<int, int>> mx, mn;
for (int i = 0; i < arrays.size(); ++i) {
mn.push({-arrays[i][0], i});
mx.push({arrays[i].back(), i});
}
auto a1 = mx.top(); mx.pop();
auto b1 = mn.top(); mn.pop();
if (a1.second != b1.second) return a1.first + b1.first;
return max(a1.first + mn.top().first, mx.top().first + b1.first);
}
};
C++:
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
int res = 0, start = arrays[0][0], end = arrays[0].back();
for (int i = 1; i < arrays.size(); ++i) {
res = max(res, max(abs(arrays[i].back() - start), abs(end - arrays[i][0])));
start = min(start, arrays[i][0]);
end = max(end, arrays[i].back());
}
return res;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 624. 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 ...
- 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 解题报告(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 ...
- 【python】Leetcode每日一题-删除有序数组中的重复项
[python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...
- 【python】Leetcode每日一题-删除有序数组中的重复项2
[python]Leetcode每日一题-删除有序数组中的重复项2 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不 ...
- LeetCode Maximum Distance in Arrays
原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
随机推荐
- mysql终止当前正在执行的sql语句
show processlist;找出你要停止的语句然后找出你要终止的语句的idkill 248
- js 变量以及函数传参
一.变量: 基本类型是变量对象重新创建一个新值给变量对象空间,虽然是同一个值但是互不影响. 引用类型是也是将一个值重新赋值给新的变量空间,但是这个值是堆中对象的一个指针,新的变量和旧的变量指向是同一个 ...
- js实现文本框自动显示两位小数
转自https://blog.csdn.net/qiji2011/article/details/81270552 1.js: //保留2位小数,如:2,会在2后面补上00.即2.00 functio ...
- 关于jsp页面中name=“username”与name=“username ”的区别
我们可以仔细的观察一下,上面的name属性都等于username,但是确实存在大同小异的差距,为什么这样说呢,因为,第二个比第一个多了一个空格,在jsp中,我曾经遇到过一个情况就是两个单选按钮用同一个 ...
- Dubbo源码分析:Invoker
背景 调用对象!在调用过程可以使用Filter接口方法.Inovoker调用过程采用了装饰者设计模式.Filter最后一个ExcpetionFilter对象,这个对象之后就调用服务方法.服务对象是配置 ...
- cifar-10数据集的可视化
import numpy as np from PIL import Image import pickle import os CHANNEL = 3 WIDTH = 32 HEIGHT = 32 ...
- MySQL 开启远程链接(localhost 以外的主机)
1.在连接服务器后,操作mysql系统数据库 命令为: mysql -u root -p use mysql: 查询用户表命令:select User,authentication_string, ...
- c++ 将float 类型转换成string 类型
string Convert(float Num) { ostringstream oss; oss<<Num; string str(oss.str()); return str; }
- 【洛谷P4931】 情侣?给我烧了!(加强版)组合计数
挺有意思的一道题... code: #include <bits/stdc++.h> using namespace std; #define N 5000006 #define mod ...
- jedis的连接池
1.需要先打开虚拟机,并开启Linux系统的端口号:6379: 其中,第一行代码为修改字符编码格式,解决SSH中文乱码问题. 2.开启redis: 3.利用连接池实现数据的存取: (1)代码实现: i ...