-
Notifications
You must be signed in to change notification settings - Fork 3
/
CellsWithOddValuesMatrix1252.kt
50 lines (41 loc) · 1.24 KB
/
CellsWithOddValuesMatrix1252.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
50
package easy
/*
Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.
Return the number of cells with odd values in the matrix after applying the increment to all indices.
Example 1:
Input: n = 2, m = 3, indices = [[0,1],[1,1]]
Output: 6
Explanation: Initial matrix = [[0,0,0],[0,0,0]].
After applying first increment it becomes [[1,2,1],[0,1,0]].
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.
*/
fun oddCells(n: Int, m: Int, indices: Array<IntArray>): Int {
val array = Array(n) { IntArray(m) }
for (i in 0 until n)
{
for (j in 0 until m)
{
array[i][j]=0
}
}
indices.forEach {cell->
array.forEachIndexed { i,row->
row.forEachIndexed { j, column ->
if(cell[0]==i)
array[i][j]++
if(cell[1]==j)
array[i][j]++
}
}
}
var result=0
for (i in array.indices)
{
for (j in array[i].indices)
{
if(array[i][j]%2!=0)
result++
}
}
return result
}