Skip to content

feat: add solutions to lc problem: No.2080 #4074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 108 additions & 15 deletions solution/2000-2099/2080.Range Frequency Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ rangeFreqQuery.query(0, 11, 33); // 返回 2 。33 在整个子数组中出现 2

<!-- solution:start -->

### 方法一:哈希表
### 方法一:哈希表 + 二分查找

我们用一个哈希表 $g$ 来存储每个值对应的下标数组。在构造函数中,我们遍历数组 $\textit{arr}$,将每个值对应的下标加入到哈希表中。

Expand Down Expand Up @@ -215,20 +215,8 @@ class RangeFreqQuery {
if (!idx) {
return 0;
}
const search = (x: number): number => {
let [l, r] = [0, idx.length];
while (l < r) {
const mid = (l + r) >> 1;
if (idx[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const l = search(left);
const r = search(right + 1);
const l = _.sortedIndex(idx, left);
const r = _.sortedIndex(idx, right + 1);
return r - l;
}
}
Expand All @@ -240,6 +228,111 @@ class RangeFreqQuery {
*/
```

#### Rust

```rust
use std::collections::HashMap;

struct RangeFreqQuery {
g: HashMap<i32, Vec<usize>>,
}

impl RangeFreqQuery {
fn new(arr: Vec<i32>) -> Self {
let mut g = HashMap::new();
for (i, &value) in arr.iter().enumerate() {
g.entry(value).or_insert_with(Vec::new).push(i);
}
RangeFreqQuery { g }
}

fn query(&self, left: i32, right: i32, value: i32) -> i32 {
if let Some(idx) = self.g.get(&value) {
let l = idx.partition_point(|&x| x < left as usize);
let r = idx.partition_point(|&x| x <= right as usize);
return (r - l) as i32;
}
0
}
}
```

#### JavaScript

```js
/**
* @param {number[]} arr
*/
var RangeFreqQuery = function (arr) {
this.g = new Map();

for (let i = 0; i < arr.length; ++i) {
if (!this.g.has(arr[i])) {
this.g.set(arr[i], []);
}
this.g.get(arr[i]).push(i);
}
};

/**
* @param {number} left
* @param {number} right
* @param {number} value
* @return {number}
*/
RangeFreqQuery.prototype.query = function (left, right, value) {
const idx = this.g.get(value);
if (!idx) {
return 0;
}
const l = _.sortedIndex(idx, left);
const r = _.sortedIndex(idx, right + 1);
return r - l;
};

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

#### C#

```cs
public class RangeFreqQuery {
private Dictionary<int, List<int>> g;

public RangeFreqQuery(int[] arr) {
g = new Dictionary<int, List<int>>();
for (int i = 0; i < arr.Length; ++i) {
if (!g.ContainsKey(arr[i])) {
g[arr[i]] = new List<int>();
}
g[arr[i]].Add(i);
}
}

public int Query(int left, int right, int value) {
if (g.ContainsKey(value)) {
var idx = g[value];
int l = idx.BinarySearch(left);
int r = idx.BinarySearch(right + 1);
l = l < 0 ? -l - 1 : l;
r = r < 0 ? -r - 1 : r;
return r - l;
}
return 0;
}
}

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

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
123 changes: 108 additions & 15 deletions solution/2000-2099/2080.Range Frequency Queries/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the

<!-- solution:start -->

### Solution 1: Hash Table
### Solution 1: Hash Table + Binary Search

We use a hash table $g$ to store the array of indices corresponding to each value. In the constructor, we traverse the array $\textit{arr}$, adding the index corresponding to each value to the hash table.

Expand Down Expand Up @@ -214,20 +214,8 @@ class RangeFreqQuery {
if (!idx) {
return 0;
}
const search = (x: number): number => {
let [l, r] = [0, idx.length];
while (l < r) {
const mid = (l + r) >> 1;
if (idx[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
const l = search(left);
const r = search(right + 1);
const l = _.sortedIndex(idx, left);
const r = _.sortedIndex(idx, right + 1);
return r - l;
}
}
Expand All @@ -239,6 +227,111 @@ class RangeFreqQuery {
*/
```

#### Rust

```rust
use std::collections::HashMap;

struct RangeFreqQuery {
g: HashMap<i32, Vec<usize>>,
}

impl RangeFreqQuery {
fn new(arr: Vec<i32>) -> Self {
let mut g = HashMap::new();
for (i, &value) in arr.iter().enumerate() {
g.entry(value).or_insert_with(Vec::new).push(i);
}
RangeFreqQuery { g }
}

fn query(&self, left: i32, right: i32, value: i32) -> i32 {
if let Some(idx) = self.g.get(&value) {
let l = idx.partition_point(|&x| x < left as usize);
let r = idx.partition_point(|&x| x <= right as usize);
return (r - l) as i32;
}
0
}
}
```

#### JavaScript

```js
/**
* @param {number[]} arr
*/
var RangeFreqQuery = function (arr) {
this.g = new Map();

for (let i = 0; i < arr.length; ++i) {
if (!this.g.has(arr[i])) {
this.g.set(arr[i], []);
}
this.g.get(arr[i]).push(i);
}
};

/**
* @param {number} left
* @param {number} right
* @param {number} value
* @return {number}
*/
RangeFreqQuery.prototype.query = function (left, right, value) {
const idx = this.g.get(value);
if (!idx) {
return 0;
}
const l = _.sortedIndex(idx, left);
const r = _.sortedIndex(idx, right + 1);
return r - l;
};

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

#### C#

```cs
public class RangeFreqQuery {
private Dictionary<int, List<int>> g;

public RangeFreqQuery(int[] arr) {
g = new Dictionary<int, List<int>>();
for (int i = 0; i < arr.Length; ++i) {
if (!g.ContainsKey(arr[i])) {
g[arr[i]] = new List<int>();
}
g[arr[i]].Add(i);
}
}

public int Query(int left, int right, int value) {
if (g.ContainsKey(value)) {
var idx = g[value];
int l = idx.BinarySearch(left);
int r = idx.BinarySearch(right + 1);
l = l < 0 ? -l - 1 : l;
r = r < 0 ? -r - 1 : r;
return r - l;
}
return 0;
}
}

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

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
31 changes: 31 additions & 0 deletions solution/2000-2099/2080.Range Frequency Queries/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class RangeFreqQuery {
private Dictionary<int, List<int>> g;

public RangeFreqQuery(int[] arr) {
g = new Dictionary<int, List<int>>();
for (int i = 0; i < arr.Length; ++i) {
if (!g.ContainsKey(arr[i])) {
g[arr[i]] = new List<int>();
}
g[arr[i]].Add(i);
}
}

public int Query(int left, int right, int value) {
if (g.ContainsKey(value)) {
var idx = g[value];
int l = idx.BinarySearch(left);
int r = idx.BinarySearch(right + 1);
l = l < 0 ? -l - 1 : l;
r = r < 0 ? -r - 1 : r;
return r - l;
}
return 0;
}
}

/**
* Your RangeFreqQuery object will be instantiated and called as such:
* RangeFreqQuery obj = new RangeFreqQuery(arr);
* int param_1 = obj.Query(left, right, value);
*/
35 changes: 35 additions & 0 deletions solution/2000-2099/2080.Range Frequency Queries/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @param {number[]} arr
*/
var RangeFreqQuery = function (arr) {
this.g = new Map();

for (let i = 0; i < arr.length; ++i) {
if (!this.g.has(arr[i])) {
this.g.set(arr[i], []);
}
this.g.get(arr[i]).push(i);
}
};

/**
* @param {number} left
* @param {number} right
* @param {number} value
* @return {number}
*/
RangeFreqQuery.prototype.query = function (left, right, value) {
const idx = this.g.get(value);
if (!idx) {
return 0;
}
const l = _.sortedIndex(idx, left);
const r = _.sortedIndex(idx, right + 1);
return r - l;
};

/**
* Your RangeFreqQuery object will be instantiated and called as such:
* var obj = new RangeFreqQuery(arr)
* var param_1 = obj.query(left,right,value)
*/
24 changes: 24 additions & 0 deletions solution/2000-2099/2080.Range Frequency Queries/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::collections::HashMap;

struct RangeFreqQuery {
g: HashMap<i32, Vec<usize>>,
}

impl RangeFreqQuery {
fn new(arr: Vec<i32>) -> Self {
let mut g = HashMap::new();
for (i, &value) in arr.iter().enumerate() {
g.entry(value).or_insert_with(Vec::new).push(i);
}
RangeFreqQuery { g }
}

fn query(&self, left: i32, right: i32, value: i32) -> i32 {
if let Some(idx) = self.g.get(&value) {
let l = idx.partition_point(|&x| x < left as usize);
let r = idx.partition_point(|&x| x <= right as usize);
return (r - l) as i32;
}
0
}
}
Loading