ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Range Frequency Queries

2021-11-22 13:32:23  阅读:177  来源: 互联网

标签:RangeFreqQuery arr right int value Range Frequency Queries left


Design a data structure to find the frequency of a given value in a given subarray.

The frequency of a value in a subarray is the number of occurrences of that value in the subarray.

Implement the RangeFreqQuery class:

  • RangeFreqQuery(int[] arr) Constructs an instance of the class with the given 0-indexed integer array arr.
  • int query(int left, int right, int value) Returns the frequency of value in the subarray arr[left...right].

subarray is a contiguous sequence of elements within an array. arr[left...right] denotes the subarray that contains the elements of nums between indices left and right (inclusive).

Example 1:

Input
["RangeFreqQuery", "query", "query"]
[[[12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]], [1, 2, 4], [0, 11, 33]]
Output
[null, 1, 2]

Explanation
RangeFreqQuery rangeFreqQuery = new RangeFreqQuery([12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]);
rangeFreqQuery.query(1, 2, 4); // return 1. The value 4 occurs 1 time in the subarray [33, 4]
rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the whole array.

Constraints:

  • 1 <= arr.length <= 105
  • 1 <= arr[i], value <= 104
  • 0 <= left <= right < arr.length
  • At most 105 calls will be made to query

思路:reverted index,先收集每个元素出现的index,每个元素肯定是个递增的序列,那么可以用binarysearch或者用treemap,logn去搜,[1,4,7,9,10,24] 里面全部是index,然后用left去搜第一个大于left的index,在新array里面占第i,用right去搜最后一个小于right的index,在新array里面占第j,然后个数就是 j - i + 1; 这题要明确,treemap里面存的是什么;

class RangeFreqQuery {
                                //原来index, 在新list里面占的位置 index
    private HashMap<Integer, TreeMap<Integer, Integer>> hashmap;
    public RangeFreqQuery(int[] arr) {
        this.hashmap = new HashMap<>();
        for(int i = 0; i < arr.length; i++) {
            if(!hashmap.containsKey(arr[i])) {
                hashmap.put(arr[i], new TreeMap<Integer, Integer>());
            }
            hashmap.get(arr[i]).put(i, hashmap.get(arr[i]).size());
        }
    }
    
    public int query(int left, int right, int value) {
        if(!hashmap.containsKey(value)) {
            return 0;
        }
        TreeMap<Integer, Integer> nums = hashmap.get(value);
        Integer a = nums.ceilingKey(left);
        Integer b = nums.floorKey(right);
        if(a == null || b == null) {
            return 0;
        }
        return nums.get(b) - nums.get(a) + 1;
    }
}

/**
 * Your RangeFreqQuery object will be instantiated and called as such:
 * RangeFreqQuery obj = new RangeFreqQuery(arr);
 * int param_1 = obj.query(left,right,value);
 */

标签:RangeFreqQuery,arr,right,int,value,Range,Frequency,Queries,left
来源: https://blog.csdn.net/u013325815/article/details/121469611

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有