Pārlūkot izejas kodu

New problem done

master
Lachlan Jacob pirms 5 gadiem
vecāks
revīzija
eca730702c
3 mainītis faili ar 65 papildinājumiem un 0 dzēšanām
  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 Parādīt failu

@@ -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 Parādīt failu

@@ -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 Parādīt failu

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

python3 main.py

Notiek ielāde…
Atcelt
Saglabāt