【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 ...
随机推荐
- Linux之crond任务调度
1. 示意图 2. 基本语法 crontab [选项] # -e : 编辑crontab定时任务 # -l : 查询crontab # -r : 删除当前用户所有的crontab任务 # 例子: # ...
- 一个简单但能考察C语言基础的题目
请看题: #include<stdio.h> int a=1; int main(void) { int a=a; printf("a=d%\n",a); return ...
- 搭建简单的SpringCloud项目一:注册中心和公共层
注:笔者在搭建途中其实遇见不少问题,统一放在后面的文章说明,现在的搭建是测试OK的. GitHub:https://github.com/ownzyuan/test-cloud 后续:搭建简单的Spr ...
- 从面试官的角度,聊聊java面试流程
在这篇回答里,就讲以我常规的面试流程为例,说下java方面大致会问什么问题,以及如何确认候选人达到招聘要求. 先说面试前准备,可能有些面试官是拿到简历直接问,而且是在候选人自我介绍时再草草浏览简历,但 ...
- Angular Service设计理念及使用
官方认为组件不应该直接获取或保存数据, 它们应该聚焦于展示数据,而把数据访问的职责委托给某个服务. 而服务就充当着数据访问,逻辑处理的功能.把组件和服务区分开,以提高模块性和复用性. 1.依赖注入 注 ...
- 设计和实现OLAP解决方案 [转]
第一讲 简介首先,啥叫数据仓库? 数据仓库就是数据的仓库!用外文说叫Data Warehouse,简称DW. 是不是哐当倒下一片啊,要不咱换个专业点的说法? 数据仓库是一个面向主题的.集成的.相对稳定 ...
- 容器之分类与各种测试(四)——map
map和set的区别在于,前者key和value是分开的,前者的key不会重复,value可以重复:后者的key即为value,后者的value不允许重复.还有,map在插入时可以使用 [ ]进行(看 ...
- Javaj基础知识runtime error
遇到的java 运行时错误: NullPointerException空指针 ,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始 ...
- lucene中创建索引库
package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...
- Mysql资料 索引
目录 一.介绍 什么是索引? 为什么要有索引呢? 二.索引的原理 原理 磁盘IO与预读 索引的数据结构 b+树的查找过程 b+树性质 三.索引管理 MySQL的索引分类 各索引应用场景 索引类型 操作 ...