![JAR search and dependency download from the Maven repository](/logo.png)
g1101_1200.s1124_longest_well_performing_interval.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 g1101_1200.s1124_longest_well_performing_interval
// #Medium #Array #Hash_Table #Stack #Prefix_Sum #Monotonic_Stack
// #2023_05_31_Time_313_ms_(100.00%)_Space_57.1_MB_(100.00%)
class Solution {
fun longestWPI(hours: IntArray): Int {
var i = 0
val map = HashMap()
var sum = 0
map[sum] = -1
var max = Int.MIN_VALUE
for (`val` in hours) {
sum += if (`val` > 8) 1 else -1
if (!map.containsKey(sum)) {
map[sum] = i
}
if (sum > 0) {
max = i + 1
} else if (map.containsKey(sum - 1)) {
max = Math.max(i - map[sum - 1]!!, max)
}
i++
}
if (max == Int.MIN_VALUE) {
max = 0
}
return max
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy