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].

给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 数组中的最大距离的更多相关文章

  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. 624. Maximum Distance in Arrays二重数组中的最大差值距离

    [抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...

  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. 【python】Leetcode每日一题-删除有序数组中的重复项

    [python]Leetcode每日一题-删除有序数组中的重复项 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现一次 ,返回删除后数组的新长度. 不要 ...

  7. 【python】Leetcode每日一题-删除有序数组中的重复项2

    [python]Leetcode每日一题-删除有序数组中的重复项2 [题目描述] 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不 ...

  8. LeetCode Maximum Distance in Arrays

    原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...

  9. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

随机推荐

  1. P1505 [国家集训队]旅游[树剖]

    题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路 ...

  2. 第六周测试补交 多线程代码和sumN

    1.多线程代码 要求:编译运行多线程程序,提交编译和运行命令截图 2.sumN 要求:1-N求和的截图

  3. UVA - 12183 :Top Secret(N^2的循环矩阵乘法)

    pro:N个数排成一圈.一次操作为,每个位置的数+=L*左+R*右,保留x为整数. 问S轮操作后每个位置的值. N<=1000,S<=2^30,x<=9 . sol:不难想到矩阵乘法 ...

  4. test20190909 Gluttony

    0+0+0+0+0+0=0.毒瘤出题人. BJOI2019 勘破神机 地灾军团的军师黑袍从潜伏在精灵高层的密探手中得知了神杖的情报,他对奥术宝石中蕴含的远古神秘力量十分感兴趣.他设计夺取了数块奥术宝石 ...

  5. HTML5 自定义属性 data-*属性名一定要小写吗?

    最近学习 javascript ,参考书籍是< javascript 高级程序设计>第三版,在介绍自定义元素属性时书中给出了一个例子,如下:<div id="myDiv&q ...

  6. “Another git process seems to be running in this repository...”Git此问题解决

    Git中显示:Another git process seems to be running in this repository, e.g.an editor opened by 'git comm ...

  7. 11 open source business models

    https://www.zdnet.com/article/11-open-source-business-models/ Critics are always claiming open sourc ...

  8. Maven和Ajax

    ****************使用maven构建项目******************** 一个maven项目必须要有一个pom文件. maven中常用的命令: 在使用mvn archetype: ...

  9. ZOJ 2676 Network Wars(网络流+分数规划)

    传送门 题意:求无向图割集中平均边权最小的集合. 论文<最小割模型在信息学竞赛中的应用>原题. 分数规划.每一条边取上的代价为1. #include <bits/stdc++.h&g ...

  10. CDN工作机制

    CDN(content delivery network),即内容分布网络,是一种构建在现有Internet上的一种先进的流量分配网络.CDN以缓存网站中的静态数据为主,当用户请求动态内容时,先从CD ...