{"id":8753,"date":"2021-11-21T00:18:34","date_gmt":"2021-11-21T08:18:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8753"},"modified":"2021-11-21T00:19:05","modified_gmt":"2021-11-21T08:19:05","slug":"leetcode-2080-range-frequency-queries","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-2080-range-frequency-queries\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2080. Range Frequency Queries"},"content":{"rendered":"\n<p>Design a data structure to find the&nbsp;<strong>frequency<\/strong>&nbsp;of a given value in a given subarray.<\/p>\n\n\n\n<p>The&nbsp;<strong>frequency<\/strong>&nbsp;of a value in a subarray is the number of occurrences of that value in the subarray.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>RangeFreqQuery<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>RangeFreqQuery(int[] arr)<\/code>&nbsp;Constructs an instance of the class with the given&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>arr<\/code>.<\/li><li><code>int query(int left, int right, int value)<\/code>&nbsp;Returns the&nbsp;<strong>frequency<\/strong>&nbsp;of&nbsp;<code>value<\/code>&nbsp;in the subarray&nbsp;<code>arr[left...right]<\/code>.<\/li><\/ul>\n\n\n\n<p>A&nbsp;<strong>subarray<\/strong>&nbsp;is a contiguous sequence of elements within an array.&nbsp;<code>arr[left...right]<\/code>&nbsp;denotes the subarray that contains the elements of&nbsp;<code>nums<\/code>&nbsp;between indices&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>right<\/code>&nbsp;(<strong>inclusive<\/strong>).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input<\/strong>\n[\"RangeFreqQuery\", \"query\", \"query\"]\n[[[12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]], [1, 2, 4], [0, 11, 33]]\n<strong>Output<\/strong>\n<\/pre>\n\n\n<p>[null, 1, 2]<\/p>\n\n\n\n<p><strong>Explanation<\/strong> 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.<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= arr[i], value &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= left &lt;= right &lt; arr.length<\/code><\/li><li>At most&nbsp;<code>10<sup>5<\/sup><\/code>&nbsp;calls will be made to&nbsp;<code>query<\/code><\/li><\/ul>\n\n\n\n<p>Solution: Hashtable + Binary Search<\/p>\n\n\n\n<p>Time complexity: Init: O(max(arr) + n), query: O(logn)<br>Space complexity: O(max(arr) + n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass RangeFreqQuery {\npublic:\n  RangeFreqQuery(vector<int>& arr) : s_(10001) {\n    for (int i = 0; i < arr.size(); ++i)\n      s_[arr[i]].push_back(i);\n  }\n\n  int query(int left, int right, int value) {\n    const auto&#038; m = s_[value];\n    if (m.empty()) return 0;\n    auto r = prev(upper_bound(begin(m), end(m), right));\n    auto l = lower_bound(begin(m), end(m), left);    \n    return r - l + 1;\n  }\nprivate:\n  vector<vector<int>> s_;\n};\n\/**\n * Your RangeFreqQuery object will be instantiated and called as such:\n * RangeFreqQuery* obj = new RangeFreqQuery(arr);\n * int param_1 = obj->query(left,right,value);\n *\/\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design a data structure to find the&nbsp;frequency&nbsp;of a given value in a given subarray. The&nbsp;frequency&nbsp;of a value in a subarray is the number of occurrences&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,82,177],"class_list":["post-8753","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8753"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8753\/revisions"}],"predecessor-version":[{"id":8755,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8753\/revisions\/8755"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}