2080. Range Frequency Queries Medium

@problem@discussion
#Array#Hash Table#Binary Search#Design#Segment Tree



1/**
2 * [2080] Range Frequency Queries
3 *
4 * Design a data structure to find the frequency of a given value in a given subarray.
5 * The frequency of a value in a subarray is the number of occurrences of that value in the subarray.
6 * Implement the RangeFreqQuery class:
7 * 
8 * 	RangeFreqQuery(int[] arr) Constructs an instance of the class with the given 0-indexed integer array arr.
9 * 	int query(int left, int right, int value) Returns the frequency of value in the subarray arr[left...right].
10 * 
11 * A 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).
12 *  
13 * Example 1:
14 * 
15 * Input
16 * ["RangeFreqQuery", "query", "query"]
17 * [[[12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]], [1, 2, 4], [0, 11, 33]]
18 * Output
19 * [null, 1, 2]
20 * Explanation
21 * RangeFreqQuery rangeFreqQuery = new RangeFreqQuery([12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]);
22 * rangeFreqQuery.query(1, 2, 4); // return 1. The value 4 occurs 1 time in the subarray [33, 4]
23 * rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the whole array.
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	1 <= arr.length <= 10^5
29 * 	1 <= arr[i], value <= 10^4
30 * 	0 <= left <= right < arr.length
31 * 	At most 10^5 calls will be made to query
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/range-frequency-queries/
37// discuss: https://leetcode.com/problems/range-frequency-queries/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41struct RangeFreqQuery {
42        false
43    }
44
45
46/** 
47 * `&self` means the method takes an immutable reference.
48 * If you need a mutable reference, change it to `&mut self` instead.
49 */
50impl RangeFreqQuery {
51
52    fn new(arr: Vec<i32>) -> Self {
53        
54    }
55    
56    fn query(&self, left: i32, right: i32, value: i32) -> i32 {
57        
58    }
59}
60
61/**
62 * Your RangeFreqQuery object will be instantiated and called as such:
63 * let obj = RangeFreqQuery::new(arr);
64 * let ret_1: i32 = obj.query(left, right, value);
65 */
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_2080() {
75    }
76}
77


Back
© 2025 bowen.ge All Rights Reserved.