ソースを参照

New problem done

master
Lachlan Jacob 5年前
コミット
eca730702c
3個のファイルの変更65行の追加0行の削除
  1. 32
    0
      problems/1252/main.py
  2. 30
    0
      problems/1252/problem.txt
  3. 3
    0
      problems/1252/run.sh

+ 32
- 0
problems/1252/main.py ファイルの表示

@@ -0,0 +1,32 @@
class Solution:
def oddCells(self, n, m, indices):
# Initialise the matrix
matrix = []
for x in range(n):
new_row = []
for y in range(m):
new_row.append(0)
matrix.append(new_row)
# Now apply steps
for i in indices:
ri = i[0]
ci = i[1]
for n in range(len(matrix[ri])):
matrix[ri][n] += 1
for n in range(len(matrix)):
matrix[n][ci] += 1
# now count odd values (this could be done while changes are made, but this is easier to reason)
count = 0
for x in range(len(matrix)):
for y in range(len(matrix[x])):
if matrix[x][y] % 2 != 0:
count += 1
return count

s = Solution()
print("Expected: 6")
print("Got:", s.oddCells(2, 3, [[0, 1], [1, 1]]))

+ 30
- 0
problems/1252/problem.txt ファイルの表示

@@ -0,0 +1,30 @@
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.

(There was pictures here, but hopefully this is clear enough)

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.

Example 2:

Input: n = 2, m = 2, indices = [[1,1],[0,0]]
Output: 0
Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.


Constraints:

1 <= n <= 50
1 <= m <= 50
1 <= indices.length <= 100
0 <= indices[i][0] < n
0 <= indices[i][1] < m


+ 3
- 0
problems/1252/run.sh ファイルの表示

@@ -0,0 +1,3 @@
#!/bin/bash

python3 main.py

読み込み中…
キャンセル
保存