Skip to content

Create 3394. Check if Grid can be Cut into Sections #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 3394. Check if Grid can be Cut into Sections
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public:
bool checkValidCuts(int n, vector<vector<int>>& rectangles) {
int s = rectangles.size();
vector<vector<int>> ver, hor;

// Store vertical and horizontal segment ranges
for (auto i : rectangles) {
ver.push_back({i[0], i[2]});
hor.push_back({i[1], i[3]});
}

// Sort vertical segments
sort(ver.begin(), ver.end());
int cnt = 1, mx = ver[0][1];

// Check for vertical cuts
for (int i = 1; i < s; i++) {
if (ver[i][0] < mx)
mx = max(mx, ver[i][1]);
else {
cnt++;
mx = ver[i][1];
}
if (cnt >= 3) return true;
}

// Sort horizontal segments
sort(hor.begin(), hor.end());
cnt = 1, mx = hor[0][1];

// Check for horizontal cuts
for (int i = 1; i < s; i++) {
if (hor[i][0] < mx)
mx = max(mx, hor[i][1]);
else {
cnt++;
mx = hor[i][1];
}
if (cnt >= 3) return true;
}

return false;
}
};
Loading