1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00

problems(swift): add “1582. Special Positions in a Binary Matrix”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-13 18:06:57 +01:00
parent a48dd4765e
commit eddb4c1a55
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,25 @@
class Solution {
func numSpecial(_ mat: [[Int]]) -> Int {
let m = mat.count
let n = mat[0].count
var rows: [Int] = Array(repeating: 0, count: m)
var cols: [Int] = Array(repeating: 0, count: n)
for (y, x) in (0..<m).flatMap { y in (0..<n).map {x in (y, x) } } {
if mat[y][x] == 1 {
rows[y] += 1
cols[x] += 1
}
}
var count = 0
for (y, x) in (0..<m).flatMap { y in (0..<n).map {x in (y, x) } } {
if mat[y][x] == 1 && rows[y] == 1 && cols[x] == 1 {
count += 1
}
}
return count
}
}