![JAR search and dependency download from the Maven repository](/logo.png)
g2301_2400.s2397_maximum_rows_covered_by_columns.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 g2301_2400.s2397_maximum_rows_covered_by_columns
// #Medium #Array #Matrix #Bit_Manipulation #Backtracking #Enumeration
// #2023_07_02_Time_154_ms_(100.00%)_Space_32.3_MB_(100.00%)
class Solution {
private var ans = 0
fun maximumRows(matrix: Array, numSelect: Int): Int {
dfs(matrix, /*colIndex=*/0, numSelect, /*mask=*/0)
return ans
}
private fun dfs(matrix: Array, colIndex: Int, leftColsCount: Int, mask: Int) {
if (leftColsCount == 0) {
ans = Math.max(ans, getAllZerosRowCount(matrix, mask))
return
}
if (colIndex == matrix[0].size) {
return
}
// choose this column
dfs(matrix, colIndex + 1, leftColsCount - 1, mask or (1 shl colIndex))
// not choose this column
dfs(matrix, colIndex + 1, leftColsCount, mask)
}
private fun getAllZerosRowCount(matrix: Array, mask: Int): Int {
var count = 0
for (row in matrix) {
var isAllZeros = true
for (i in row.indices) {
if (row[i] == 1 && mask shr i and 1 == 0) {
isAllZeros = false
break
}
}
if (isAllZeros) {
++count
}
}
return count
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy