Leetcode题目之求解数组之间的最大距离
来源:互联网 发布:可以看央视的网络电视 编辑:程序博客网 时间:2024/06/05 04:39
打算从今天开始开辟一个LeetCode专栏,收集并讲解LeetCode网站的一些题目。
今天的题目是LeetCode第37期的第一个题目,比较简单。
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.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个数组,这些数组已经按照升序排列,求出数组之间的最大距离。
public class Solution { public int maxDistance(List<List<Integer>> arrays) { int result = Integer.MIN_VALUE; int max = arrays.get(0).get(arrays.get(0).size()-1); int min = arrays.get(0).get(0); for(int i=1;i<arrays.size();i++) { result = Math.max(result,Math.abs(arrays.get(i).get(0)-max)); result = Math.max(result,Math.abs(arrays.get(i).get(arrays.get(i).size()-1)-min)); max = Math.max(max,arrays.get(i).get(arrays.get(i).size()-1)); min = Math.min(min,arrays.get(i).get(0)); } return result; }}
采用List来存储一些List,遍历之即可
阅读全文
1 0
- Leetcode题目之求解数组之间的最大距离
- 数组之间的距离
- java求解LeetCode题目,实现求解数组中的majority element
- 求解二叉树中节点的最大距离
- python求解LeetCode题目,找出数组中的Majority element元素
- 求解二维数组的最大/最小元素
- 求解最大连续子数组的算法
- 数组元素限定条件下的最大距离
- 编程之美 求二叉树中节点之间最大的距离
- 求解最大子数组
- 求解最大子数组
- Java求解数组中的两个元素的最小距离
- 求最大子数组的和,算法导论之分治递归求解,暴力求解,记忆扫描方法。
- 算法之 数组的“距离”
- C语言学习之求解二维数组的最大/最小元素
- 二维矩阵求解最大移动距离
- 旋转卡壳--凸包之间的最大距离
- hdu3534 树形dp(求树中两点之间的最大距离)
- java多线程之java操作进程
- 各种AJAX方法的使用比较
- 最常用的 Eclipse 快捷键总结
- 每次登录都要执行source .basharc,否则ll等命令失效
- 谈谈Mysql之事务
- Leetcode题目之求解数组之间的最大距离
- Maven仓库的简介,学习
- GCD相关知识点
- Kotlin简单实用方法既使用Kotlin优雅的开发Android应用
- 单向RNN和双向RNN在mnist数据集上的分类实验
- 最全最好用的Android Studio插件整理
- JavaScript -- 几道面试题
- STM32空闲中断+DMA解决接收不定长数据问题
- 命运