Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package g2001_2100.s2080_range_frequency_queries;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RangeFreqQuery {

Map<Integer, List<Integer>> map;

public RangeFreqQuery(int[] arr) {
map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (!map.containsKey(arr[i])) {
map.put(arr[i], new ArrayList<>());
}
map.get(arr[i]).add(i);
}
}

public int query(int left, int right, int value) {

if (!map.containsKey(value)) {
return 0;
}
List<Integer> list = map.get(value);
int s = Collections.binarySearch(list, left);
int e = Collections.binarySearch(list, right);

if (s < 0) {
s = (s + 1) * (-1);
}
if (e < 0) {
e = (e + 2) * (-1);
}
return e - s + 1;
}
}

/*
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery obj = new RangeFreqQuery(arr);
* int param_1 = obj.query(left,right,value);
*/
36 changes: 36 additions & 0 deletions src/main/java/g2001_2100/s2080_range_frequency_queries/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
2080\. Range Frequency Queries

Medium

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]`.

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**).

**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:**

* <code>1 <= arr.length <= 10<sup>5</sup></code>
* <code>1 <= arr[i], value <= 10<sup>4</sup></code>
* `0 <= left <= right < arr.length`
* At most <code>10<sup>5</sup></code> calls will be made to `query`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g2001_2100.s2080_range_frequency_queries;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class RangeFreqQueryTest {
@Test
public void rangeFreqQuery() {
RangeFreqQuery rangeFreqQuery =
new RangeFreqQuery(new int[] {12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56});
// return 1. The value 4 occurs 1 time in the subarray [33, 4]
assertThat(rangeFreqQuery.query(1, 2, 4), equalTo(1));
// return 2. The value 33 occurs 2 times in the whole array.
assertThat(rangeFreqQuery.query(0, 11, 33), equalTo(2));
}
}