题目地址:https://leetcode-cn.com/problems/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:

  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 个数组,每个数组都已经按照升序排好序了。现在你需要从两个不同的数组中选择两个整数(每个数组选一个)并且计算它们的距离。两个整数 a 和 b 之间的距离定义为它们差的绝对值 |a-b| 。你的任务就是去找到最大距离

解题方法

大根堆+小根堆

由于数组都是已经排好序的,因此要想使绝对值差最大,只能是所有数组中的最大的最后一个数字的和所有数组中最小的第一个数字之差。要注意题目要求,当两者属于同一个数组时,需要使用次大值和次小值。

找出最大最小、次大次小的方法可以使用堆,具体而言是用大根堆小根堆分别保存最大终点和最小起点,每个堆同时保存值和数组编号。先判断最大最小是否是同一数组,如果不是同一数组,那么直接返回;如果是同一数组,那么从堆中找出次大和次小,此时的绝对值差会是max(最大-次小,次大-最小)

C++代码如下:

class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> mins;
priority_queue<pair<int, int>> maxs;
for (int i = 0; i < arrays.size(); ++i) {
auto arr = arrays[i];
mins.push({arr[0], i});
maxs.push({arr[arr.size() - 1], i});
}
int res = INT_MIN;
auto min_first = mins.top(); mins.pop();
auto max_first = maxs.top(); maxs.pop();
if (min_first.second != max_first.second) {
return max_first.first - min_first.first;
}
auto min_second = mins.top();
auto max_second = maxs.top();
return max(max_second.first - min_first.first, max_first.first - min_second.first);
}
};

保存已有的最大最小

一种更简单的方法是,使用两个变量分别保存已经见到的最大curMax/最小curMin,对每个数组遍历的过程中,全局最大绝对值之差等于max(当前数组的最大值-curMin, curMax-当前数组的最小值

C++代码如下:

class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
const int N = arrays.size();
int curMin = 10010;
int curMax = -10010;
int res = 0;
for (auto& arr : arrays) {
res = max(res, (arr[arr.size() - 1] - curMin));
res = max(res, (curMax - arr[0]));
curMin = min(curMin, arr[0]);
curMax = max(curMax, arr[arr.size() - 1]);
}
return res;
}
};

日期

2019 年 9 月 19 日 —— 举杯邀明月,对影成三人

【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)的更多相关文章

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

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

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

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

  5. LeetCode 349 Intersection of Two Arrays 解题报告

    题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...

  6. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  7. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  8. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  9. 【LeetCode】486. Predict the Winner 解题报告(Python)

    [LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

随机推荐

  1. 使用bioawk对基因组fasta序列ID(染色体/scaffold名称)排序?

    目录 需求 实现 需求 已知某基因组序列,染色体或scaffold ID顺序不定,想要对其按数字排序. 原顺序: 想要的排序结果: 实现 使用bioawk,没有的话conda直接安装. bioawk ...

  2. Linux-root管理员创建新用户

    Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...

  3. 作为Java技术面试官,我如何深挖候选人的技能

    作为Java资深技术面试官,首先我感觉有必要讲解"面试官深挖问题"的动机,在了解动机的前提下,大家才能更好地准备面试.面试官为什么要在一个点上深挖?两大目的.   1 首先是通过深 ...

  4. 半天做完的数据报表,YonBuilder只要十几分钟,0代码开发

    进入数字化时代,拍脑袋的决策方式显然不靠谱,一切要靠数据说话.与信息化时代相比,数字化时代的企业对数据的应用更广泛.更深入.为了应对激烈的市场竞争,企业经营决策者们对数据的依赖度越来越高,企业各个业务 ...

  5. HTML5 之 FileReader 的使用 (二) (网页上图片拖拽并且预显示可在这里学到) [转载]

    转载至 : http://www.360doc.com/content/14/0214/18/1457948_352511645.shtml FileReader 资料(英文): https://de ...

  6. AI作曲的一个点子

    通常的AI作曲都是通过拆分音乐为几个声道, 然后再把各个声道拆成音符去分析. 我忽然之间有个想法,是否可以继续拆分下去. 音符就是一些有规则的高低电平,这样把音符拆成电平. 一定会带来巨大的运算,但如 ...

  7. jenkins之邮箱设置

  8. Dubbo服务调用超时

    服务降级的发生,其实是由于消费者调用服务超时引起的,即从发出调用请求到获取到提供者的响应结果这个时间超出了设定的时限.默认服务调用超时时限为1秒.可以在消费者端与提供者端设置超时时限. 一.创建提供者 ...

  9. 【Matlab】imagesc的使用

    imagesc(A) 将矩阵A中的元素数值按大小转化为不同颜色,并在坐标轴对应位置处以这种颜色染色 imagesc(x,y,A) x,y决定坐标范围 x,y应是两个二维向量,即x=[x1 x2],y= ...

  10. C++内存管理:new / delete 和 cookie

    new 和 delete C++的内存申请和释放是通过 new 和 delete 实现的, 而new 和 delete 其实就是通过 malloc 和 free 实现的. new 申请内存分为三个步骤 ...