[LeetCode]Maximum Distance in Arrays

栏目: 编程工具 · 发布时间: 6年前

内容简介:[LeetCode]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 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:

  1. Each given array will have at least 1 number. There will be at least two non-empty arrays.
  2. The total number of the integers in all the m arrays will be in the range of [2, 10000].
  3. The integers in the m arrays will be in the range of [-10000, 10000].

题目大意:

给定一组数组arrays,各数组递增有序,求不同数组之间最小值、最大值之间差值绝对值的最大值。

解题思路:

TreeMap(红黑树) 时间复杂度O(n * log(n)),n为arrays的长度

构造红黑树maxMap, minMap分别存储arrays各数组的最大值和最小值。

遍历arrays,记当前数组为array,其最小值min = array[0], 最大值max = array[array.length - 1]

  分别将max和min从maxMap,minMap中移除
  
  利用maxMap.lastKey() - min,max - minMap.firstKey()更新答案
  
  然后将max和min添加回maxMap与minMap

Java代码:

public class Solution {
    public int maxDistance(int[][] arrays) {
        if (arrays.length <= 1) return 0;
        TreeMap<Integer, Integer> maxMap = new TreeMap<>();
        TreeMap<Integer, Integer> minMap = new TreeMap<>();
        for (int array[] : arrays) {
            int min = array[0], max = array[array.length - 1];
            maxMap.put(max, maxMap.getOrDefault(max, 0) + 1);
            minMap.put(min, minMap.getOrDefault(min, 0) + 1);
        }
        int ans = 0;
        for (int array[] : arrays) {
            int min = array[0], max = array[array.length - 1];
            if (maxMap.put(max, maxMap.get(max) - 1) == 1) {
                maxMap.remove(max);
            }
            if (minMap.put(min, minMap.get(min) - 1) == 1) {
                minMap.remove(min);
            }
            ans = Math.max(ans, maxMap.lastKey() - min);
            ans = Math.max(ans, max - minMap.firstKey());
            maxMap.put(max, maxMap.getOrDefault(max, 0) + 1);
            minMap.put(min, minMap.getOrDefault(min, 0) + 1);
        }
        return ans;
    }
    
}

以上所述就是小编给大家介绍的《[LeetCode]Maximum Distance in Arrays》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Processing编程学习指南(原书第2版)

Processing编程学习指南(原书第2版)

[美]丹尼尔希夫曼(Daniel Shiffman) / 李存 / 机械工业出版社 / 2017-3-1 / 99.00元

在视觉化界面中学习电脑编程的基本原理! 本书介绍了编程的基本原理,涵盖了创建最前沿的图形应用程序(例如互动艺术、实时视频处理和数据可视化)所需要的基础知识。作为一本实验风格的手册,本书精心挑选了部分高级技术进行详尽解释,可以让图形和网页设计师、艺术家及平面设计师快速熟悉Processing编程环境。 从算法设计到数据可视化,从计算机视觉到3D图形,在有趣的互动视觉媒体和创意编程的背景之......一起来看看 《Processing编程学习指南(原书第2版)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具