Table: Products
+-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | low_fats | enum | | recyclable | enum | +-------------+---------+ product_id is the primary key (column with unique values) for this table. low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not. recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.
Write a solution to find the ids of products that are both low fat and recyclable.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Products table: +-------------+----------+------------+ | product_id | low_fats | recyclable | +-------------+----------+------------+ | 0 | Y | N | | 1 | Y | Y | | 2 | N | Y | | 3 | Y | Y | | 4 | N | N | +-------------+----------+------------+ Output: +-------------+ | product_id | +-------------+ | 1 | | 3 | +-------------+ Explanation: Only products 1 and 3 are both low fat and recyclable.
The problem requires retrieving product_id
s from the Products
table where both low_fats
and recyclable
columns have the value 'Y'. This is a simple filtering task achievable using SQL or Pandas.
Both solutions employ a straightforward filtering approach. They check for the desired conditions (low_fats = 'Y'
and recyclable = 'Y'
) and select only the matching product_id
s.
The Python solution utilizes the Pandas library for data manipulation.
import pandas as pd
def find_products(products: pd.DataFrame) -> pd.DataFrame:
rs = products[(products["low_fats"] == "Y") & (products["recyclable"] == "Y")]
rs = rs[["product_id"]]
return rs
import pandas as pd
: Imports the Pandas library.def find_products(products: pd.DataFrame) -> pd.DataFrame:
: Defines a function that takes a Pandas DataFrame (products
) as input and returns another Pandas DataFrame.rs = products[(products["low_fats"] == "Y") & (products["recyclable"] == "Y")]
: This line performs the core filtering. It creates a boolean mask by comparing the low_fats
and recyclable
columns to 'Y'. The &
operator ensures both conditions must be true. The resulting boolean mask is then used to select only the rows that satisfy both conditions.rs = rs[["product_id"]]
: This line selects only the product_id
column from the filtered DataFrame.return rs
: The function returns the DataFrame containing only the product_id
s of products that are both low-fat and recyclable.The MySQL solution is a concise SQL query:
SELECT
product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
SELECT product_id
: Specifies that only the product_id
column should be returned.FROM Products
: Indicates that the data should be retrieved from the Products
table.WHERE low_fats = 'Y' AND recyclable = 'Y'
: This is the filtering condition. It selects rows where both low_fats
and recyclable
columns are equal to 'Y'.Both solutions have a time complexity of O(n), where n is the number of rows in the Products
table. This is because both the Pandas filtering and the SQL WHERE
clause need to iterate (or at least perform a scan) through the table to identify matching rows. The filtering operation itself is linear in the number of rows.
The space complexity of both solutions is O(k) where k is the number of rows that satisfy the condition. In the worst case, k could be equal to n, but in many practical scenarios, k will be significantly smaller than n. The Python solution uses extra space for the creation of the resulting Pandas DataFrame, while the SQL solution's space usage is determined by the size of the result set.