leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 3cd1b5564145691e318084324b1c7827181ba79b
parent 6f4ebe95ad9890f3ed441ff4d037f2c63be0888a
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue,  3 Mar 2026 20:02:53 -0700

1582. special positions in a binary matrix

Diffstat:
Mspecial_position_binary_matrix.c | 56++++++++++++++++++++++++++++++--------------------------
Mspecial_position_binary_matrix.py | 27+++++++++++++++++++--------
2 files changed, 49 insertions(+), 34 deletions(-)

diff --git a/special_position_binary_matrix.c b/special_position_binary_matrix.c @@ -7,35 +7,39 @@ * other elements in a row and column are 0 */ -// int numSpecial(int **mat, int mat_size, int *mat_col_size) { -int numSpecial(int r, int c, int mat[r][c], int mat_size, int *mat_col_size) { - int cnt = 0; - for (int rows = 0; rows < mat_size; rows++) - for (int cols = 0; cols < *mat_col_size; cols++) - if (mat[rows][cols]) { - bool match = 1; - for (int i = 0; i < *mat_col_size; i++) - if (mat[rows][i] && i != cols) { - match = 0; - break; - } - if (!match) - break; - for (int i = 0; i < mat_size; i++) - if (mat[i][cols] && i != rows) { - match = 0; - break; - } - if (match) - cnt++; +int numSpecial(int **mat, int matSize, int *matColSize) { + int n = matSize, m = matColSize[0]; + int *row_sum = (int *)calloc(n, sizeof(int)); + int *col_sum = (int *)calloc(m, sizeof(int)); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (mat[i][j]) { + row_sum[i]++; + col_sum[j]++; } + int cnt = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (mat[i][j] && row_sum[i] == 1 && col_sum[j] == 1) + cnt++; + free(row_sum); + free(col_sum); return cnt; } int main() { - int m1[3][3] = {{1, 0, 0}, {0, 0, 1}, {1, 0, 0}}, - m2[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; - int mcs1[] = {}, mcs2[] = {}; - printf("%d\n", numSpecial(3, 3, m1, 3, mcs1)); // expect: 1 - printf("%d\n", numSpecial(3, 3, m1, 3, mcs2)); // expect: 3 + int m1i[3][3] = {{1, 0, 0}, {0, 0, 1}, {1, 0, 0}}; + int m2i[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; + struct two_d_arr *m1 = + two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + struct two_d_arr *m2 = + two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + int r1 = numSpecial(m1->arr, m1->row_size, m1->col_size); + int r2 = numSpecial(m2->arr, m2->row_size, m2->col_size); + printf("%d\n", r1); // expect: 1 + assert(r1 == 1); + printf("%d\n", r2); // expect: 3 + assert(r2 == 3); + two_d_arr_free(m1); + two_d_arr_free(m2); } diff --git a/special_position_binary_matrix.py b/special_position_binary_matrix.py @@ -1,4 +1,5 @@ # 1582. Special Positions in a Binary Matrix +from typing import List """ given an 'm n' binary matrix 'mat', return the number of special positions @@ -7,15 +8,25 @@ other elements in a row and column are 0 """ -class Solution(object): - def numSpecial(self, mat): - """ - :type mat: List[List[int]] - :rtype: int - """ +class Solution: + def numSpecial(self, mat: List[List[int]]) -> int: + col_idx = [0] * len(mat) + row_sum = [0] * len(mat) + col_sum = [0] * len(mat[0]) + for r, row in enumerate(mat): + for c, col in enumerate(mat[r]): + if col: + row_sum[r] += 1 + col_sum[c] += 1 + col_idx[r] = c + cnt = 0 + for i, j in enumerate(row_sum): + if j == 1 and col_sum[col_idx[i]] == 1: + cnt += 1 + return cnt if __name__ == "__main__": obj = Solution() - print(obj.numSpecial(mat=[[1, 0, 0], [0, 0, 1], [1, 0, 0]])) # expect: 1 - print(obj.numSpecial(mat=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])) # expect: 3 + print(obj.numSpecial(mat=[[1, 0, 0], [0, 0, 1], [1, 0, 0]])) + print(obj.numSpecial(mat=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]))