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:
parent
e810e992fe
commit
8fa1d853ff
1 changed files with 26 additions and 0 deletions
26
cs/check-if-grid-can-be-cut-into-sections.cs
Normal file
26
cs/check-if-grid-can-be-cut-into-sections.cs
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue