[LeetCode] 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
m
arrays will be in the range of [2, 10000]. - 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;
}
};
参考资料:
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Maximum Distance in Arrays 数组中的最大距离的更多相关文章
- [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 ...
- LeetCode Maximum Distance in Arrays
原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...
- LeetCode:寻找旋转排序数组中的最小值【153】
LeetCode:寻找旋转排序数组中的最小值[153] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0 ...
- 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 ...
- [LeetCode] Longest Mountain in Array 数组中最长的山
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- 领扣(LeetCode)寻找旋转排序数组中的最小值 个人题解
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重 ...
随机推荐
- Post Office
Post Office poj-1160 题目大意:给你在数轴上的n个村庄,建立m个邮局,使得每一个村庄距离它最近的邮局的距离和最小,求距离最小和. 注释:n<=300,m<=min(n, ...
- QuietHit小Game
根据项目的要求分别建出几个类 有游戏类 玩家类 测试类 等级类 等级时间类 一以下类图: 游戏类: public class Game { private Player player; public ...
- CountDownLatch 使用说明
CountDownLatch是一种java.util.concurrent包下一个同步工具类,它允许一个或多个线程等待直到在其他线程中一组操作执行完成. CountDownLatch的用法非常简单,下 ...
- 20162320刘先润第三周Bag类测试
前言 以下内容是本周Bag代码的课后作业,要求是完成伪代码.产品代码和测试代码,为了书写方便我将伪代码以注释的形式写在了产品代码的后面 测试步骤 1.首先对Bag类引用BagInterface的代码进 ...
- bug终结者 团队作业第八周
bug终结者 团队作业第八周 本次任务 素材提供及编辑:20162328 蔡文琛 博客修改完善:20162322 朱娅霖 "bug终结者" 宏伟蓝图 UML 手绘底稿 用例图 选项 ...
- Week1绪论--抽象数据类型
一.作业题目 1.构造有理数T,元素e1,e2分别被赋以分子.分母值 2.销毁有理数T 3.用e(引用类型参数)返回有理数T的分子或分母,当入参i为1时返回分子, i为2是返回分母. 4.将有理数T的 ...
- Spring-Data-JPA整合MySQL和配置
一.简介 (1).MySQL是一个关系型数据库系统,是如今互联网公司最常用的数据库和最广泛的数据库.为服务端数据库,能承受高并发的访问量. (2).Spring-Data-Jpa是在JPA规范下提供的 ...
- nyoj 黑色帽子
黑色帽子 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 最近发现了一个搞笑的游戏,不过目前还没玩过.一个舞会上,每个人头上都戴着一顶帽子,帽子只有黑 ...
- python之路--day8---day9--两日内容
一.不使用函数的问题 1,代码的组织结构不清晰,可读性差 2,遇到重复的功能只能重复编写实现代码,代码冗余 3,功能需要扩展时,需要找出所有实现该功能的地方修改,无法统一管理且维护难度极大 二.函数是 ...
- 前端双引号单引号,正则反向引用,js比较jq
1.js,jq,css,html属性必须双,如果同时出现需要嵌套使用,属性的规范是双但是也可以用单测试有效 单引号现象举例:jq中获取元素标签是单引号:$('input').click:弹出也是单引号 ...