From 1747fdb6805f8e4181a6120e545300432591d3f1 Mon Sep 17 00:00:00 2001 From: aucker Date: Tue, 25 Jun 2024 08:29:05 +0800 Subject: [PATCH] June25: Good Subset of matrix [H] Hard, subset --- daily/June25.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 daily/June25.cpp diff --git a/daily/June25.cpp b/daily/June25.cpp new file mode 100644 index 0000000..cc8cd8d --- /dev/null +++ b/daily/June25.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief LC: 2732: Good Subset of Matrix [H] + * Time: + * Space: + * + * @param grid + * @return vector + */ + vector goodSubsetofBinaryMatrix(vector>& grid) { + vector a[1 << 5]; + vector ans; + int m = grid.size(); + int n = grid[0].size(); + for (int i = 0; i < m; i++) { + int s = 0; + for (auto c : grid[i]) { + s = (s << 1) + c; + } + if (s == 0) { + ans.push_back(i); + return ans; + } + a[s].push_back(i); + } + for (int i = 0; i < (1 << n); i++) { + if (a[i].size() == 0) { + continue; + } + for (int j = i + 1; j < (1 << n); j++) { + if (a[j].size() == 0) { + continue; + } + if ((i & j) == 0) { + ans.push_back(a[i][0]); + ans.push_back(a[j][0]); + sort(ans.begin(), ans.end()); + return ans; + } + } + } + return ans; + } +}; \ No newline at end of file