Skip to content

Commit d595ebe

Browse files
Add initial project files: .gitignore, VSCode settings, and README for Leetcode SQL problems
0 parents  commit d595ebe

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnPaste": true
3+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Leetcode | Top SQL 50
2+
3+
01. [Recyclable and Low Fat Products](solutions/01.1757.recyclable-and-low-fat-products/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 (&#39;Y&#39;, &#39;N&#39;) where &#39;Y&#39; means this product is low fat and &#39;N&#39; means it is not.
28+
recyclable is an ENUM (category) of types (&#39;Y&#39;, &#39;N&#39;) where &#39;Y&#39; means this product is recyclable and &#39;N&#39; means it is not.</pre>
29+
30+
<p>&nbsp;</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>&nbsp;</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

Comments
 (0)