624.Maximum Distance in Arrays --找数组中的最大差值
来源:互联网 发布:数据分析就业 编辑:程序博客网 时间:2024/06/18 08:29
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: 4Explanation: 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.
这道题是找二维数组中的最大差值,其中所选取的整数a和b要来自不同的子数组,并且子数组中的整数是按升序排好的,所以每个子数组的第一个元素是最小的,最后一个元素是最大的,只需要比较每个子数组中的第一个即可找到最小值,比较每个子数组中的第二个即可找到最大值。但是因为整个二维数组的最大值和最小值可能来自相同的子数组,所以用一个变量res来控制,起初res赋值为0,因为差值的绝对值最小为0,然后求当前子数组中的最大值和之前所有子数组的最小值的差的绝对值,并用res记录,再求当前子数组中的最小值和之前所有子数组中的最值的差的绝对值,用res记录。这样保证了既取得最大值,并且所用的整数来自不同的子数组。代码如下:
int res = 0;int min = arrays[0][0];int max = arrays[0][arrays[0].length-1];for(int i=1;i<arrays.length;i++){res = Math.max(res, Math.abs(arrays[i][arrays[i].length-1]-min));res = Math.max(res, Math.abs(max-arrays[i][0]));min = Math.min(min,arrays[i][0]);max = Math.max(max, arrays[i][arrays[i].length-1]);}return res;
阅读全文
1 0
- 624.Maximum Distance in Arrays --找数组中的最大差值
- 624. Maximum Distance in Arrays 数组最大距离
- LeetCode-624:Maximum Distance in Arrays (多数组找元素最大距离)
- leetcode-624. Maximum Distance in Arrays
- [leetcode]: 624. Maximum Distance in Arrays
- [LeetCode]624. Maximum Distance in Arrays
- [LeetCode]624. Maximum Distance in Arrays
- [LeetCode] 624.Maximum Distance in Arrays
- leetcode624: Maximum Distance in Arrays
- leetcode624. Maximum Distance in Arrays
- Maximum Distance in Arrays (第十七周 数组)
- 数组中找最大差值
- Python 刷题日记:LeetCode 624. Maximum Distance in Arrays
- 数组中的最大差值
- Leetcode 624 Maximum Distance in Arrays
- 566. Reshape the Matrix/624. Maximum Distance in Arrays/594. Longest Harmonious Subsequence
- BAT面试题 - 找一个无序实数数组中的最大差值
- 在一个数组中找差值最大数对,输出最大差值
- Storm与Spark:谁才是我们的实时处理利器
- java正则获取html的图片连接
- C++中的static关键字
- html头部底部如何加载
- POJ 2790 Consecutive ones 笔记
- 624.Maximum Distance in Arrays --找数组中的最大差值
- Android事件分发机制
- Java动态加载配置文件
- 指针与引用
- verilog入门知识【2】
- 三种Android图片压缩方法 压缩到指定大小
- Python日期和时间
- C#获取进程的主窗口句柄
- Spring Data Jpa ---- repository