【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 ...
随机推荐
- snpEff注释结果各区域统计之和大于变异总数?
目录 问题一:各区域注释之和大于变异总数? 问题二:注释Region出现Gene和transcript等区域? 问题一:各区域注释之和大于变异总数? snpEff的结果很简单,但常常遇到如下问题. 我 ...
- springcloud报Load balancer does not have available server for client: PROVIDER-SERVER
1.后台报错截图 这个的意思就是:负载均衡服务器中没有这个我自定义的PROVIDER-SERVER.开始我以为是Ribbon的原因,所以去折腾了一下,但是:最后不断往前推到之后发现本质是:在注册中心E ...
- 巩固java第五天
巩固内容: HTML 实例解析 <p> 元素: <p>这是第一个段落.</p> 这个 <p> 元素定义了 HTML 文档中的一个段落. 这个元素拥有一个 ...
- 23. 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
原文:https://blog.csdn.net/u011596455/article/details/60322568 版权声明:本文为博主原创文章,转载请附上博文链接! 在Ubuntu中,有时候运 ...
- Maven打包及场景
场景一 对当前项目打包并指定主类. <build> <plugins> <plugin> <artifactId>maven-compiler-plug ...
- NERD_commenter快捷键
快捷键有点多,记不过来,做个备份 1. \cc 注释当前行和选中行 2. \cn 没有发现和\cc有区别 3. \c<空格> 如果被选区域有部分被注释,则对被选区域执行取消注释操作,其它情 ...
- 如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach
如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach 2016年01月13日 16:04:20 炸鸡叔 阅读数:7277 转发自:http://baike.ba ...
- 【JAVA】【基础知识】Java程序执行过程
1. Java程序制作过程 使用文本编辑器进行编辑 2. 编译源文件,生成class文件(字节码文件) javac源文件路径. 3.运行程序class文件.
- 【CentOS】检查系统是否安装OpenSSH
CentOS7 远程联机 哔哩哔哩 萌狼蓝天 博客:https://mllt.cc 微信公众号:萌狼蓝天 检查与安装配置OpenSSH [CentOS7]检查系统是否安装OpenSSH yum -q ...
- centos源码安装ruby
目录 一.简介 二.程序部署 一.简介 Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言.rvm是ruby的管理器,可以切换ruby版本,下载ruby. 二.程序部署 1.下载ruby w ...