【LeetCode】624. Maximum Distance in Arrays 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址: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:
- 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].
题目大意
给定 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++)的更多相关文章
- 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] 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
		Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up 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 349 Intersection of Two Arrays 解题报告
		题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ... 
- 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 ... 
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
		[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ... 
- 【LeetCode】732. My Calendar III解题报告
		[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ... 
- 【LeetCode】486. Predict the Winner 解题报告(Python)
		[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ... 
随机推荐
- 用 AppImage文件创建快捷图标和软连接
			背景 AppImage是一种在Linux系统中用于分发便携式软件而不需要超级用户权限来安装它们的格式.[1] 它还试图允许Linux的上游开发者来分发他们的程序而不用考虑不同Linux发行版间的区别. ... 
- Xwiki——实现
			基于CentOS6.9 yum install java wget http://download.forge.ow2.org/xwiki/xwiki-enterprise-installer-gen ... 
- 内网穿透—使用 frp 实现内外网互通
			前言 什么是内网穿透? 内网穿透,又叫 NET 穿透,是计算机用语.用通俗的说法就是你家里的个人电脑,可以直接被外网的人访问.例如你在公司,不通过远程工具,直接也可以访问到家里的电脑(本文章特指 we ... 
- 备忘录:关于.net程序连接Oracle数据库
			目录 关于使用MSSM访问Oracle数据库 关于. net 程序中连接Oracle数据库 志铭-2021年12月7日 21:22:15 关于使用MSSM访问Oracle数据库 安装访问接口组件:Or ... 
- 大数据学习----day27----hive02------1.  分桶表以及分桶抽样查询 2. 导出数据 3.Hive数据类型  4 逐行运算查询基本语法(group by用法,原理补充) 5.case when(练习题,多表关联)6 排序
			1. 分桶表以及分桶抽样查询 1.1 分桶表 对Hive(Inceptor)表分桶可以将表中记录按分桶键(某个字段对应的的值)的哈希值分散进多个文件中,这些小文件称为桶. 如要按照name属性分为3个 ... 
- java.sql.SQLException: Cannot create com._51doit.pojo.User: com._51doit.pojo.User Query: select * from user where username = ? and password = ? Parameters: [AA, 123]
			在从数据库中查询数据,并存入javabean中去时,报这个错误 原因:在建立User类去存储信息时没有创建无参构造方法,创建一个无参构造方法即可 
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
			[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ... 
- linux重启后JDk环境变量配置失效最终解决方案
			最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端 ... 
- 为什么Redis集群有16384个槽
			一.前言 我在<那些年用过的Redis集群架构(含面试解析)>一文里提到过,现在redis集群架构,redis cluster用的会比较多. 如下图所示 对于客户端请求的key,根据公式H ... 
- RDS备份到OSS增量+全量
			一.前言 阿里云的RDS备份是占用使用量的,你购买200G那备份使用量是100G左右,导致备份一般也就存半个月,2个全备份. 那半个月后之前的也就删除了,如果要持续保留更久将花费不少的金钱.所以这里用 ... 
