|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +tags: |
| 5 | + - Database |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- problem:start --> |
| 9 | + |
| 10 | +# [1757. Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +<!-- description:start --> |
| 15 | + |
| 16 | +<p>Table: <code>Products</code></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | ++-------------+---------+ |
| 20 | +| Column Name | Type | |
| 21 | ++-------------+---------+ |
| 22 | +| product_id | int | |
| 23 | +| low_fats | enum | |
| 24 | +| recyclable | enum | |
| 25 | ++-------------+---------+ |
| 26 | +product_id is the primary key (column with unique values) for this table. |
| 27 | +low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not. |
| 28 | +recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | + |
| 32 | +<p>Write a solution to find the ids of products that are both low fat and recyclable.</p> |
| 33 | + |
| 34 | +<p>Return the result table in <strong>any order</strong>.</p> |
| 35 | + |
| 36 | +<p>The result format is in the following example.</p> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong class="example">Example 1:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input:</strong> |
| 43 | +Products table: |
| 44 | ++-------------+----------+------------+ |
| 45 | +| product_id | low_fats | recyclable | |
| 46 | ++-------------+----------+------------+ |
| 47 | +| 0 | Y | N | |
| 48 | +| 1 | Y | Y | |
| 49 | +| 2 | N | Y | |
| 50 | +| 3 | Y | Y | |
| 51 | +| 4 | N | N | |
| 52 | ++-------------+----------+------------+ |
| 53 | +<strong>Output:</strong> |
| 54 | ++-------------+ |
| 55 | +| product_id | |
| 56 | ++-------------+ |
| 57 | +| 1 | |
| 58 | +| 3 | |
| 59 | ++-------------+ |
| 60 | +<strong>Explanation:</strong> Only products 1 and 3 are both low fat and recyclable. |
| 61 | +</pre> |
| 62 | + |
| 63 | +<!-- description:end --> |
| 64 | + |
| 65 | +## Solutions |
| 66 | + |
| 67 | +<!-- solution:start --> |
| 68 | + |
| 69 | +### Solution 1: Conditional Filtering |
| 70 | + |
| 71 | +We can directly filter the product IDs where `low_fats` is `Y` and `recyclable` is `Y`. |
| 72 | + |
| 73 | +<!-- tabs:start --> |
| 74 | + |
| 75 | +#### Python3 |
| 76 | + |
| 77 | +```python |
| 78 | +import pandas as pd |
| 79 | + |
| 80 | + |
| 81 | +def find_products(products: pd.DataFrame) -> pd.DataFrame: |
| 82 | + rs = products[(products["low_fats"] == "Y") & (products["recyclable"] == "Y")] |
| 83 | + rs = rs[["product_id"]] |
| 84 | + return rs |
| 85 | +``` |
| 86 | + |
| 87 | +#### MySQL |
| 88 | + |
| 89 | +```sql |
| 90 | +SELECT |
| 91 | + product_id |
| 92 | +FROM Products |
| 93 | +WHERE low_fats = 'Y' AND recyclable = 'Y'; |
| 94 | +``` |
| 95 | + |
| 96 | +<!-- tabs:end --> |
| 97 | + |
| 98 | +<!-- solution:end --> |
| 99 | + |
| 100 | +<!-- problem:end --> |
0 commit comments