g0201_0300.s0274_h_index.Solution.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 g0201_0300.s0274_h_index
// #Medium #Array #Sorting #Counting_Sort #2022_11_04_Time_291_ms_(45.45%)_Space_34.7_MB_(100.00%)
class Solution {
fun hIndex(citations: IntArray): Int {
val counts = IntArray(citations.size + 1)
for (citation in citations) {
val idx = (citations.size).coerceAtMost(citation)
counts[idx] += 1
}
var total = 0
for (i in counts.indices) {
val idx = citations.size - i
total += counts[idx]
if (total >= idx) {
return idx
}
}
return 0
}
}