![JAR search and dependency download from the Maven repository](/logo.png)
g1201_1300.s1248_count_number_of_nice_subarrays.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 g1201_1300.s1248_count_number_of_nice_subarrays
// #Medium #Array #Hash_Table #Math #Sliding_Window
// #2023_06_09_Time_431_ms_(93.33%)_Space_48.7_MB_(80.00%)
class Solution {
fun numberOfSubarrays(nums: IntArray, k: Int): Int {
var oddLen = 0
var startIndex = 0
var num = 0
var endIndex: Int
var res = 0
var hasK: Boolean
for (i in nums.indices) {
hasK = false
endIndex = i
if (nums[i] % 2 == 1) {
oddLen++
}
while (oddLen >= k) {
hasK = true
if (nums[startIndex++] % 2 == 1) {
oddLen--
}
num++
}
res += num
while (hasK && ++endIndex < nums.size && nums[endIndex] % 2 == 0) {
res += num
}
num = 0
}
return res
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy