leetcode 850. Rectangle Area II | 850. 矩形面积 II(递归分割未重叠矩形)
題目
https://leetcode.com/problems/rectangle-area-ii/
題解
沒有看懂官方答案,評論區有一種解法寫的挺通俗的:
Clean Recursive Solution [Java]
The idea here is to maintain a list of non-overlapping rectangles to calculate final area.
If a new rectangle does not overlap with any of the existing rectanlges, add it to the list.
If there is an overlap, split the non-overlapping regions of the rectangle into smaller rectangles and compare with the rest of the list.
For example, when a new rectangle (green) is compared with the current rectangle (blue), the non-overlapping regions can be split into two smaller rectangles. Rectangle 1 will be covered by the first overlapping case in addRectangle() and rectangle 2 will be covered by the third case. Rectangle 3 overlaps with the current rectangle and need not be considered.
看了之后自己又實現了一遍:
總結
以上是生活随笔為你收集整理的leetcode 850. Rectangle Area II | 850. 矩形面积 II(递归分割未重叠矩形)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 36. Valid S
- 下一篇: leetcode 464. Can I