![JAR search and dependency download from the Maven repository](/logo.png)
g2001_2100.s2080_range_frequency_queries.RangeFreqQuery.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2001_2100.s2080_range_frequency_queries
// #Medium #Array #Hash_Table #Binary_Search #Design #Segment_Tree
// #2023_06_27_Time_1102_ms_(100.00%)_Space_165.6_MB_(100.00%)
import java.util.Collections
class RangeFreqQuery(arr: IntArray) {
private val map: MutableMap>
init {
map = HashMap()
for (i in arr.indices) {
if (!map.containsKey(arr[i])) {
map[arr[i]] = ArrayList()
}
map[arr[i]]!!.add(i)
}
}
fun query(left: Int, right: Int, value: Int): Int {
if (!map.containsKey(value)) {
return 0
}
val list: List = map[value]!!
var s = Collections.binarySearch(list, left)
var e = Collections.binarySearch(list, right)
if (s < 0) {
s = (s + 1) * -1
}
if (e < 0) {
e = (e + 2) * -1
}
return e - s + 1
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy