题目描述

(通过次数5,158 | 提交次数8,221,通过率62.74%)

表: Points
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| x_value | int |
| y_value | int |
+---------------+---------+
id 是该表主键
每个点都用二维坐标 (x_value, y_value) 表示
写一个 SQL 语句,报告由表中任意两点可以形成的所有 边与坐标轴平行 且 面积不为零 的矩形。
结果表中的每一行包含三列 (p1, p2, area)如下:
p1和p2是矩形两个对角的 id
矩形的面积由列area表示
请按照面积area 大小降序排列;如果面积相同的话, 则按照p1升序排序;若仍相同,则按 p2 升序排列。
查询结果如下例所示:
Points 表:
+----------+-------------+-------------+
| id | x_value | y_value |
+----------+-------------+-------------+
| 1 | 2 | 7 |
| 2 | 4 | 8 |
| 3 | 2 | 10 |
+----------+-------------+-------------+
Result 表:
+----------+-------------+-------------+
| p1 | p2 | area |
+----------+-------------+-------------+
| 2 | 3 | 4 |
| 1 | 2 | 2 |
+----------+-------------+-------------+
p1 = 2 且 p2 = 3 时, 面积等于 |4-2| * |8-10| = 4
p1 = 1 且 p2 = 2 时, 面积等于 ||2-4| * |7-8| = 2
p1 = 1 且 p2 = 3 时, 是不可能为矩形的, 面积等于 0
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rectangles-area
表: Points +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | x_value | int | | y_value | int | +---------------+---------+ id 是该表主键 每个点都用二维坐标 (x_value, y_value) 表示 写一个 SQL 语句,报告由表中任意两点可以形成的所有 边与坐标轴平行 且 面积不为零 的矩形。 结果表中的每一行包含三列 (p1, p2, area)如下: p1和p2是矩形两个对角的 id 矩形的面积由列area表示 请按照面积area 大小降序排列;如果面积相同的话, 则按照p1升序排序;若仍相同,则按 p2 升序排列。 查询结果如下例所示: Points 表: +----------+-------------+-------------+ | id | x_value | y_value | +----------+-------------+-------------+ | 1 | 2 | 7 | | 2 | 4 | 8 | | 3 | 2 | 10 | +----------+-------------+-------------+ Result 表: +----------+-------------+-------------+ | p1 | p2 | area | +----------+-------------+-------------+ | 2 | 3 | 4 | | 1 | 2 | 2 | +----------+-------------+-------------+ p1 = 2 且 p2 = 3 时, 面积等于 |4-2| * |8-10| = 4 p1 = 1 且 p2 = 2 时, 面积等于 ||2-4| * |7-8| = 2 p1 = 1 且 p2 = 3 时, 是不可能为矩形的, 面积等于 0 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/rectangles-area
表: Points
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| x_value       | int     |
| y_value       | int     |
+---------------+---------+
id 是该表主键
每个点都用二维坐标 (x_value, y_value) 表示

写一个 SQL 语句,报告由表中任意两点可以形成的所有 边与坐标轴平行 且 面积不为零 的矩形。
结果表中的每一行包含三列 (p1, p2, area)如下:
p1和p2是矩形两个对角的 id
矩形的面积由列area表示
请按照面积area 大小降序排列;如果面积相同的话, 则按照p1升序排序;若仍相同,则按 p2 升序排列。

查询结果如下例所示:
Points 表:
+----------+-------------+-------------+
| id       | x_value     | y_value     |
+----------+-------------+-------------+
| 1        | 2           | 7           |
| 2        | 4           | 8           |
| 3        | 2           | 10          |
+----------+-------------+-------------+
Result 表:
+----------+-------------+-------------+
| p1       | p2          | area        |
+----------+-------------+-------------+
| 2        | 3           | 4           |
| 1        | 2           | 2           |
+----------+-------------+-------------+
p1 = 2 且 p2 = 3 时, 面积等于 |4-2| * |8-10| = 4
p1 = 1 且 p2 = 2 时, 面积等于 ||2-4| * |7-8| = 2 
p1 = 1 且 p2 = 3 时, 是不可能为矩形的, 面积等于 0

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rectangles-area
//测试数据
Create table If Not Exists Points (id int, x_value int, y_value int);
insert into Points (id, x_value, y_value) values ('1', '2', '7');
insert into Points (id, x_value, y_value) values ('2', '4', '8');
insert into Points (id, x_value, y_value) values ('3', '2', '10');
//测试数据 Create table If Not Exists Points (id int, x_value int, y_value int); insert into Points (id, x_value, y_value) values ('1', '2', '7'); insert into Points (id, x_value, y_value) values ('2', '4', '8'); insert into Points (id, x_value, y_value) values ('3', '2', '10');
//测试数据
Create table If Not Exists Points (id int, x_value int, y_value int);

insert into Points (id, x_value, y_value) values ('1', '2', '7');
insert into Points (id, x_value, y_value) values ('2', '4', '8');
insert into Points (id, x_value, y_value) values ('3', '2', '10');


解题思路

Points表存储了坐标系上的一些点。坐标系上的任意两点,可能可以组成一个矩形。
既然是两两组合,正好笛卡尔积非常合适。我们首先可以将坐标系上所有点的组合生成出来。
可以使用如下代码:

select
a.id p1,
a.x_value,
a.y_value,
b.id p2,
b.x_value,
b.y_value
from Points a
inner join Points b
on 1=1;
select a.id p1, a.x_value, a.y_value, b.id p2, b.x_value, b.y_value from Points a inner join Points b on 1=1;
select
    a.id p1,
    a.x_value,
    a.y_value,
    b.id p2,
    b.x_value,
    b.y_value
from Points a
inner join Points b
on 1=1;

既然所有点的组合都有了,直接根据公式计算面积即可。
但这里有两个点需要考虑:
第一:不是任意的两个点都可以形成矩形。比如,x轴或y轴坐标相同的两个点,是不能形成矩形的,只能形成一个线段。
所以,需要对笛卡尔积的结果进行裁剪。将x轴的坐标相同或y轴的坐标相同的组合剔除。
第二:要考虑剔除重复情况。比如,a点与b点形成的矩形,与b点和a点形成的矩形是相同的,没必要在结果中重复出现。
那么,我们可以人为的要求组合中,第1个点的ID小于第2个点的ID,这样就可以剔除重复情况。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select
a.id p1,
b.id p2,
abs((a.x_value - b.x_value) * (a.y_value - b.y_value)) area
from Points a
inner join Points b
on 1=1
and a.x_value <> b.x_value
and a.y_value <> b.y_value
and a.id < b.id
order by 3 desc,1,2;
select a.id p1, b.id p2, abs((a.x_value - b.x_value) * (a.y_value - b.y_value)) area from Points a inner join Points b on 1=1 and a.x_value <> b.x_value and a.y_value <> b.y_value and a.id < b.id order by 3 desc,1,2;
select
    a.id p1,
    b.id p2,
    abs((a.x_value - b.x_value) * (a.y_value - b.y_value)) area
from Points a
inner join Points b
on 1=1
and a.x_value <> b.x_value
and a.y_value <> b.y_value
and a.id < b.id
order by 3 desc,1,2;
picture loss