Rectangles Area — LeetCode 1459 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1459
Reading time
3 min

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

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview