|
| 1 | +class Solution: |
| 2 | + def __calculate_boundaries(self, rectangles: list[list[int]], dim: int) -> list[tuple[int, int]]: |
| 3 | + boundaries = [(rectangle[dim], rectangle[dim+2]) for rectangle in rectangles] |
| 4 | + boundaries.sort() |
| 5 | + return boundaries |
| 6 | + |
| 7 | + def __line_sweep(self, lines: list[tuple[int, int]]) -> bool: |
| 8 | + n_lines = 0 |
| 9 | + last = lines[0][1] |
| 10 | + for start, end in lines: |
| 11 | + if start >= last: |
| 12 | + n_lines += 1 |
| 13 | + if n_lines == 2: |
| 14 | + return True |
| 15 | + last = max(last, end) |
| 16 | + return False |
| 17 | + |
| 18 | + def checkValidCuts(self, _: int, rectangles: list[list[int]]) -> bool: |
| 19 | + h_lines = self.__calculate_boundaries(rectangles, 1) |
| 20 | + v_lines = self.__calculate_boundaries(rectangles, 0) |
| 21 | + return self.__line_sweep(h_lines) or self.__line_sweep(v_lines) |
| 22 | + |
| 23 | + |
| 24 | +def main(): |
| 25 | + n = 5 |
| 26 | + rectangles = [[1, 0, 5, 2], [0, 2, 2, 4], [3, 2, 5, 3], [0, 4, 4, 5]] |
| 27 | + assert Solution().checkValidCuts(n, rectangles) is True |
| 28 | + |
| 29 | + n = 4 |
| 30 | + rectangles = [[0, 0, 1, 1], [2, 0, 3, 4], [0, 2, 2, 3], [3, 0, 4, 3]] |
| 31 | + assert Solution().checkValidCuts(n, rectangles) is True |
| 32 | + |
| 33 | + n = 4 |
| 34 | + rectangles = [[0, 2, 2, 4], [1, 0, 3, 2], [2, 2, 3, 4], [3, 0, 4, 2], [3, 2, 4, 4]] |
| 35 | + assert Solution().checkValidCuts(n, rectangles) is False |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == '__main__': |
| 39 | + main() |
0 commit comments