首页 > 其他分享 >treemap/treeset 相关 1438

treemap/treeset 相关 1438

时间:2023-01-27 14:01:25浏览次数:46  
标签:nums int treemap 1438 maximum limit diff treeset absolute

1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Medium

Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.

 Example 1:

Input: nums = [8,2,4,7], limit = 4
Output: 2 
Explanation: All subarrays are: 
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4. 
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4. 
Therefore, the size of the longest subarray is 2.

Example 2:

Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4 
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.

Example 3:

Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3

 Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 0 <= limit <= 109
class Solution {
    public int longestSubarray(int[] nums, int limit) {
        TreeSet<Integer> tree = new TreeSet<Integer>((x,y)->{
            return nums[x]==nums[y] ? x-y : nums[x]-nums[y];
        });
        int left = 0;
        int result = 0;
        for(int i=0; i < nums.length; i++){
            tree.add(i);
            while(nums[tree.last()]-nums[tree.first()]>limit){
                tree.remove(left++);
            }
            result = Math.max(result, i-left+1);
        }
        return result;
    }
}

 

标签:nums,int,treemap,1438,maximum,limit,diff,treeset,absolute
From: https://www.cnblogs.com/cynrjy/p/17068862.html

相关文章

  • JDK 1.8 TreeMap源码分析
    /**   *TreeMap特点:   * 底层:二叉红黑树key输入无序,升序排列,null不可以   * 1.2    */publicclassTreeMap<K,V>   extendsAbstractMap<K......
  • JDK 1.8 TreeSet 源码分析
       /**   *TreeSet的特点:无序 唯一需要比较器自定义<>中的内容需要实现comparable的接口推荐外部实现:多态,自定义多种规则   *底层实现逻辑:二叉红黑......
  • treemap与hashcode
    有个需求 需要将map排序 我就用了treemap 一个map列表  将总计字段放在最后面 其他无所谓 最开始是这样写的Map<String,Object>temp=newTreeMap<>(ne......
  • 总结HashSet和TreeSet的去重
    HashSet的去重添加的对象需要重写hashCode()和equals()方法,其中hashCode()方法,应该是根据自定义类对象的成员属性值计算得来,equals()方法,应该是比较自定义类对象的成员属......
  • java中的TreeSet的add()方法解析
    TreeSet的add()方法解析【添加和去重】1publicclassHomeWork05{2publicstaticvoidmain(String[]args){3//TreeSet最好是同一类型。4......
  • HashMap 和 treemap
    Map接口概述将键映射到值的对象一个映射不能包含重复的键每个键最多只能映射到一个值Map接口和Collection接口的不同Map是双列的,Col......
  • Java:Should I use a `HashSet` or a `TreeSet` for a very large dataset?
    这是StackOverflow上一个有意思的提问,记录一下。原地址在这翻译:对于大型数据集,应该使用”哈希集”还是”树集”?(因为HashTable有着O(1)的查找速度比树结构更有效率,虽然H......
  • TreeMap使用案例-多个映射(中级版)
    细节问题请看简单版publicclassWordMapn{publicstaticMap<String,List<String>>computeAdjacentWords(List<String>theWords){Map<String,L......
  • TreeMap使用案例-多个映射(简单版)
    题目,找出单词表中每个单词只有一个字母不同的所有单词(简单版,花费时间多)importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.Tree......
  • 集合之Map【TreeMap】
    packagecom.Lucky.Map;importjava.util.Comparator;importjava.util.TreeMap;/*TreeMap:底层结构和TreeSet一样是红黑树可以排序/无重......