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. Collaborative Knowledge base Embedding (CKE)

    Collaborative Knowledge base Embedding (CKE) 在推荐系统中存在着很多与知识图谱相关的信息,以电影推荐为例: 结构化知识(structural knowled ...

  2. jmeter-多用户循环执行(存储token)

    1.从cvs文件中读取数据 登录接口读取文件: 2.读取token,保存token 在登录接口下添加 设置: 把token保存为全局变量: 设置: 输入${__setProperty(newtoken ...

  3. vue 弹框

    弹框展示: 代码: <template> <div> <el-col :span="9" style="text-align: right; ...

  4. 3、Python的IDE之Jupyter的使用

    一.Jupyter介绍 Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown.用途包括:数据清理和转换,数 ...

  5. Json在序列化getter导致的问题

    Java中的Json序列化,不容忽视的getter 问题重现 public class AjaxJson { private boolean success; private String msg; ...

  6. MapReduce如何解决数据倾斜?

    数据倾斜是日常大数据查询中隐形的一个BUG,遇不到它时你觉得数据倾斜也就是书本博客上的一个无病呻吟的偶然案例,但当你遇到它是你就会懊悔当初怎么不多了解一下这个赫赫有名的事故. https://www. ...

  7. python - 对接微信支付(PC)和 注意点

    注:本文仅提供 pc 端微信扫码支付(模式一)的示例代码. 关于对接过程中遇到的问题总结在本文最下方. 参考: 官方文档,    https://blog.csdn.net/lm_is_dc/arti ...

  8. C#线程池 ThreadPool

    什么是线程池 大家都知道,我们在打开一个应用的时候,操作系统是要做很多的事情的,动态链接.装载.分配虚拟空间.等等等等,其实一个应用的打开同时也伴随着一个进程的建立. 进程的建立是需要时间的,在进程上 ...

  9. OI歌曲汇总

    在学习的间隙,我们广大的OIer创作了许多广为人知的歌曲 这里来个总结 (持续更新ing......) Lemon OI 葛平 Lemon OI chen_zhe Lemon OI kkksc03 膜 ...

  10. C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source

    提示说是预编译出现问题,提示添加头文件stdafx.h,但是添加了也会继续有其他错误解决方法: 在菜单Project->Properties(或者直接快捷键Alt+F7)->C/C++-& ...