Python | Leetcode Python题解之第391题完美矩形

题目:

题解:

python 复制代码
class Solution:
    def isRectangleCover(self, rectangles: List[List[int]]) -> bool:
        area, minX, minY, maxX, maxY = 0, rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3]
        cnt = defaultdict(int)
        for rect in rectangles:
            x, y, a, b = rect[0], rect[1], rect[2], rect[3]
            area += (a - x) * (b - y)

            minX = min(minX, x)
            minY = min(minY, y)
            maxX = max(maxX, a)
            maxY = max(maxY, b)

            cnt[(x, y)] += 1
            cnt[(x, b)] += 1
            cnt[(a, y)] += 1
            cnt[(a, b)] += 1

        if area != (maxX - minX) * (maxY - minY) or cnt[(minX, minY)] != 1 or cnt[(minX, maxY)] != 1 or cnt[(maxX, minY)] != 1 or cnt[(maxX, maxY)] != 1:
            return False

        del cnt[(minX, minY)], cnt[(minX, maxY)], cnt[(maxX, minY)], cnt[(maxX, maxY)]

        return all(c == 2 or c == 4 for c in cnt.values())
相关推荐
人机(未转人工)19 分钟前
python 批量测试ip是否能ping( 多线程 )
python
cndes24 分钟前
安装Miniconda+Jupyter notebook
ide·python·jupyter
Patrick在香港33 分钟前
Python 拉取城巴开放数据:406 条路线,香港岛巴士网络的数字画像
网络·python·数据分析·php·数据可视化·香港·开放数据
for_ever_love__43 分钟前
python基础语法学习: 正则表达式
python·学习·正则表达式
aiqianji1 小时前
功能全面的教AI短篇小说写作的软件都有哪些?
人工智能·python
轮到我狗叫了1 小时前
Slurm如何使用
人工智能·python·深度学习·机器学习
Ali885202 小时前
Python列表增删改查速查表和详细演示
python
新时代牛马2 小时前
softirq 与tasklet:从irq_exit 到__do_softirq 的下半部路径
python
tqs_123452 小时前
值传递与引用传递
java·开发语言·python
YsyaaabB2 小时前
Deep Agents 实战课程
python·langchain