Python Algorithm
LeetCode
1252. Cells with Odd Values in a Matrix
- There is an m x n matrix that is initialized to all 0's. There is also a 2D array [indices] where each indices[i] = [rᵢ, cᵢ] represents a 0-indexed location to perform some increment operations on the matrix.
- For each location indices[i], do both of the following:
- Increment all the cells on row rᵢ.
- Increment all the cells on column cᵢ.
- Given m, n, and [indices], return the number of odd-valued cells in the matrix after applying the increment to all locations in [indices].
- Constraints
- 1 ≤ m, n ≤ 50
- 1 ≤ indices.length ≤ 100
- 0 ≤ ri < m
- 0 ≤ ci < n
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
matrix = [[0 for x in range(n)] for y in range(m)]
for row in matrix:
print(row)
answer = 0
for row, col in indices:
for i in range(n):
# 특정 row의 값을 1씩 증가시켜 주기
matrix[row][i] += 1
for i in range(m):
# 특정 column 값을 1씩 증가시켜 주기
matrix[i][col] += 1
# 전체 matrix 순회하면서 홀수 개수 세어서 리턴
for i in range(m):
for j in range(n):
if matrix[i][j] % 2 == 1:
answer += 1
return answer
import numpy as np
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
answer = 0
mat = np.zeros((m, n))
for r, c in indices:
mat[r] += 1
mat[:, c] += 1
for i in range(m):
for j in range(n):
if mat[i][j] % 2 != 0:
answer += 1
return answer
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
answer = 0
mat = [[0 for _ in range(n)] for _ in range(m)]
for r, c in indices:
mat[r] = [x + 1 for x in mat[r]]
for col in mat:
col[c] += 1
for i in range(m):
for j in range(n):
if mat[i][j] % 2 != 0:
answer += 1
return answer
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
answer = 0
mat = [[0 for _ in range(n)] for _ in range(m)]
for r, c in indices:
for i in range(n):
mat[r][i] += 1
for i in range(m):
mat[i][c] += 1
for i in range(m):
for j in range(n):
if mat[i][j] % 2 != 0:
answer += 1
return answer
1351. Count Negative Numbers in a Sorted Matrix
- Given a m x n matrix [grid] which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in [grid].
- Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m, n ≤ 100
- -100 ≤ grid[i][j] ≤ 100
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
return sum([1 for row in grid for num in row if num < 0])
class Solution:
def countNegatives(self, grid: List[List[int]]) -> int:
answer = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] < 0:
answer += 1
return answer
https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글
| Python - Algorithm #49 (0) | 2024.09.09 |
|---|---|
| Python - Algorithm #48 (0) | 2024.09.06 |
| Python - Algorithm #46 (0) | 2024.09.04 |
| Python - Algorithm #45 (0) | 2024.09.03 |
| Python - Algorithm #44 (0) | 2024.09.02 |