LeetCode/cs/check-if-grid-can-be-cut-into-sections.cs

26 lines
730 B
C#

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);
}