1294. Range Frequency Queries
My accepted Python solution to LeetCode problem 1294, Range Frequency Queries, running in 177ms.
- Difficulty: Medium
- Python
- Runtime 177ms
- Memory 56.7MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 177ms, memory 56.7MB, accepted 2025-12-29.
class RangeFreqQuery:
def __init__(self, arr: List[int]):
from collections import defaultdict
import bisect
self.indices = defaultdict(list)
for i, val in enumerate(arr):
self.indices[val].append(i)
def query(self, left: int, right: int, value: int) -> int:
import bisect
if value not in self.indices:
return 0
idx_list = self.indices[value]
# Find count of indices in range [left, right]
l = bisect.bisect_left(idx_list, left)
r = bisect.bisect_right(idx_list, right)
return r - l
# Your RangeFreqQuery object will be instantiated and called as such:
# obj = RangeFreqQuery(arr)
# param_1 = obj.query(left,right,value)