diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md index 215f07dba3821..27fd9b41c3d06 100644 --- a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md @@ -91,25 +91,165 @@ tags: #### Python3 ```python - +class Solution: + def goodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]: + g = {} + for i, row in enumerate(grid): + mask = 0 + for j, x in enumerate(row): + mask |= x << j + if mask == 0: + return [i] + g[mask] = i + for a, i in g.items(): + for b, j in g.items(): + if (a & b) == 0: + return sorted([i, j]) + return [] ``` #### Java ```java - +class Solution { + public List goodSubsetofBinaryMatrix(int[][] grid) { + Map g = new HashMap<>(); + for (int i = 0; i < grid.length; ++i) { + int mask = 0; + for (int j = 0; j < grid[0].length; ++j) { + mask |= grid[i][j] << j; + } + if (mask == 0) { + return List.of(i); + } + g.put(mask, i); + } + for (var e1 : g.entrySet()) { + for (var e2 : g.entrySet()) { + if ((e1.getKey() & e2.getKey()) == 0) { + int i = e1.getValue(), j = e2.getValue(); + return List.of(Math.min(i, j), Math.max(i, j)); + } + } + } + return List.of(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector goodSubsetofBinaryMatrix(vector>& grid) { + unordered_map g; + for (int i = 0; i < grid.size(); ++i) { + int mask = 0; + for (int j = 0; j < grid[0].size(); ++j) { + mask |= grid[i][j] << j; + } + if (mask == 0) { + return {i}; + } + g[mask] = i; + } + for (auto& [a, i] : g) { + for (auto& [b, j] : g) { + if ((a & b) == 0) { + return {min(i, j), max(i, j)}; + } + } + } + return {}; + } +}; ``` #### Go ```go +func goodSubsetofBinaryMatrix(grid [][]int) []int { + g := map[int]int{} + for i, row := range grid { + mask := 0 + for j, x := range row { + mask |= x << j + } + if mask == 0 { + return []int{i} + } + g[mask] = i + } + for a, i := range g { + for b, j := range g { + if a&b == 0 { + return []int{min(i, j), max(i, j)} + } + } + } + return []int{} +} +``` + +#### TypeScript + +```ts +function goodSubsetofBinaryMatrix(grid: number[][]): number[] { + const g: Map = new Map(); + const m = grid.length; + const n = grid[0].length; + for (let i = 0; i < m; ++i) { + let mask = 0; + for (let j = 0; j < n; ++j) { + mask |= grid[i][j] << j; + } + if (!mask) { + return [i]; + } + g.set(mask, i); + } + for (const [a, i] of g.entries()) { + for (const [b, j] of g.entries()) { + if ((a & b) === 0) { + return [Math.min(i, j), Math.max(i, j)]; + } + } + } + return []; +} +``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn good_subsetof_binary_matrix(grid: Vec>) -> Vec { + let mut g: HashMap = HashMap::new(); + for (i, row) in grid.iter().enumerate() { + let mut mask = 0; + for (j, &x) in row.iter().enumerate() { + mask |= x << j; + } + if mask == 0 { + return vec![i as i32]; + } + g.insert(mask, i as i32); + } + + for (&a, &i) in g.iter() { + for (&b, &j) in g.iter() { + if (a & b) == 0 { + return vec![i.min(j), i.max(j)]; + } + } + } + + vec![] + } +} ``` diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md index d3dd728330d2d..8aa57c0909d5d 100644 --- a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md @@ -89,25 +89,165 @@ The length of the chosen subset is 1. #### Python3 ```python - +class Solution: + def goodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]: + g = {} + for i, row in enumerate(grid): + mask = 0 + for j, x in enumerate(row): + mask |= x << j + if mask == 0: + return [i] + g[mask] = i + for a, i in g.items(): + for b, j in g.items(): + if (a & b) == 0: + return sorted([i, j]) + return [] ``` #### Java ```java - +class Solution { + public List goodSubsetofBinaryMatrix(int[][] grid) { + Map g = new HashMap<>(); + for (int i = 0; i < grid.length; ++i) { + int mask = 0; + for (int j = 0; j < grid[0].length; ++j) { + mask |= grid[i][j] << j; + } + if (mask == 0) { + return List.of(i); + } + g.put(mask, i); + } + for (var e1 : g.entrySet()) { + for (var e2 : g.entrySet()) { + if ((e1.getKey() & e2.getKey()) == 0) { + int i = e1.getValue(), j = e2.getValue(); + return List.of(Math.min(i, j), Math.max(i, j)); + } + } + } + return List.of(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector goodSubsetofBinaryMatrix(vector>& grid) { + unordered_map g; + for (int i = 0; i < grid.size(); ++i) { + int mask = 0; + for (int j = 0; j < grid[0].size(); ++j) { + mask |= grid[i][j] << j; + } + if (mask == 0) { + return {i}; + } + g[mask] = i; + } + for (auto& [a, i] : g) { + for (auto& [b, j] : g) { + if ((a & b) == 0) { + return {min(i, j), max(i, j)}; + } + } + } + return {}; + } +}; ``` #### Go ```go +func goodSubsetofBinaryMatrix(grid [][]int) []int { + g := map[int]int{} + for i, row := range grid { + mask := 0 + for j, x := range row { + mask |= x << j + } + if mask == 0 { + return []int{i} + } + g[mask] = i + } + for a, i := range g { + for b, j := range g { + if a&b == 0 { + return []int{min(i, j), max(i, j)} + } + } + } + return []int{} +} +``` + +#### TypeScript + +```ts +function goodSubsetofBinaryMatrix(grid: number[][]): number[] { + const g: Map = new Map(); + const m = grid.length; + const n = grid[0].length; + for (let i = 0; i < m; ++i) { + let mask = 0; + for (let j = 0; j < n; ++j) { + mask |= grid[i][j] << j; + } + if (!mask) { + return [i]; + } + g.set(mask, i); + } + for (const [a, i] of g.entries()) { + for (const [b, j] of g.entries()) { + if ((a & b) === 0) { + return [Math.min(i, j), Math.max(i, j)]; + } + } + } + return []; +} +``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn good_subsetof_binary_matrix(grid: Vec>) -> Vec { + let mut g: HashMap = HashMap::new(); + for (i, row) in grid.iter().enumerate() { + let mut mask = 0; + for (j, &x) in row.iter().enumerate() { + mask |= x << j; + } + if mask == 0 { + return vec![i as i32]; + } + g.insert(mask, i as i32); + } + + for (&a, &i) in g.iter() { + for (&b, &j) in g.iter() { + if (a & b) == 0 { + return vec![i.min(j), i.max(j)]; + } + } + } + + vec![] + } +} ``` diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.cpp b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.cpp new file mode 100644 index 0000000000000..01b307e73c216 --- /dev/null +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector goodSubsetofBinaryMatrix(vector>& grid) { + unordered_map g; + for (int i = 0; i < grid.size(); ++i) { + int mask = 0; + for (int j = 0; j < grid[0].size(); ++j) { + mask |= grid[i][j] << j; + } + if (mask == 0) { + return {i}; + } + g[mask] = i; + } + for (auto& [a, i] : g) { + for (auto& [b, j] : g) { + if ((a & b) == 0) { + return {min(i, j), max(i, j)}; + } + } + } + return {}; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.go b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.go new file mode 100644 index 0000000000000..bd042b73b6220 --- /dev/null +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.go @@ -0,0 +1,21 @@ +func goodSubsetofBinaryMatrix(grid [][]int) []int { + g := map[int]int{} + for i, row := range grid { + mask := 0 + for j, x := range row { + mask |= x << j + } + if mask == 0 { + return []int{i} + } + g[mask] = i + } + for a, i := range g { + for b, j := range g { + if a&b == 0 { + return []int{min(i, j), max(i, j)} + } + } + } + return []int{} +} \ No newline at end of file diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.java b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.java new file mode 100644 index 0000000000000..2927b8da0ac7a --- /dev/null +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.java @@ -0,0 +1,24 @@ +class Solution { + public List goodSubsetofBinaryMatrix(int[][] grid) { + Map g = new HashMap<>(); + for (int i = 0; i < grid.length; ++i) { + int mask = 0; + for (int j = 0; j < grid[0].length; ++j) { + mask |= grid[i][j] << j; + } + if (mask == 0) { + return List.of(i); + } + g.put(mask, i); + } + for (var e1 : g.entrySet()) { + for (var e2 : g.entrySet()) { + if ((e1.getKey() & e2.getKey()) == 0) { + int i = e1.getValue(), j = e2.getValue(); + return List.of(Math.min(i, j), Math.max(i, j)); + } + } + } + return List.of(); + } +} \ No newline at end of file diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.py b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.py new file mode 100644 index 0000000000000..c9765a82fcbed --- /dev/null +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def goodSubsetofBinaryMatrix(self, grid: List[List[int]]) -> List[int]: + g = {} + for i, row in enumerate(grid): + mask = 0 + for j, x in enumerate(row): + mask |= x << j + if mask == 0: + return [i] + g[mask] = i + for a, i in g.items(): + for b, j in g.items(): + if (a & b) == 0: + return sorted([i, j]) + return [] diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.rs b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.rs new file mode 100644 index 0000000000000..e8feeae4179dd --- /dev/null +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.rs @@ -0,0 +1,27 @@ +use std::collections::HashMap; + +impl Solution { + pub fn good_subsetof_binary_matrix(grid: Vec>) -> Vec { + let mut g: HashMap = HashMap::new(); + for (i, row) in grid.iter().enumerate() { + let mut mask = 0; + for (j, &x) in row.iter().enumerate() { + mask |= x << j; + } + if mask == 0 { + return vec![i as i32]; + } + g.insert(mask, i as i32); + } + + for (&a, &i) in g.iter() { + for (&b, &j) in g.iter() { + if (a & b) == 0 { + return vec![i.min(j), i.max(j)]; + } + } + } + + vec![] + } +} diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.ts b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.ts new file mode 100644 index 0000000000000..1314da0fd86f5 --- /dev/null +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/Solution.ts @@ -0,0 +1,23 @@ +function goodSubsetofBinaryMatrix(grid: number[][]): number[] { + const g: Map = new Map(); + const m = grid.length; + const n = grid[0].length; + for (let i = 0; i < m; ++i) { + let mask = 0; + for (let j = 0; j < n; ++j) { + mask |= grid[i][j] << j; + } + if (!mask) { + return [i]; + } + g.set(mask, i); + } + for (const [a, i] of g.entries()) { + for (const [b, j] of g.entries()) { + if ((a & b) === 0) { + return [Math.min(i, j), Math.max(i, j)]; + } + } + } + return []; +}