![JAR search and dependency download from the Maven repository](/logo.png)
g2701_2800.s2768_number_of_black_blocks.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 g2701_2800.s2768_number_of_black_blocks
// #Medium #Array #Hash_Table #Enumeration #2023_08_11_Time_719_ms_(100.00%)_Space_55.3_MB_(100.00%)
class Solution {
fun countBlackBlocks(m: Int, n: Int, coordinates: Array): LongArray {
val ans = LongArray(5)
val count: MutableMap = HashMap()
for (coordinate in coordinates) {
val x = coordinate[0]
val y = coordinate[1]
for (i in x until x + 2) {
for (j in y until y + 2) {
if (i - 1 >= 0 && i < m && j - 1 >= 0 && j < n) {
count.merge(
i * n + j, 1
) { a: Int?, b: Int? -> Integer.sum(a!!, b!!) }
}
}
}
}
for (freq in count.values) {
++ans[freq]
}
ans[0] = (m - 1L) * (n - 1) - ans.sum()
return ans
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy