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

这道题给我们了一些数组,每个数组都是有序的,让我们从不同的数组中各取一个数字,使得这两个数字的差的绝对值最大,让我们求这个最大值。那么我们想,既然数组都是有序的,那么差的绝对值最大的两个数字肯定是分别位于数组的首和尾,注意题目中说要从不同的数组中取数,那么即使某个数组的首尾差距很大,也不行。博主最先考虑的是用堆来做,一个最大堆,一个最小堆,最大堆存每个数组的尾元素,最小堆存每个数组的首元素,由于最大的数字和最小的数字有可能来自于同一个数组,所以我们在堆中存数字的时候还要存入当前数字所在的数组的序号,最后我们其实要分别在最大堆和最小堆中各取两个数字,如果最大的数字和最小的数字不在一个数组,那么直接返回二者的绝对差即可,如果在的话,那么要返回第二大数字和最小数字绝对差跟最大数字和第二小数字绝对差中的较大值,参见代码如下:

解法一:

class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
priority_queue<pair<int, int>> mx, mn;
for (int i = ; i < arrays.size(); ++i) {
mn.push({-arrays[i][], 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);
}
};

下面这种方法还是很不错的,并没有用到堆,而是用两个变量start和end分别表示当前遍历过的数组中最小的首元素,和最大的尾元素,那么每当我们遍历到一个新的数组时,只需计算新数组尾元素和start绝对差,跟end和新数组首元素的绝对差,取二者之间的较大值来更新结果res即可,参见代码如下:

解法二:

class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
int res = , start = arrays[][], end = arrays[].back();
for (int i = ; i < arrays.size(); ++i) {
res = max(res, max(abs(arrays[i].back() - start), abs(end - arrays[i][])));
start = min(start, arrays[i][]);
end = max(end, arrays[i].back());
}
return res;
}
};

参考资料:

https://discuss.leetcode.com/topic/92858/c-o-n

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Maximum Distance in Arrays 数组中的最大距离的更多相关文章

  1. [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 ...

  2. LeetCode Maximum Distance in Arrays

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

  3. LeetCode:寻找旋转排序数组中的最小值【153】

    LeetCode:寻找旋转排序数组中的最小值[153] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0 ...

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

  5. 624. Maximum Distance in Arrays二重数组中的最大差值距离

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

  6. 【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...

  7. 624. Maximum Distance in Arrays

    Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...

  8. [LeetCode] Longest Mountain in Array 数组中最长的山

    Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...

  9. 领扣(LeetCode)寻找旋转排序数组中的最小值 个人题解

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重 ...

随机推荐

  1. java日期格式大全 format SimpleDateFormat

    http://www.cnblogs.com/loveyakamoz/archive/2011/08/18/2145047.html

  2. ASP.NET Core MVC 2.1 顶级参数验证

    本文讨论ASP.NET Core 2.1中与ASP.NET Core MVC / Web API控制器中的模型绑定相关的功能.虽说这是一个功能,但从我的角度来看,它更像是一个错误修复! 请注意,我使用 ...

  3. Wannafly交流赛1(施工中)

    A.有理数 签到题:直接用floor函数就行了,详细看代码 #define debug #include<stdio.h> #include<math.h> #include& ...

  4. 使用Python中的mock模块进行单元测试

    在进行单元测试的时候,有时候会遇到这种情况: 出于某些原因,我们不想测试某一部分内容,但是我们想要测试的部分却依赖这部分内容. 这时候,可以使用mock模块来模拟调用这部分内容,并给出返回结果,举例如 ...

  5. Java虚拟机之性能监控

    一.jstat:虚拟机统计信息监控工具监视虚拟机各种运行状态 图中,S0.S1(Survivor0.Survivor1)代表两个Survivor区,其中一个值为57.60%.另一个为0.E(Eden) ...

  6. Beta Scrum Day 1

    听说

  7. 201621123040 《Java程序设计》第1周学习总结

    1.本周学习总结 关键词 JAVA概述 HelloWorld JDK JRE JVM JAVA基础语法 相关联系 通过一周的学习,我对JAVA有了初步的了解,JAVA是一种优秀的跨平台编写代码的应用平 ...

  8. Environment.getExternalStorageDirectory()

    Environment.getExternalStorageDirectory()得到的是storage/emulated/0

  9. 使用JavaScript实现一个俄罗斯方块

    清明假期期间,闲的无聊,就做了一个小游戏玩玩,目前游戏逻辑上暂未发现bug,只不过样子稍微丑了一些-.-项目地址:https://github.com/Jiasm/tetris在线Demo:http: ...

  10. Web Api HttpWebRequest 请求 Api 及 异常处理

    HttpWebRequest request = WebRequest.CreateHttp(url); request.Method = "post"; request.Head ...