返回介绍

solution / 1400-1499 / 1459.Rectangles Area / README_EN

发布于 2024-06-17 01:03:19 字数 3030 浏览 0 评论 0 收藏 0

1459. Rectangles Area

中文文档

Description

Table: Points

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| id      | int   |
| x_value     | int   |
| y_value     | int   |
+---------------+---------+
id is the column with unique values for this table.
Each point is represented as a 2D coordinate (x_value, y_value).

 

Write a solution to report all possible axis-aligned rectangles with a non-zero area that can be formed by any two points from the Points table.

Each row in the result should contain three columns (p1, p2, area) where:

  • p1 and p2 are the id's of the two points that determine the opposite corners of a rectangle.
  • area is the area of the rectangle and must be non-zero.

Return the result table ordered by area in descending order. If there is a tie, order them by p1 in ascending order. If there is still a tie, order them by p2 in ascending order.

The result format is in the following table.

 

Example 1:

Input: 
Points table:
+----------+-------------+-------------+
| id     | x_value   | y_value   |
+----------+-------------+-------------+
| 1    | 2       | 7       |
| 2    | 4       | 8       |
| 3    | 2       | 10      |
+----------+-------------+-------------+
Output: 
+----------+-------------+-------------+
| p1     | p2      | area    |
+----------+-------------+-------------+
| 2    | 3       | 4       |
| 1    | 2       | 2       |
+----------+-------------+-------------+
Explanation: 
The rectangle formed by p1 = 2 and p2 = 3 has an area equal to |4-2| * |8-10| = 4.
The rectangle formed by p1 = 1 and p2 = 2 has an area equal to |2-4| * |7-8| = 2.
Note that the rectangle formed by p1 = 1 and p2 = 3 is invalid because the area is 0.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  p1.id AS p1,
  p2.id AS p2,
  ABS(p1.x_value - p2.x_value) * ABS(p1.y_value - p2.y_value) AS area
FROM
  Points AS p1
  JOIN Points AS p2 ON p1.id < p2.id
WHERE p1.x_value != p2.x_value AND p1.y_value != p2.y_value
ORDER BY area DESC, p1, p2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文