cs: add «3394. Check if Grid can be Cut into Sections»

URL:	https://leetcode.com/problems/check-if-grid-can-be-cut-into-sections/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-03-25 15:53:13 +01:00
parent e810e992fe
commit 8fa1d853ff
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,26 @@
public class Solution {
private const int X = 0;
private const int Y = 1;
private bool CheckCuts(int[][] rectangles, int dimension) {
Array.Sort(rectangles, (x, y) =>
x[dimension].CompareTo(y[dimension])
);
var (gaps, furthestEnd) = (0, rectangles[0][dimension + 2]);
for (var i = 1; i < rectangles.Length; ++i) {
var r = rectangles[i];
if (furthestEnd <= r[dimension]) {
++gaps;
}
furthestEnd = Math.Max(furthestEnd, r[dimension + 2]);
}
return gaps >= 2;
}
public bool CheckValidCuts(int n, int[][] rectangles)
=> CheckCuts(rectangles, X) || CheckCuts(rectangles, Y);
}