624. Maximum Distance in Arrays
Problem statement
Given
marrays, 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 integersaandbto 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
marrays will be in the range of [2, 10000].- The integers in the
marrays will be in the range of [-10000, 10000].
Solution
This is the first question in leetcode weekly contest 37, the key issue is we need to find the maximum distance among two different arrays.
My solution:
The general idea is as follows:
- Two vectors, store all min and max value with their array index in the format of pair.
- Sort these two vectors.
- Return values: if min and max values come from different arrays, return their difference directly. Otherwise, do more process.
Time complexity is O(nlgn), space complexity is O(n). n is the number of arrays.
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
vector<pair<int, int>> minv;
vector<pair<int, int>> maxv;
// add each value with it`s array index to a vector
for(int i = ; i < arrays.size(); i++){
minv.push_back({arrays[i][], i});
maxv.push_back({arrays[i].back(), i});
}
// sort the array by incrasing order
sort(minv.begin(), minv.end());
sort(maxv.begin(), maxv.end());
// return directly if the min and max come from different arrays
if(minv[].second != maxv.back().second){
return maxv.back().first - minv[].first;
} else {
// otherwise
return maxv.back().first - minv[].first > maxv[maxv.size() - ].first - minv[].first ?
maxv.back().first - minv[].first : maxv[maxv.size() - ].first - minv[].first;
}
}
};
The solution from leetcode
This solution is more intuitive and is the most concise version.
Since the max difference of min and max can not come from same array, we store the min and max value in previous processed arrays and compare with current min and max.
Time complexity is O(n), space complexity is O(1).
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
int minv = arrays[][];
int maxv = arrays[].back();
int max_dis = INT_MIN;
for(int i = ; i < arrays.size(); i++){
max_dis = max(max_dis, max(abs(maxv - arrays[i][]), abs(arrays[i].back() - minv)));
maxv = max(maxv, arrays[i].back());
minv = min(minv, arrays[i][]);
}
return max_dis;
}
};
624. 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 ...
- 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 数组中的最大距离
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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 保存已有的最大最小 日期 题目地址:h ...
- [LeetCode] 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-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 ...
- Avito Cool Challenge 2018:D. Maximum Distance
D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...
随机推荐
- VS配置本地IIS以域名访问
1.IIS下配置自己的网站,添加主机名 2.修改hosts文件(C://Windows/System32/drivers/etc) 3.VS中配置项目Web服务器(选择外部主机)
- AJPFX关于Set接口学习笔记及总结
Set接口中的方法和Collection中方法一致的.Set接口取出方式只有一种,迭代器. |--HashSet:底层数据结构是哈希表,线程是不同步的.无序,高效: HashSet集合保证元素唯一性: ...
- 【学习笔记】深入理解js原型和闭包(0)——目录
文章转载:https://www.cnblogs.com/wangfupeng1988/p/4001284.html 说明: 本篇文章一共16篇章,外加两篇后补的和一篇自己后来添加的学习笔记,一共19 ...
- XML基本概念及增删改查操作
一.概念及特征: 1. XML 指可扩展标记语言(Extensible Markup Language),用户可以自己定义标签.XML 被设计用来传输和存储数据,而 HTML 用于格式化并显示数据,并 ...
- Oracle ORA
ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常. ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 OR ...
- 洛谷 P2604 [ZJOI2010]网络扩容
题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的最小扩容费用. ...
- rmdir
rmdir——删除空目录 remove empty directories 命令所在路径:bin/rmdir 示例: # rmdir /tmp/japan/longze 删除/tmp/japan/目录 ...
- ECharts是我接触过的最优秀的可视化工具,也是进步最快的软件,希望它早日成为世界级的开源项目。
ECharts的广泛网址: http://echarts.baidu.com/doc/example.html 零编程玩转图表: http://tushuo.baidu.com/?qq-pf-to=p ...
- mybatis 存储过程的写法
(注意事项: 在使用游标的时候,不能在游标声明之前,使用crud) 存储过程示例 CREATE DEFINER=`root`@`::` PROCEDURE `earnings_proceduce`() ...
- exit - 使程序正常中止
SYNOPSIS 总览 #include <stdlib.h> void exit(int status); DESCRIPTION 描述 函数 exit() 使得程序正常中止,statu ...