MediumNo. 1459LeetCode Premium

Rectangles Area — LeetCode 1459 Python Solution

Topics
Database
Reading time
3 min

Table schema

SQLTable 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

SQLExample
+---------------+---------+
| 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

PythonSolution
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

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

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.

Free to download. Check it runs on your machine, then pick a plan.