-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFirstCompletelyPaintedRowOrColumn2661.kt
49 lines (41 loc) · 1.36 KB
/
FirstCompletelyPaintedRowOrColumn2661.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package medium
object FirstCompletelyPaintedRowOrColumn2661 {
fun firstCompleteIndex(arr: IntArray, mat: Array<IntArray>): Int {
val numRows = mat.size
val numCols = mat[0].size
val numToPos = HashMap<Int, Pair<Int, Int>>()
// Populate the numToPos map by iterating over the matrix
for (row in 0 until numRows) {
for (col in 0 until numCols) {
val value = mat[row][col]
numToPos[value] = Pair(row, col)
}
}
arr.forEachIndexed { index, value ->
val pos: Pair<Int, Int> = numToPos.getOrDefault(value, Pair(0,0))
val row = pos.first
val col = pos.second
mat[row][col]=-mat[row][col]
// check for the completely painted row or column
if (checkRow(row, mat) || checkCol(col, mat))
return index
}
return 0
}
private fun checkRow(row: Int, mat: Array<IntArray>): Boolean {
val n = mat[0].size
for (col in 0 until n) {
if (mat[row][col] >= 0)
return false
}
return true
}
private fun checkCol(col: Int, mat: Array<IntArray>): Boolean {
val m = mat.size
for (row in 0 until m) {
if (mat[row][col] >= 0)
return false
}
return true
}
}