当前位置:网站首页>LeetCode_容斥原理_中等_223.矩形面积
LeetCode_容斥原理_中等_223.矩形面积
2022-07-29 11:33:00 【小城老街】
1.题目
给你二维平面上两个由直线构成且边与坐标轴平行/垂直的矩形,请你计算并返回两个矩形覆盖的总面积。
每个矩形由其左下顶点和右上顶点坐标表示:
第一个矩形由其左下顶点 (ax1, ay1) 和右上顶点 (ax2, ay2) 定义。
第二个矩形由其左下顶点 (bx1, by1) 和右上顶点 (bx2, by2) 定义。
示例 1:
输入:ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
输出:45
示例 2:
输入:ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
输出:16
提示:
-104 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 104
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rectangle-area
2.思路
(1)容斥原理
通过 2 个集合的容斥原理:A ∪ B = A + B - A ∩ B 可知,两个矩形覆盖的总面积 = 两个矩形的面积之和 - 重合部分的面积,所以我们只需要分别两个矩形的面积以及它们重合部分的面积即可,需要注意的是两个矩形可能不重合,即重合部分的面积为 0。
3.代码实现(Java)
//思路1————容斥原理
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
// 矩形 A 的面积
int areaA = (ax2 - ax1) * (ay2 - ay1);
// 矩形 B 的面积
int areaB = (bx2 - bx1) * (by2 - by1);
int x = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
int y = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
// 矩形 A 和矩形 B 重合部分的面积(可能为 0)
int coincide = x * y;
/* 2 个集合的容斥原理:A ∪ B = A + B - A ∩ B 两个矩形覆盖的总面积 = 两个矩形的面积之和 - 重合部分的面积 */
return areaA + areaB - coincide;
}
}
边栏推荐
- Regular expression matching URL
- Self collection online computer wallpaper PHP source code v2.0 adaptive end
- 8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice
- 自采集在线电脑壁纸php源码v2.0自适应端
- Learning with Recoverable Forgetting readings
- 深入理解C# 进入快速通道的委托
- Use anyio instead of asyncio
- 考完PMP后有什么益处
- SkiaSharp 之 WPF 自绘 弹动小球(案例版)
- Steps of project explanation in interview
猜你喜欢

Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life

The interviewer training courseware (very practical in-house training courseware)

Self collection online computer wallpaper PHP source code v2.0 adaptive end

DNS协议、ICMP协议、NAT技术

How to use "copy – link" to accelerate docker to build and optimize cache

Niuke net brush questions

8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice

Why should kubernetes be used in development environments

QML(一):自定义圆角按钮的处理

微信云托管入门与实践
随机推荐
Xiaoxiao authorization system V5.0 happy version
️ 炒 股 实 战丨原 地 起 飞 ️
『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
Self collection online computer wallpaper PHP source code v2.0 adaptive end
从零开始Blazor Server(3)--添加cookie授权
QML(一):自定义圆角按钮的处理
如何使用“COPY –link”加速 Docker 构建和优化缓存
【表达式计算】表达式计算问题的通用解法(练习加强版,含总结)
即学即用的问题解决思维,给无意识的生活装上“后视镜”
After connect and SQL join in on conditions and where
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
Spark efficient data analysis 01. Establishment of idea development environment
TCP and UDP
「PHP基础知识」使用数组保存数据
深入理解C# 进入快速通道的委托
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
多线程顺序运行的 4 种方法,面试随便问!
Basic. Blocking
HMS Core Discovery 16 review | with tiger mound, embracing new AI "voice" state
Starrocks technology insider: how to have both real-time update and fast query