MediumNo. 1459LeetCode Premium
Rectangles Area — LeetCode 1459 Python Solution
- Topics
- Database
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
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).Example
+---------------+---------+
| 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).Python solution
import duckdb
import pandas as pd
def solution(points: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Points", points)
return con.execute("""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;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 1459. Rectangles Area?
LeetCode 1459. Rectangles Area is rated Medium on LeetCode.
- What topics does LeetCode 1459. Rectangles Area cover?
LeetCode 1459. Rectangles Area is tagged Database on LeetCode.
- Is LeetCode 1459. Rectangles Area a premium problem?
Yes. LeetCode 1459. Rectangles Area is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.