Rectangles Area — LeetCode 1459 Python Solution
MediumLeetCode PremiumDatabase
- Problem
- #1459
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
SQL
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
SQL
+---------------+---------+
| 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
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.