Form a Chemical Bond — LeetCode 2480 Python Solution

EasyLeetCode PremiumDatabase
Problem
#2480
Reading time
3 min

Table schema

SQL
Table: Elements +-------------+---------+ | Column Name | Type | +-------------+---------+ | symbol | varchar | | type | enum | | electrons | int | +-------------+---------+ symbol is the primary key (column with unique values) for this table. Each row of this table contains information of one element.

Example

SQL
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| symbol      | varchar |
| type        | enum    |
| electrons   | int     |
+-------------+---------+
symbol is the primary key (column with unique values) for this table.
Each row of this table contains information of one element.
type is an ENUM (category) of type ('Metal', 'Nonmetal', 'Noble')
 - If type is Noble, electrons is 0.
 - If type is Metal, electrons is the number of electrons that one atom of this element can give.
 - If type is Nonmetal, electrons is the number of electrons that one atom of this element needs.

Python solution

Python
import duckdb
import pandas as pd

def solution(elements: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Elements", elements)
    return con.execute("""SELECT a.symbol AS metal, b.symbol AS nonmetal
FROM
    Elements AS a,
    Elements AS b
WHERE a.type = 'Metal' AND b.type = 'Nonmetal';""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2480. Form a Chemical Bond?
LeetCode 2480. Form a Chemical Bond is rated Easy on LeetCode.
What topics does LeetCode 2480. Form a Chemical Bond cover?
LeetCode 2480. Form a Chemical Bond is tagged Database on LeetCode.
Is LeetCode 2480. Form a Chemical Bond a premium problem?
Yes. LeetCode 2480. Form a Chemical Bond 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