mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-06 16:01:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<p>Table: <code>Insurance</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+-------+
|
||||
| Column Name | Type |
|
||||
+-------------+-------+
|
||||
| pid | int |
|
||||
| tiv_2015 | float |
|
||||
| tiv_2016 | float |
|
||||
| lat | float |
|
||||
| lon | float |
|
||||
+-------------+-------+
|
||||
pid is the primary key (column with unique values) for this table.
|
||||
Each row of this table contains information about one policy where:
|
||||
pid is the policyholder's policy ID.
|
||||
tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
|
||||
lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
|
||||
lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to report the sum of all total investment values in 2016 <code>tiv_2016</code>, for all policyholders who:</p>
|
||||
|
||||
<ul>
|
||||
<li>have the same <code>tiv_2015</code> value as one or more other policyholders, and</li>
|
||||
<li>are not located in the same city as any other policyholder (i.e., the (<code>lat, lon</code>) attribute pairs must be unique).</li>
|
||||
</ul>
|
||||
|
||||
<p>Round <code>tiv_2016</code> to <strong>two decimal places</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Insurance table:
|
||||
+-----+----------+----------+-----+-----+
|
||||
| pid | tiv_2015 | tiv_2016 | lat | lon |
|
||||
+-----+----------+----------+-----+-----+
|
||||
| 1 | 10 | 5 | 10 | 10 |
|
||||
| 2 | 20 | 20 | 20 | 20 |
|
||||
| 3 | 10 | 30 | 20 | 20 |
|
||||
| 4 | 10 | 40 | 40 | 40 |
|
||||
+-----+----------+----------+-----+-----+
|
||||
<strong>Output:</strong>
|
||||
+----------+
|
||||
| tiv_2016 |
|
||||
+----------+
|
||||
| 45.00 |
|
||||
+----------+
|
||||
<strong>Explanation:</strong>
|
||||
The first record in the table, like the last record, meets both of the two criteria.
|
||||
The tiv_2015 value 10 is the same as the third and fourth records, and its location is unique.
|
||||
|
||||
The second record does not meet any of the two criteria. Its tiv_2015 is not like any other policyholders and its location is the same as the third record, which makes the third record fail, too.
|
||||
So, the result is the sum of tiv_2016 of the first and last record, which is 45.
|
||||
</pre>
|
@@ -0,0 +1,51 @@
|
||||
<p>Table: <code>Employees</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+----------+
|
||||
| Column Name | Type |
|
||||
+-------------+----------+
|
||||
| employee_id | int |
|
||||
| name | varchar |
|
||||
| manager_id | int |
|
||||
| salary | int |
|
||||
+-------------+----------+
|
||||
In SQL, employee_id is the primary key for this table.
|
||||
This table contains information about the employees, their salary, and the ID of their manager. Some employees do not have a manager (manager_id is null).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Find the IDs of the employees whose salary is strictly less than <code>$30000</code> and whose manager left the company. When a manager leaves the company, their information is deleted from the <code>Employees</code> table, but the reports still have their <code>manager_id</code> set to the manager that left.</p>
|
||||
|
||||
<p>Return the result table ordered by <code>employee_id</code>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>
|
||||
Employees table:
|
||||
+-------------+-----------+------------+--------+
|
||||
| employee_id | name | manager_id | salary |
|
||||
+-------------+-----------+------------+--------+
|
||||
| 3 | Mila | 9 | 60301 |
|
||||
| 12 | Antonella | null | 31000 |
|
||||
| 13 | Emery | null | 67084 |
|
||||
| 1 | Kalel | 11 | 21241 |
|
||||
| 9 | Mikaela | null | 50937 |
|
||||
| 11 | Joziah | 6 | 28485 |
|
||||
+-------------+-----------+------------+--------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+
|
||||
| employee_id |
|
||||
+-------------+
|
||||
| 11 |
|
||||
+-------------+
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah).
|
||||
Kalel's manager is employee 11, who is still in the company (Joziah).
|
||||
Joziah's manager is employee 6, who left the company because there is no row for employee 6 as it was deleted.
|
||||
</pre>
|
@@ -0,0 +1,67 @@
|
||||
<p>Table: <code>Customer</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| customer_id | int |
|
||||
| product_key | int |
|
||||
+-------------+---------+
|
||||
This table may contain duplicates rows.
|
||||
<code>customer_id</code> is not NULL<code>.</code>
|
||||
product_key is a foreign key (reference column) to <code>Product</code> table.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Product</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| product_key | int |
|
||||
+-------------+---------+
|
||||
product_key is the primary key (column with unique values) for this table.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to report the customer ids from the <code>Customer</code> table that bought all the products in the <code>Product</code> table.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Customer table:
|
||||
+-------------+-------------+
|
||||
| customer_id | product_key |
|
||||
+-------------+-------------+
|
||||
| 1 | 5 |
|
||||
| 2 | 6 |
|
||||
| 3 | 5 |
|
||||
| 3 | 6 |
|
||||
| 1 | 6 |
|
||||
+-------------+-------------+
|
||||
Product table:
|
||||
+-------------+
|
||||
| product_key |
|
||||
+-------------+
|
||||
| 5 |
|
||||
| 6 |
|
||||
+-------------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+
|
||||
| customer_id |
|
||||
+-------------+
|
||||
| 1 |
|
||||
| 3 |
|
||||
+-------------+
|
||||
<strong>Explanation:</strong>
|
||||
The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.
|
||||
</pre>
|
@@ -0,0 +1,75 @@
|
||||
<p>Table: <code>Sales</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+-------+
|
||||
| Column Name | Type |
|
||||
+-------------+-------+
|
||||
| sale_id | int |
|
||||
| product_id | int |
|
||||
| year | int |
|
||||
| quantity | int |
|
||||
| price | int |
|
||||
+-------------+-------+
|
||||
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
|
||||
product_id is a foreign key (reference column) to <code>Product</code> table.
|
||||
Each row of this table shows a sale on the product product_id in a certain year.
|
||||
Note that the price is per unit.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Product</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
+--------------+---------+
|
||||
product_id is the primary key (column with unique values) of this table.
|
||||
Each row of this table indicates the product name of each product.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to report the <code>product_name</code>, <code>year</code>, and <code>price</code> for each <code>sale_id</code> in the <code>Sales</code> table.</p>
|
||||
|
||||
<p>Return the resulting table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Sales table:
|
||||
+---------+------------+------+----------+-------+
|
||||
| sale_id | product_id | year | quantity | price |
|
||||
+---------+------------+------+----------+-------+
|
||||
| 1 | 100 | 2008 | 10 | 5000 |
|
||||
| 2 | 100 | 2009 | 12 | 5000 |
|
||||
| 7 | 200 | 2011 | 15 | 9000 |
|
||||
+---------+------------+------+----------+-------+
|
||||
Product table:
|
||||
+------------+--------------+
|
||||
| product_id | product_name |
|
||||
+------------+--------------+
|
||||
| 100 | Nokia |
|
||||
| 200 | Apple |
|
||||
| 300 | Samsung |
|
||||
+------------+--------------+
|
||||
<strong>Output:</strong>
|
||||
+--------------+-------+-------+
|
||||
| product_name | year | price |
|
||||
+--------------+-------+-------+
|
||||
| Nokia | 2008 | 5000 |
|
||||
| Nokia | 2009 | 5000 |
|
||||
| Apple | 2011 | 9000 |
|
||||
+--------------+-------+-------+
|
||||
<strong>Explanation:</strong>
|
||||
From sale_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008.
|
||||
From sale_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009.
|
||||
From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
|
||||
</pre>
|
@@ -0,0 +1,70 @@
|
||||
<p>Table: <code>Sales</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+-------+
|
||||
| Column Name | Type |
|
||||
+-------------+-------+
|
||||
| sale_id | int |
|
||||
| product_id | int |
|
||||
| year | int |
|
||||
| quantity | int |
|
||||
| price | int |
|
||||
+-------------+-------+
|
||||
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
|
||||
product_id is a foreign key (reference column) to <code>Product</code> table.
|
||||
Each row of this table shows a sale on the product product_id in a certain year.
|
||||
Note that the price is per unit.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Product</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
+--------------+---------+
|
||||
product_id is the primary key (column with unique values) of this table.
|
||||
Each row of this table indicates the product name of each product.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to select the <strong>product id</strong>, <strong>year</strong>, <strong>quantity</strong>, and <strong>price</strong> for the <strong>first year</strong> of every product sold.</p>
|
||||
|
||||
<p>Return the resulting table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Sales table:
|
||||
+---------+------------+------+----------+-------+
|
||||
| sale_id | product_id | year | quantity | price |
|
||||
+---------+------------+------+----------+-------+
|
||||
| 1 | 100 | 2008 | 10 | 5000 |
|
||||
| 2 | 100 | 2009 | 12 | 5000 |
|
||||
| 7 | 200 | 2011 | 15 | 9000 |
|
||||
+---------+------------+------+----------+-------+
|
||||
Product table:
|
||||
+------------+--------------+
|
||||
| product_id | product_name |
|
||||
+------------+--------------+
|
||||
| 100 | Nokia |
|
||||
| 200 | Apple |
|
||||
| 300 | Samsung |
|
||||
+------------+--------------+
|
||||
<strong>Output:</strong>
|
||||
+------------+------------+----------+-------+
|
||||
| product_id | first_year | quantity | price |
|
||||
+------------+------------+----------+-------+
|
||||
| 100 | 2008 | 10 | 5000 |
|
||||
| 200 | 2011 | 15 | 9000 |
|
||||
+------------+------------+----------+-------+
|
||||
</pre>
|
@@ -0,0 +1,30 @@
|
||||
<p>Write a solution to <strong>create</strong> a DataFrame from a 2D list called <code>student_data</code>. This 2D list contains the IDs and ages of some students.</p>
|
||||
|
||||
<p>The DataFrame should have two columns, <code>student_id</code> and <code>age</code>, and be in the same order as the original 2D list.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:
|
||||
</strong>student_data:<strong>
|
||||
</strong><code>[
|
||||
[1, 15],
|
||||
[2, 11],
|
||||
[3, 11],
|
||||
[4, 20]
|
||||
]</code>
|
||||
<strong>Output:</strong>
|
||||
+------------+-----+
|
||||
| student_id | age |
|
||||
+------------+-----+
|
||||
| 1 | 15 |
|
||||
| 2 | 11 |
|
||||
| 3 | 11 |
|
||||
| 4 | 20 |
|
||||
+------------+-----+
|
||||
<strong>Explanation:</strong>
|
||||
A DataFrame was created on top of student_data, with two columns named <code>student_id</code> and <code>age</code>.
|
||||
</pre>
|
@@ -0,0 +1,75 @@
|
||||
<p>Table: <code>Employees</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| name | varchar |
|
||||
+---------------+---------+
|
||||
id is the primary key (column with unique values) for this table.
|
||||
Each row of this table contains the id and the name of an employee in a company.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>EmployeeUNI</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| unique_id | int |
|
||||
+---------------+---------+
|
||||
(id, unique_id) is the primary key (combination of columns with unique values) for this table.
|
||||
Each row of this table contains the id and the corresponding unique id of an employee in the company.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to show the <strong>unique ID </strong>of each user, If a user does not have a unique ID replace just show <code>null</code>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any</strong> order.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Employees table:
|
||||
+----+----------+
|
||||
| id | name |
|
||||
+----+----------+
|
||||
| 1 | Alice |
|
||||
| 7 | Bob |
|
||||
| 11 | Meir |
|
||||
| 90 | Winston |
|
||||
| 3 | Jonathan |
|
||||
+----+----------+
|
||||
EmployeeUNI table:
|
||||
+----+-----------+
|
||||
| id | unique_id |
|
||||
+----+-----------+
|
||||
| 3 | 1 |
|
||||
| 11 | 2 |
|
||||
| 90 | 3 |
|
||||
+----+-----------+
|
||||
<strong>Output:</strong>
|
||||
+-----------+----------+
|
||||
| unique_id | name |
|
||||
+-----------+----------+
|
||||
| null | Alice |
|
||||
| null | Bob |
|
||||
| 2 | Meir |
|
||||
| 3 | Winston |
|
||||
| 1 | Jonathan |
|
||||
+-----------+----------+
|
||||
<strong>Explanation:</strong>
|
||||
Alice and Bob do not have a unique ID, We will show null instead.
|
||||
The unique ID of Meir is 2.
|
||||
The unique ID of Winston is 3.
|
||||
The unique ID of Jonathan is 1.
|
||||
</pre>
|
@@ -0,0 +1,41 @@
|
||||
<pre>
|
||||
DataFrame <code>employees</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| salary | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>A company intends to give its employees a pay rise.</p>
|
||||
|
||||
<p>Write a solution to <strong>modify</strong> the <code>salary</code> column by multiplying each salary by 2.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:
|
||||
</strong>DataFrame employees
|
||||
+---------+--------+
|
||||
| name | salary |
|
||||
+---------+--------+
|
||||
| Jack | 19666 |
|
||||
| Piper | 74754 |
|
||||
| Mia | 62509 |
|
||||
| Ulysses | 54866 |
|
||||
+---------+--------+
|
||||
<strong>Output:
|
||||
</strong>+---------+--------+
|
||||
| name | salary |
|
||||
+---------+--------+
|
||||
| Jack | 39332 |
|
||||
| Piper | 149508 |
|
||||
| Mia | 125018 |
|
||||
| Ulysses | 109732 |
|
||||
+---------+--------+
|
||||
<strong>Explanation:
|
||||
</strong>Every salary has been doubled.</pre>
|
@@ -0,0 +1,85 @@
|
||||
<p>Table: <code>Products</code></p>
|
||||
|
||||
<pre>
|
||||
+------------------+---------+
|
||||
| Column Name | Type |
|
||||
+------------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
| product_category | varchar |
|
||||
+------------------+---------+
|
||||
product_id is the primary key (column with unique values) for this table.
|
||||
This table contains data about the company's products.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Orders</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| order_date | date |
|
||||
| unit | int |
|
||||
+---------------+---------+
|
||||
This table may have duplicate rows.
|
||||
product_id is a foreign key (reference column) to the Products table.
|
||||
unit is the number of products ordered in order_date.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to get the names of products that have at least <code>100</code> units ordered in <strong>February 2020</strong> and their amount.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Products table:
|
||||
+-------------+-----------------------+------------------+
|
||||
| product_id | product_name | product_category |
|
||||
+-------------+-----------------------+------------------+
|
||||
| 1 | Leetcode Solutions | Book |
|
||||
| 2 | Jewels of Stringology | Book |
|
||||
| 3 | HP | Laptop |
|
||||
| 4 | Lenovo | Laptop |
|
||||
| 5 | Leetcode Kit | T-shirt |
|
||||
+-------------+-----------------------+------------------+
|
||||
Orders table:
|
||||
+--------------+--------------+----------+
|
||||
| product_id | order_date | unit |
|
||||
+--------------+--------------+----------+
|
||||
| 1 | 2020-02-05 | 60 |
|
||||
| 1 | 2020-02-10 | 70 |
|
||||
| 2 | 2020-01-18 | 30 |
|
||||
| 2 | 2020-02-11 | 80 |
|
||||
| 3 | 2020-02-17 | 2 |
|
||||
| 3 | 2020-02-24 | 3 |
|
||||
| 4 | 2020-03-01 | 20 |
|
||||
| 4 | 2020-03-04 | 30 |
|
||||
| 4 | 2020-03-04 | 60 |
|
||||
| 5 | 2020-02-25 | 50 |
|
||||
| 5 | 2020-02-27 | 50 |
|
||||
| 5 | 2020-03-01 | 50 |
|
||||
+--------------+--------------+----------+
|
||||
<strong>Output:</strong>
|
||||
+--------------------+---------+
|
||||
| product_name | unit |
|
||||
+--------------------+---------+
|
||||
| Leetcode Solutions | 130 |
|
||||
| Leetcode Kit | 100 |
|
||||
+--------------------+---------+
|
||||
<strong>Explanation:</strong>
|
||||
Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
|
||||
Products with product_id = 2 is ordered in February a total of 80.
|
||||
Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
|
||||
Products with product_id = 4 was not ordered in February 2020.
|
||||
Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.
|
||||
</pre>
|
@@ -0,0 +1,45 @@
|
||||
<pre>
|
||||
DataFrame <code>employees</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type. |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| salary | int. |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>A company plans to provide its employees with a bonus.</p>
|
||||
|
||||
<p>Write a solution to create a new column name <code>bonus</code> that contains the <strong>doubled values</strong> of the <code>salary</code> column.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
DataFrame employees
|
||||
+---------+--------+
|
||||
| name | salary |
|
||||
+---------+--------+
|
||||
| Piper | 4548 |
|
||||
| Grace | 28150 |
|
||||
| Georgia | 1103 |
|
||||
| Willow | 6593 |
|
||||
| Finn | 74576 |
|
||||
| Thomas | 24433 |
|
||||
+---------+--------+
|
||||
<strong>Output:</strong>
|
||||
+---------+--------+--------+
|
||||
| name | salary | bonus |
|
||||
+---------+--------+--------+
|
||||
| Piper | 4548 | 9096 |
|
||||
| Grace | 28150 | 56300 |
|
||||
| Georgia | 1103 | 2206 |
|
||||
| Willow | 6593 | 13186 |
|
||||
| Finn | 74576 | 149152 |
|
||||
| Thomas | 24433 | 48866 |
|
||||
+---------+--------+--------+
|
||||
<strong>Explanation:</strong>
|
||||
A new column bonus is created by doubling the value in the column salary.</pre>
|
@@ -0,0 +1,40 @@
|
||||
<pre>
|
||||
DataFrame students
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| student_id | int |
|
||||
| name | object |
|
||||
| age | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>There are some rows having missing values in the <code>name</code> column.</p>
|
||||
|
||||
<p>Write a solution to remove the rows with missing values.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:
|
||||
</strong>+------------+---------+-----+
|
||||
| student_id | name | age |
|
||||
+------------+---------+-----+
|
||||
| 32 | Piper | 5 |
|
||||
| 217 | None | 19 |
|
||||
| 779 | Georgia | 20 |
|
||||
| 849 | Willow | 14 |
|
||||
+------------+---------+-----+
|
||||
<strong>Output:
|
||||
</strong>+------------+---------+-----+
|
||||
| student_id | name | age |
|
||||
+------------+---------+-----+
|
||||
| 32 | Piper | 5 |
|
||||
| 779 | Georgia | 20 |
|
||||
| 849 | Willow | 14 |
|
||||
+------------+---------+-----+
|
||||
<strong>Explanation:</strong>
|
||||
Student with id 217 havs empty value in the name column, so it will be removed.</pre>
|
@@ -0,0 +1,44 @@
|
||||
<pre>
|
||||
DataFrame customers
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| customer_id | int |
|
||||
| name | object |
|
||||
| email | object |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>There are some duplicate rows in the DataFrame based on the <code>email</code> column.</p>
|
||||
|
||||
<p>Write a solution to remove these duplicate rows and keep only the <strong>first</strong> occurrence.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<pre>
|
||||
<strong class="example">Example 1:</strong>
|
||||
<strong>Input:</strong>
|
||||
+-------------+---------+---------------------+
|
||||
| customer_id | name | email |
|
||||
+-------------+---------+---------------------+
|
||||
| 1 | Ella | emily@example.com |
|
||||
| 2 | David | michael@example.com |
|
||||
| 3 | Zachary | sarah@example.com |
|
||||
| 4 | Alice | john@example.com |
|
||||
| 5 | Finn | john@example.com |
|
||||
| 6 | Violet | alice@example.com |
|
||||
+-------------+---------+---------------------+
|
||||
<strong>Output: </strong>
|
||||
+-------------+---------+---------------------+
|
||||
| customer_id | name | email |
|
||||
+-------------+---------+---------------------+
|
||||
| 1 | Ella | emily@example.com |
|
||||
| 2 | David | michael@example.com |
|
||||
| 3 | Zachary | sarah@example.com |
|
||||
| 4 | Alice | john@example.com |
|
||||
| 6 | Violet | alice@example.com |
|
||||
+-------------+---------+---------------------+
|
||||
<strong>Explanation:</strong>
|
||||
Alic (customer_id = 4) and Finn (customer_id = 5) both use john@example.com, so only the first occurrence of this email is retained.
|
||||
</pre>
|
@@ -0,0 +1,42 @@
|
||||
<p>Table: <code>Triangle</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| x | int |
|
||||
| y | int |
|
||||
| z | int |
|
||||
+-------------+------+
|
||||
In SQL, (x, y, z) is the primary key column for this table.
|
||||
Each row of this table contains the lengths of three line segments.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Report for every three line segments whether they can form a triangle.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Triangle table:
|
||||
+----+----+----+
|
||||
| x | y | z |
|
||||
+----+----+----+
|
||||
| 13 | 15 | 30 |
|
||||
| 10 | 20 | 15 |
|
||||
+----+----+----+
|
||||
<strong>Output:</strong>
|
||||
+----+----+----+----------+
|
||||
| x | y | z | triangle |
|
||||
+----+----+----+----------+
|
||||
| 13 | 15 | 30 | No |
|
||||
| 10 | 20 | 15 | Yes |
|
||||
+----+----+----+----------+
|
||||
</pre>
|
@@ -0,0 +1,55 @@
|
||||
<p>Table: <code>Delivery</code></p>
|
||||
|
||||
<pre>
|
||||
+-----------------------------+---------+
|
||||
| Column Name | Type |
|
||||
+-----------------------------+---------+
|
||||
| delivery_id | int |
|
||||
| customer_id | int |
|
||||
| order_date | date |
|
||||
| customer_pref_delivery_date | date |
|
||||
+-----------------------------+---------+
|
||||
delivery_id is the column of unique values of this table.
|
||||
The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>If the customer's preferred delivery date is the same as the order date, then the order is called <strong>immediate;</strong> otherwise, it is called <strong>scheduled</strong>.</p>
|
||||
|
||||
<p>The <strong>first order</strong> of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has precisely one first order.</p>
|
||||
|
||||
<p>Write a solution to find the percentage of immediate orders in the first orders of all customers, <strong>rounded to 2 decimal places</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Delivery table:
|
||||
+-------------+-------------+------------+-----------------------------+
|
||||
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
|
||||
+-------------+-------------+------------+-----------------------------+
|
||||
| 1 | 1 | 2019-08-01 | 2019-08-02 |
|
||||
| 2 | 2 | 2019-08-02 | 2019-08-02 |
|
||||
| 3 | 1 | 2019-08-11 | 2019-08-12 |
|
||||
| 4 | 3 | 2019-08-24 | 2019-08-24 |
|
||||
| 5 | 3 | 2019-08-21 | 2019-08-22 |
|
||||
| 6 | 2 | 2019-08-11 | 2019-08-13 |
|
||||
| 7 | 4 | 2019-08-09 | 2019-08-09 |
|
||||
+-------------+-------------+------------+-----------------------------+
|
||||
<strong>Output:</strong>
|
||||
+----------------------+
|
||||
| immediate_percentage |
|
||||
+----------------------+
|
||||
| 50.00 |
|
||||
+----------------------+
|
||||
<strong>Explanation:</strong>
|
||||
The customer id 1 has a first order with delivery id 1 and it is scheduled.
|
||||
The customer id 2 has a first order with delivery id 2 and it is immediate.
|
||||
The customer id 3 has a first order with delivery id 5 and it is scheduled.
|
||||
The customer id 4 has a first order with delivery id 7 and it is immediate.
|
||||
Hence, half the customers have immediate first orders.
|
||||
</pre>
|
@@ -0,0 +1,72 @@
|
||||
<p>Table: <code>MyNumbers</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| num | int |
|
||||
+-------------+------+
|
||||
This table may contain duplicates (In other words, there is no primary key for this table in SQL).
|
||||
Each row of this table contains an integer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>A <strong>single number</strong> is a number that appeared only once in the <code>MyNumbers</code> table.</p>
|
||||
|
||||
<p>Find the largest <strong>single number</strong>. If there is no <strong>single number</strong>, report <code>null</code>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
<ptable> </ptable>
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
MyNumbers table:
|
||||
+-----+
|
||||
| num |
|
||||
+-----+
|
||||
| 8 |
|
||||
| 8 |
|
||||
| 3 |
|
||||
| 3 |
|
||||
| 1 |
|
||||
| 4 |
|
||||
| 5 |
|
||||
| 6 |
|
||||
+-----+
|
||||
<strong>Output:</strong>
|
||||
+-----+
|
||||
| num |
|
||||
+-----+
|
||||
| 6 |
|
||||
+-----+
|
||||
<strong>Explanation:</strong> The single numbers are 1, 4, 5, and 6.
|
||||
Since 6 is the largest single number, we return it.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
MyNumbers table:
|
||||
+-----+
|
||||
| num |
|
||||
+-----+
|
||||
| 8 |
|
||||
| 8 |
|
||||
| 7 |
|
||||
| 7 |
|
||||
| 3 |
|
||||
| 3 |
|
||||
| 3 |
|
||||
+-----+
|
||||
<strong>Output:</strong>
|
||||
+------+
|
||||
| num |
|
||||
+------+
|
||||
| null |
|
||||
+------+
|
||||
<strong>Explanation:</strong> There are no single numbers in the input table so we return null.
|
||||
</pre>
|
@@ -0,0 +1,81 @@
|
||||
<p>Table: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| user_id | int |
|
||||
| user_name | varchar |
|
||||
+-------------+---------+
|
||||
user_id is the primary key (column with unique values) for this table.
|
||||
Each row of this table contains the name and the id of a user.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Register</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| contest_id | int |
|
||||
| user_id | int |
|
||||
+-------------+---------+
|
||||
(contest_id, user_id) is the primary key (combination of columns with unique values) for this table.
|
||||
Each row of this table contains the id of a user and the contest they registered into.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the percentage of the users registered in each contest rounded to <strong>two decimals</strong>.</p>
|
||||
|
||||
<p>Return the result table ordered by <code>percentage</code> in <strong>descending order</strong>. In case of a tie, order it by <code>contest_id</code> in <strong>ascending order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Users table:
|
||||
+---------+-----------+
|
||||
| user_id | user_name |
|
||||
+---------+-----------+
|
||||
| 6 | Alice |
|
||||
| 2 | Bob |
|
||||
| 7 | Alex |
|
||||
+---------+-----------+
|
||||
Register table:
|
||||
+------------+---------+
|
||||
| contest_id | user_id |
|
||||
+------------+---------+
|
||||
| 215 | 6 |
|
||||
| 209 | 2 |
|
||||
| 208 | 2 |
|
||||
| 210 | 6 |
|
||||
| 208 | 6 |
|
||||
| 209 | 7 |
|
||||
| 209 | 6 |
|
||||
| 215 | 7 |
|
||||
| 208 | 7 |
|
||||
| 210 | 2 |
|
||||
| 207 | 2 |
|
||||
| 210 | 7 |
|
||||
+------------+---------+
|
||||
<strong>Output:</strong>
|
||||
+------------+------------+
|
||||
| contest_id | percentage |
|
||||
+------------+------------+
|
||||
| 208 | 100.0 |
|
||||
| 209 | 100.0 |
|
||||
| 210 | 100.0 |
|
||||
| 215 | 66.67 |
|
||||
| 207 | 33.33 |
|
||||
+------------+------------+
|
||||
<strong>Explanation:</strong>
|
||||
All the users registered in contests 208, 209, and 210. The percentage is 100% and we sort them in the answer table by contest_id in ascending order.
|
||||
Alice and Alex registered in contest 215 and the percentage is ((2/3) * 100) = 66.67%
|
||||
Bob registered in contest 207 and the percentage is ((1/3) * 100) = 33.33%
|
||||
</pre>
|
@@ -0,0 +1,69 @@
|
||||
<p>Table: <code>Employee</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| empId | int |
|
||||
| name | varchar |
|
||||
| supervisor | int |
|
||||
| salary | int |
|
||||
+-------------+---------+
|
||||
empId is the column with unique values for this table.
|
||||
Each row of this table indicates the name and the ID of an employee in addition to their salary and the id of their manager.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Bonus</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| empId | int |
|
||||
| bonus | int |
|
||||
+-------------+------+
|
||||
empId is the column of unique values for this table.
|
||||
empId is a foreign key (reference column) to empId from the Employee table.
|
||||
Each row of this table contains the id of an employee and their respective bonus.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to report the name and bonus amount of each employee with a bonus <strong>less than</strong> <code>1000</code>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Employee table:
|
||||
+-------+--------+------------+--------+
|
||||
| empId | name | supervisor | salary |
|
||||
+-------+--------+------------+--------+
|
||||
| 3 | Brad | null | 4000 |
|
||||
| 1 | John | 3 | 1000 |
|
||||
| 2 | Dan | 3 | 2000 |
|
||||
| 4 | Thomas | 3 | 4000 |
|
||||
+-------+--------+------------+--------+
|
||||
Bonus table:
|
||||
+-------+-------+
|
||||
| empId | bonus |
|
||||
+-------+-------+
|
||||
| 2 | 500 |
|
||||
| 4 | 2000 |
|
||||
+-------+-------+
|
||||
<strong>Output:</strong>
|
||||
+------+-------+
|
||||
| name | bonus |
|
||||
+------+-------+
|
||||
| Brad | null |
|
||||
| John | null |
|
||||
| Dan | 500 |
|
||||
+------+-------+
|
||||
</pre>
|
@@ -0,0 +1,58 @@
|
||||
<p>Table: <code>Employee</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| employee_id | int |
|
||||
| department_id | int |
|
||||
| primary_flag | varchar |
|
||||
+---------------+---------+
|
||||
(employee_id, department_id) is the primary key (combination of columns with unique values) for this table.
|
||||
employee_id is the id of the employee.
|
||||
department_id is the id of the department to which the employee belongs.
|
||||
primary_flag is an ENUM (category) of type ('Y', 'N'). If the flag is 'Y', the department is the primary department for the employee. If the flag is 'N', the department is not the primary.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Employees can belong to multiple departments. When the employee joins other departments, they need to decide which department is their primary department. Note that when an employee belongs to only one department, their primary column is <code>'N'</code>.</p>
|
||||
|
||||
<p>Write a solution to report all the employees with their primary department. For employees who belong to one department, report their only department.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Employee table:
|
||||
+-------------+---------------+--------------+
|
||||
| employee_id | department_id | primary_flag |
|
||||
+-------------+---------------+--------------+
|
||||
| 1 | 1 | N |
|
||||
| 2 | 1 | Y |
|
||||
| 2 | 2 | N |
|
||||
| 3 | 3 | N |
|
||||
| 4 | 2 | N |
|
||||
| 4 | 3 | Y |
|
||||
| 4 | 4 | N |
|
||||
+-------------+---------------+--------------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+---------------+
|
||||
| employee_id | department_id |
|
||||
+-------------+---------------+
|
||||
| 1 | 1 |
|
||||
| 2 | 1 |
|
||||
| 3 | 3 |
|
||||
| 4 | 3 |
|
||||
+-------------+---------------+
|
||||
<strong>Explanation:</strong>
|
||||
- The Primary department for employee 1 is 1.
|
||||
- The Primary department for employee 2 is 1.
|
||||
- The Primary department for employee 3 is 3.
|
||||
- The Primary department for employee 4 is 3.
|
||||
</pre>
|
@@ -0,0 +1,37 @@
|
||||
<pre>
|
||||
DataFrame <code>products</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| quantity | int |
|
||||
| price | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to fill in the missing value as <code><strong>0</strong></code> in the <code>quantity</code> column.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<pre>
|
||||
<strong class="example">Example 1:</strong>
|
||||
<strong>Input:</strong>+-----------------+----------+-------+
|
||||
| name | quantity | price |
|
||||
+-----------------+----------+-------+
|
||||
| Wristwatch | None | 135 |
|
||||
| WirelessEarbuds | None | 821 |
|
||||
| GolfClubs | 779 | 9319 |
|
||||
| Printer | 849 | 3051 |
|
||||
+-----------------+----------+-------+
|
||||
<strong>Output:
|
||||
</strong>+-----------------+----------+-------+
|
||||
| name | quantity | price |
|
||||
+-----------------+----------+-------+
|
||||
| Wristwatch | 0 | 135 |
|
||||
| WirelessEarbuds | 0 | 821 |
|
||||
| GolfClubs | 779 | 9319 |
|
||||
| Printer | 849 | 3051 |
|
||||
+-----------------+----------+-------+
|
||||
<strong>Explanation:</strong>
|
||||
The quantity for Wristwatch and WirelessEarbuds are filled by 0.</pre>
|
@@ -0,0 +1,48 @@
|
||||
<p>Table: <code>RequestAccepted</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| requester_id | int |
|
||||
| accepter_id | int |
|
||||
| accept_date | date |
|
||||
+----------------+---------+
|
||||
(requester_id, accepter_id) is the primary key (combination of columns with unique values) for this table.
|
||||
This table contains the ID of the user who sent the request, the ID of the user who received the request, and the date when the request was accepted.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the people who have the most friends and the most friends number.</p>
|
||||
|
||||
<p>The test cases are generated so that only one person has the most friends.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
RequestAccepted table:
|
||||
+--------------+-------------+-------------+
|
||||
| requester_id | accepter_id | accept_date |
|
||||
+--------------+-------------+-------------+
|
||||
| 1 | 2 | 2016/06/03 |
|
||||
| 1 | 3 | 2016/06/08 |
|
||||
| 2 | 3 | 2016/06/08 |
|
||||
| 3 | 4 | 2016/06/09 |
|
||||
+--------------+-------------+-------------+
|
||||
<strong>Output:</strong>
|
||||
+----+-----+
|
||||
| id | num |
|
||||
+----+-----+
|
||||
| 3 | 3 |
|
||||
+----+-----+
|
||||
<strong>Explanation:</strong>
|
||||
The person with id 3 is a friend of people 1, 2, and 4, so he has three friends in total, which is the most number than any others.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> In the real world, multiple people could have the same most number of friends. Could you find all these people in this case?</p>
|
@@ -0,0 +1,113 @@
|
||||
<p>Table: <code>Students</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| student_id | int |
|
||||
| student_name | varchar |
|
||||
+---------------+---------+
|
||||
student_id is the primary key (column with unique values) for this table.
|
||||
Each row of this table contains the ID and the name of one student in the school.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Subjects</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| subject_name | varchar |
|
||||
+--------------+---------+
|
||||
subject_name is the primary key (column with unique values) for this table.
|
||||
Each row of this table contains the name of one subject in the school.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Examinations</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| student_id | int |
|
||||
| subject_name | varchar |
|
||||
+--------------+---------+
|
||||
There is no primary key (column with unique values) for this table. It may contain duplicates.
|
||||
Each student from the Students table takes every course from the Subjects table.
|
||||
Each row of this table indicates that a student with ID student_id attended the exam of subject_name.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the number of times each student attended each exam.</p>
|
||||
|
||||
<p>Return the result table ordered by <code>student_id</code> and <code>subject_name</code>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Students table:
|
||||
+------------+--------------+
|
||||
| student_id | student_name |
|
||||
+------------+--------------+
|
||||
| 1 | Alice |
|
||||
| 2 | Bob |
|
||||
| 13 | John |
|
||||
| 6 | Alex |
|
||||
+------------+--------------+
|
||||
Subjects table:
|
||||
+--------------+
|
||||
| subject_name |
|
||||
+--------------+
|
||||
| Math |
|
||||
| Physics |
|
||||
| Programming |
|
||||
+--------------+
|
||||
Examinations table:
|
||||
+------------+--------------+
|
||||
| student_id | subject_name |
|
||||
+------------+--------------+
|
||||
| 1 | Math |
|
||||
| 1 | Physics |
|
||||
| 1 | Programming |
|
||||
| 2 | Programming |
|
||||
| 1 | Physics |
|
||||
| 1 | Math |
|
||||
| 13 | Math |
|
||||
| 13 | Programming |
|
||||
| 13 | Physics |
|
||||
| 2 | Math |
|
||||
| 1 | Math |
|
||||
+------------+--------------+
|
||||
<strong>Output:</strong>
|
||||
+------------+--------------+--------------+----------------+
|
||||
| student_id | student_name | subject_name | attended_exams |
|
||||
+------------+--------------+--------------+----------------+
|
||||
| 1 | Alice | Math | 3 |
|
||||
| 1 | Alice | Physics | 2 |
|
||||
| 1 | Alice | Programming | 1 |
|
||||
| 2 | Bob | Math | 1 |
|
||||
| 2 | Bob | Physics | 0 |
|
||||
| 2 | Bob | Programming | 1 |
|
||||
| 6 | Alex | Math | 0 |
|
||||
| 6 | Alex | Physics | 0 |
|
||||
| 6 | Alex | Programming | 0 |
|
||||
| 13 | John | Math | 1 |
|
||||
| 13 | John | Physics | 1 |
|
||||
| 13 | John | Programming | 1 |
|
||||
+------------+--------------+--------------+----------------+
|
||||
<strong>Explanation:</strong>
|
||||
The result table should contain all students and all subjects.
|
||||
Alice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time.
|
||||
Bob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics exam.
|
||||
Alex did not attend any exams.
|
||||
John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.
|
||||
</pre>
|
@@ -0,0 +1,75 @@
|
||||
<p>Table: <code>Prices</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| start_date | date |
|
||||
| end_date | date |
|
||||
| price | int |
|
||||
+---------------+---------+
|
||||
(product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table.
|
||||
Each row of this table indicates the price of the product_id in the period from start_date to end_date.
|
||||
For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>UnitsSold</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| purchase_date | date |
|
||||
| units | int |
|
||||
+---------------+---------+
|
||||
This table may contain duplicate rows.
|
||||
Each row of this table indicates the date, units, and product_id of each product sold.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the average selling price for each product. <code>average_price</code> should be <strong>rounded to 2 decimal places</strong>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Prices table:
|
||||
+------------+------------+------------+--------+
|
||||
| product_id | start_date | end_date | price |
|
||||
+------------+------------+------------+--------+
|
||||
| 1 | 2019-02-17 | 2019-02-28 | 5 |
|
||||
| 1 | 2019-03-01 | 2019-03-22 | 20 |
|
||||
| 2 | 2019-02-01 | 2019-02-20 | 15 |
|
||||
| 2 | 2019-02-21 | 2019-03-31 | 30 |
|
||||
+------------+------------+------------+--------+
|
||||
UnitsSold table:
|
||||
+------------+---------------+-------+
|
||||
| product_id | purchase_date | units |
|
||||
+------------+---------------+-------+
|
||||
| 1 | 2019-02-25 | 100 |
|
||||
| 1 | 2019-03-01 | 15 |
|
||||
| 2 | 2019-02-10 | 200 |
|
||||
| 2 | 2019-03-22 | 30 |
|
||||
+------------+---------------+-------+
|
||||
<strong>Output:</strong>
|
||||
+------------+---------------+
|
||||
| product_id | average_price |
|
||||
+------------+---------------+
|
||||
| 1 | 6.96 |
|
||||
| 2 | 16.96 |
|
||||
+------------+---------------+
|
||||
<strong>Explanation:</strong>
|
||||
Average selling price = Total Price of Product / Number of products sold.
|
||||
Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96
|
||||
Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
1
leetcode-cn/problem (English)/打地鼠(English) [ZbAuEH].html
Normal file
1
leetcode-cn/problem (English)/打地鼠(English) [ZbAuEH].html
Normal file
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,46 @@
|
||||
<p>Table: <code>Products</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| new_price | int |
|
||||
| change_date | date |
|
||||
+---------------+---------+
|
||||
(product_id, change_date) is the primary key (combination of columns with unique values) of this table.
|
||||
Each row of this table indicates that the price of some product was changed to a new price at some date.</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the prices of all products on <code>2019-08-16</code>. Assume the price of all products before any change is <code>10</code>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Products table:
|
||||
+------------+-----------+-------------+
|
||||
| product_id | new_price | change_date |
|
||||
+------------+-----------+-------------+
|
||||
| 1 | 20 | 2019-08-14 |
|
||||
| 2 | 50 | 2019-08-14 |
|
||||
| 1 | 30 | 2019-08-15 |
|
||||
| 1 | 35 | 2019-08-16 |
|
||||
| 2 | 65 | 2019-08-17 |
|
||||
| 3 | 20 | 2019-08-18 |
|
||||
+------------+-----------+-------------+
|
||||
<strong>Output:</strong>
|
||||
+------------+-------+
|
||||
| product_id | price |
|
||||
+------------+-------+
|
||||
| 2 | 50 |
|
||||
| 1 | 35 |
|
||||
| 3 | 10 |
|
||||
+------------+-------+
|
||||
</pre>
|
@@ -0,0 +1,56 @@
|
||||
<p>Table: <code>Accounts</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| account_id | int |
|
||||
| income | int |
|
||||
+-------------+------+
|
||||
account_id is the primary key (column with unique values) for this table.
|
||||
Each row contains information about the monthly income for one bank account.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to calculate the number of bank accounts for each salary category. The salary categories are:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"Low Salary"</code>: All the salaries <strong>strictly less</strong> than <code>$20000</code>.</li>
|
||||
<li><code>"Average Salary"</code>: All the salaries in the <strong>inclusive</strong> range <code>[$20000, $50000]</code>.</li>
|
||||
<li><code>"High Salary"</code>: All the salaries <strong>strictly greater</strong> than <code>$50000</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The result table <strong>must</strong> contain all three categories. If there are no accounts in a category, return <code>0</code>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Accounts table:
|
||||
+------------+--------+
|
||||
| account_id | income |
|
||||
+------------+--------+
|
||||
| 3 | 108939 |
|
||||
| 2 | 12747 |
|
||||
| 8 | 87709 |
|
||||
| 6 | 91796 |
|
||||
+------------+--------+
|
||||
<strong>Output:</strong>
|
||||
+----------------+----------------+
|
||||
| category | accounts_count |
|
||||
+----------------+----------------+
|
||||
| Low Salary | 1 |
|
||||
| Average Salary | 0 |
|
||||
| High Salary | 3 |
|
||||
+----------------+----------------+
|
||||
<strong>Explanation:</strong>
|
||||
Low Salary: Account 2.
|
||||
Average Salary: No accounts.
|
||||
High Salary: Accounts 3, 6, and 8.
|
||||
</pre>
|
@@ -0,0 +1,56 @@
|
||||
<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>
|
||||
|
||||
<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Equivalent characters follow the usual rules of any equivalence relation:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li>
|
||||
<li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li>
|
||||
<li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>
|
||||
|
||||
<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser"
|
||||
<strong>Output:</strong> "makkek"
|
||||
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
|
||||
The characters in each group are equivalent and sorted in lexicographical order.
|
||||
So the answer is "makkek".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold"
|
||||
<strong>Output:</strong> "hdld"
|
||||
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
|
||||
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
|
||||
<strong>Output:</strong> "aauaaaaada"
|
||||
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
|
||||
<li><code>s1.length == s2.length</code></li>
|
||||
<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,38 @@
|
||||
<pre>
|
||||
DataFrame <code>students</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| student_id | int |
|
||||
| name | object |
|
||||
| age | int |
|
||||
| grade | float |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to correct the errors:</p>
|
||||
|
||||
<p>The <code>grade</code> column is stored as floats, convert it to integers.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<pre>
|
||||
<strong class="example">Example 1:</strong>
|
||||
<strong>Input:
|
||||
</strong>DataFrame students:
|
||||
+------------+------+-----+-------+
|
||||
| student_id | name | age | grade |
|
||||
+------------+------+-----+-------+
|
||||
| 1 | Ava | 6 | 73.0 |
|
||||
| 2 | Kate | 15 | 87.0 |
|
||||
+------------+------+-----+-------+
|
||||
<strong>Output:
|
||||
</strong>+------------+------+-----+-------+
|
||||
| student_id | name | age | grade |
|
||||
+------------+------+-----+-------+
|
||||
| 1 | Ava | 6 | 73 |
|
||||
| 2 | Kate | 15 | 87 |
|
||||
+------------+------+-----+-------+
|
||||
<strong>Explanation:</strong>
|
||||
The data types of the column grade is converted to int.</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,36 @@
|
||||
<pre>
|
||||
DataFrame students
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| student_id | int |
|
||||
| name | object |
|
||||
| age | int |
|
||||
+-------------+--------+
|
||||
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to select the name and age of the student with <code>student_id = 101</code>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<pre>
|
||||
<strong>Example 1:
|
||||
Input:</strong>
|
||||
+------------+---------+-----+
|
||||
| student_id | name | age |
|
||||
+------------+---------+-----+
|
||||
| 101 | Ulysses | 13 |
|
||||
| 53 | William | 10 |
|
||||
| 128 | Henry | 6 |
|
||||
| 3 | Henry | 11 |
|
||||
+------------+---------+-----+
|
||||
<strong>Output:</strong>
|
||||
+---------+-----+
|
||||
| name | age |
|
||||
+---------+-----+
|
||||
| Ulysses | 13 |
|
||||
+---------+-----+
|
||||
<strong>Explanation:
|
||||
</strong>Student Ulysses has student_id = 101, we select the name and age.</pre>
|
@@ -0,0 +1,45 @@
|
||||
<pre>
|
||||
DataFrame <code>weather</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| city | object |
|
||||
| month | object |
|
||||
| temperature | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to <strong>pivot</strong> the data so that each row represents temperatures for a specific month, and each city is a separate column.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<pre>
|
||||
<strong class="example">Example 1:</strong>
|
||||
<strong>Input:</strong>
|
||||
+--------------+----------+-------------+
|
||||
| city | month | temperature |
|
||||
+--------------+----------+-------------+
|
||||
| Jacksonville | January | 13 |
|
||||
| Jacksonville | February | 23 |
|
||||
| Jacksonville | March | 38 |
|
||||
| Jacksonville | April | 5 |
|
||||
| Jacksonville | May | 34 |
|
||||
| ElPaso | January | 20 |
|
||||
| ElPaso | February | 6 |
|
||||
| ElPaso | March | 26 |
|
||||
| ElPaso | April | 2 |
|
||||
| ElPaso | May | 43 |
|
||||
+--------------+----------+-------------+
|
||||
<strong>Output:</strong><code>
|
||||
+----------+--------+--------------+
|
||||
| month | ElPaso | Jacksonville |
|
||||
+----------+--------+--------------+
|
||||
| April | 2 | 5 |
|
||||
| February | 6 | 23 |
|
||||
| January | 20 | 13 |
|
||||
| March | 26 | 38 |
|
||||
| May | 43 | 34 |
|
||||
+----------+--------+--------------+</code>
|
||||
<strong>Explanation:
|
||||
</strong>The table is pivoted, each column represents a city, and each row represents a specific month.</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,52 @@
|
||||
<pre>
|
||||
DataFrame <code>animals</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| species | object |
|
||||
| age | int |
|
||||
| weight | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to list the names of animals that weigh <strong>strictly more than</strong> <code>100</code> kilograms.</p>
|
||||
|
||||
<p>Return the animals sorted by weight in <strong>descending order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
DataFrame animals:
|
||||
+----------+---------+-----+--------+
|
||||
| name | species | age | weight |
|
||||
+----------+---------+-----+--------+
|
||||
| Tatiana | Snake | 98 | 464 |
|
||||
| Khaled | Giraffe | 50 | 41 |
|
||||
| Alex | Leopard | 6 | 328 |
|
||||
| Jonathan | Monkey | 45 | 463 |
|
||||
| Stefan | Bear | 100 | 50 |
|
||||
| Tommy | Panda | 26 | 349 |
|
||||
+----------+---------+-----+--------+
|
||||
<strong>Output:</strong>
|
||||
+----------+
|
||||
| name |
|
||||
+----------+
|
||||
| Tatiana |
|
||||
| Jonathan |
|
||||
| Tommy |
|
||||
| Alex |
|
||||
+----------+
|
||||
<strong>Explanation:</strong>
|
||||
All animals weighing more than 100 should be included in the results table.
|
||||
Tatiana's weight is 464, Jonathan's weight is 463, Tommy's weight is 349, and Alex's weight is 328.
|
||||
The results should be sorted in descending order of weight.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p>In Pandas, <strong>method chaining</strong> enables us to perform operations on a DataFrame without breaking up each operation into a separate line or creating multiple temporary variables. </p>
|
||||
|
||||
<p>Can you complete this task in just <strong>one line </strong>of code using method chaining?</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,43 @@
|
||||
<p>Table: <code>Tweets</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| tweet_id | int |
|
||||
| content | varchar |
|
||||
+----------------+---------+
|
||||
tweet_id is the primary key (column with unique values) for this table.
|
||||
This table contains all the tweets in a social media app.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is <strong>strictly greater</strong> than <code>15</code>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Tweets table:
|
||||
+----------+----------------------------------+
|
||||
| tweet_id | content |
|
||||
+----------+----------------------------------+
|
||||
| 1 | Vote for Biden |
|
||||
| 2 | Let us make America great again! |
|
||||
+----------+----------------------------------+
|
||||
<strong>Output:</strong>
|
||||
+----------+
|
||||
| tweet_id |
|
||||
+----------+
|
||||
| 2 |
|
||||
+----------+
|
||||
<strong>Explanation:</strong>
|
||||
Tweet 1 has length = 14. It is a valid tweet.
|
||||
Tweet 2 has length = 32. It is an invalid tweet.
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,40 @@
|
||||
<pre>
|
||||
DataFrame: <code>employees</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| employee_id | int |
|
||||
| name | object |
|
||||
| department | object |
|
||||
| salary | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>Write a solution to display the <strong>first <code>3</code> </strong>rows<strong> </strong>of this DataFrame.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:
|
||||
</strong>DataFrame employees
|
||||
+-------------+-----------+-----------------------+--------+
|
||||
| employee_id | name | department | salary |
|
||||
+-------------+-----------+-----------------------+--------+
|
||||
| 3 | Bob | Operations | 48675 |
|
||||
| 90 | Alice | Sales | 11096 |
|
||||
| 9 | Tatiana | Engineering | 33805 |
|
||||
| 60 | Annabelle | InformationTechnology | 37678 |
|
||||
| 49 | Jonathan | HumanResources | 23793 |
|
||||
| 43 | Khaled | Administration | 40454 |
|
||||
+-------------+-----------+-----------------------+--------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+---------+-------------+--------+
|
||||
| employee_id | name | department | salary |
|
||||
+-------------+---------+-------------+--------+
|
||||
| 3 | Bob | Operations | 48675 |
|
||||
| 90 | Alice | Sales | 11096 |
|
||||
| 9 | Tatiana | Engineering | 33805 |
|
||||
+-------------+---------+-------------+--------+
|
||||
<strong>Explanation:</strong>
|
||||
Only the first 3 rows are displayed.</pre>
|
@@ -0,0 +1,60 @@
|
||||
<p>Table: <code>Queue</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| person_id | int |
|
||||
| person_name | varchar |
|
||||
| weight | int |
|
||||
| turn | int |
|
||||
+-------------+---------+
|
||||
person_id column contains unique values.
|
||||
This table has the information about all people waiting for a bus.
|
||||
The person_id and turn columns will contain all numbers from 1 to n, where n is the number of rows in the table.
|
||||
turn determines the order of which the people will board the bus, where turn=1 denotes the first person to board and turn=n denotes the last person to board.
|
||||
weight is the weight of the person in kilograms.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>There is a queue of people waiting to board a bus. However, the bus has a weight limit of <code>1000</code><strong> kilograms</strong>, so there may be some people who cannot board.</p>
|
||||
|
||||
<p>Write a solution to find the <code>person_name</code> of the <strong>last person</strong> that can fit on the bus without exceeding the weight limit. The test cases are generated such that the first person does not exceed the weight limit.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Queue table:
|
||||
+-----------+-------------+--------+------+
|
||||
| person_id | person_name | weight | turn |
|
||||
+-----------+-------------+--------+------+
|
||||
| 5 | Alice | 250 | 1 |
|
||||
| 4 | Bob | 175 | 5 |
|
||||
| 3 | Alex | 350 | 2 |
|
||||
| 6 | John Cena | 400 | 3 |
|
||||
| 1 | Winston | 500 | 6 |
|
||||
| 2 | Marie | 200 | 4 |
|
||||
+-----------+-------------+--------+------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+
|
||||
| person_name |
|
||||
+-------------+
|
||||
| John Cena |
|
||||
+-------------+
|
||||
<strong>Explanation:</strong> The folowing table is ordered by the turn for simplicity.
|
||||
+------+----+-----------+--------+--------------+
|
||||
| Turn | ID | Name | Weight | Total Weight |
|
||||
+------+----+-----------+--------+--------------+
|
||||
| 1 | 5 | Alice | 250 | 250 |
|
||||
| 2 | 3 | Alex | 350 | 600 |
|
||||
| 3 | 6 | John Cena | 400 | 1000 | (last person to board)
|
||||
| 4 | 2 | Marie | 200 | 1200 | (cannot board)
|
||||
| 5 | 4 | Bob | 175 | ___ |
|
||||
| 6 | 1 | Winston | 500 | ___ |
|
||||
+------+----+-----------+--------+--------------+
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,60 @@
|
||||
<p>Table: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| user_id | int |
|
||||
| name | varchar |
|
||||
| mail | varchar |
|
||||
+---------------+---------+
|
||||
user_id is the primary key (column with unique values) for this table.
|
||||
This table contains information of the users signed up in a website. Some e-mails are invalid.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find the users who have <strong>valid emails</strong>.</p>
|
||||
|
||||
<p>A valid e-mail has a prefix name and a domain where:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>The prefix name</strong> is a string that may contain letters (upper or lower case), digits, underscore <code>'_'</code>, period <code>'.'</code>, and/or dash <code>'-'</code>. The prefix name <strong>must</strong> start with a letter.</li>
|
||||
<li><strong>The domain</strong> is <code>'@leetcode.com'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Users table:
|
||||
+---------+-----------+-------------------------+
|
||||
| user_id | name | mail |
|
||||
+---------+-----------+-------------------------+
|
||||
| 1 | Winston | winston@leetcode.com |
|
||||
| 2 | Jonathan | jonathanisgreat |
|
||||
| 3 | Annabelle | bella-@leetcode.com |
|
||||
| 4 | Sally | sally.come@leetcode.com |
|
||||
| 5 | Marwan | quarz#2020@leetcode.com |
|
||||
| 6 | David | david69@gmail.com |
|
||||
| 7 | Shapiro | .shapo@leetcode.com |
|
||||
+---------+-----------+-------------------------+
|
||||
<strong>Output:</strong>
|
||||
+---------+-----------+-------------------------+
|
||||
| user_id | name | mail |
|
||||
+---------+-----------+-------------------------+
|
||||
| 1 | Winston | winston@leetcode.com |
|
||||
| 3 | Annabelle | bella-@leetcode.com |
|
||||
| 4 | Sally | sally.come@leetcode.com |
|
||||
+---------+-----------+-------------------------+
|
||||
<strong>Explanation:</strong>
|
||||
The mail of user 2 does not have a domain.
|
||||
The mail of user 5 has the # sign which is not allowed.
|
||||
The mail of user 6 does not have the leetcode domain.
|
||||
The mail of user 7 starts with a period.
|
||||
</pre>
|
@@ -0,0 +1,69 @@
|
||||
<p>Table: <code>Queries</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| query_name | varchar |
|
||||
| result | varchar |
|
||||
| position | int |
|
||||
| rating | int |
|
||||
+-------------+---------+
|
||||
This table may have duplicate rows.
|
||||
This table contains information collected from some queries on a database.
|
||||
The <code>position</code> column has a value from <strong>1</strong> to <strong>500</strong>.
|
||||
The <code>rating</code> column has a value from <strong>1</strong> to <strong>5</strong>. Query with <code>rating</code> less than 3 is a poor query.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>We define query <code>quality</code> as:</p>
|
||||
|
||||
<blockquote>
|
||||
<p>The average of the ratio between query rating and its position.</p>
|
||||
</blockquote>
|
||||
|
||||
<p>We also define <code>poor query percentage</code> as:</p>
|
||||
|
||||
<blockquote>
|
||||
<p>The percentage of all queries with rating less than 3.</p>
|
||||
</blockquote>
|
||||
|
||||
<p>Write a solution to find each <code>query_name</code>, the <code>quality</code> and <code>poor_query_percentage</code>.</p>
|
||||
|
||||
<p>Both <code>quality</code> and <code>poor_query_percentage</code> should be <strong>rounded to 2 decimal places</strong>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Queries table:
|
||||
+------------+-------------------+----------+--------+
|
||||
| query_name | result | position | rating |
|
||||
+------------+-------------------+----------+--------+
|
||||
| Dog | Golden Retriever | 1 | 5 |
|
||||
| Dog | German Shepherd | 2 | 5 |
|
||||
| Dog | Mule | 200 | 1 |
|
||||
| Cat | Shirazi | 5 | 2 |
|
||||
| Cat | Siamese | 3 | 3 |
|
||||
| Cat | Sphynx | 7 | 4 |
|
||||
+------------+-------------------+----------+--------+
|
||||
<strong>Output:</strong>
|
||||
+------------+---------+-----------------------+
|
||||
| query_name | quality | poor_query_percentage |
|
||||
+------------+---------+-----------------------+
|
||||
| Dog | 2.50 | 33.33 |
|
||||
| Cat | 0.66 | 33.33 |
|
||||
+------------+---------+-----------------------+
|
||||
<strong>Explanation:</strong>
|
||||
Dog queries quality is ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
|
||||
Dog queries poor_ query_percentage is (1 / 3) * 100 = 33.33
|
||||
|
||||
Cat queries quality equals ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
|
||||
Cat queries poor_ query_percentage is (1 / 3) * 100 = 33.33
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,56 @@
|
||||
<p>Table: <code>Teacher</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| teacher_id | int |
|
||||
| subject_id | int |
|
||||
| dept_id | int |
|
||||
+-------------+------+
|
||||
(subject_id, dept_id) is the primary key (combinations of columns with unique values) of this table.
|
||||
Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to calculate the number of unique subjects each teacher teaches in the university.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is shown in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Teacher table:
|
||||
+------------+------------+---------+
|
||||
| teacher_id | subject_id | dept_id |
|
||||
+------------+------------+---------+
|
||||
| 1 | 2 | 3 |
|
||||
| 1 | 2 | 4 |
|
||||
| 1 | 3 | 3 |
|
||||
| 2 | 1 | 1 |
|
||||
| 2 | 2 | 1 |
|
||||
| 2 | 3 | 1 |
|
||||
| 2 | 4 | 1 |
|
||||
+------------+------------+---------+
|
||||
<strong>Output:</strong>
|
||||
+------------+-----+
|
||||
| teacher_id | cnt |
|
||||
+------------+-----+
|
||||
| 1 | 2 |
|
||||
| 2 | 4 |
|
||||
+------------+-----+
|
||||
<strong>Explanation:</strong>
|
||||
Teacher 1:
|
||||
- They teach subject 2 in departments 3 and 4.
|
||||
- They teach subject 3 in department 3.
|
||||
Teacher 2:
|
||||
- They teach subject 1 in department 1.
|
||||
- They teach subject 2 in department 1.
|
||||
- They teach subject 3 in department 1.
|
||||
- They teach subject 4 in department 1.
|
||||
</pre>
|
@@ -0,0 +1,47 @@
|
||||
<p>Table: <code>Employees</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+----------+
|
||||
| Column Name | Type |
|
||||
+-------------+----------+
|
||||
| employee_id | int |
|
||||
| name | varchar |
|
||||
| reports_to | int |
|
||||
| age | int |
|
||||
+-------------+----------+
|
||||
employee_id is the column with unique values for this table.
|
||||
This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>For this problem, we will consider a <strong>manager</strong> an employee who has at least 1 other employee reporting to them.</p>
|
||||
|
||||
<p>Write a solution to report the ids and the names of all <strong>managers</strong>, the number of employees who report <strong>directly</strong> to them, and the average age of the reports rounded to the nearest integer.</p>
|
||||
|
||||
<p>Return the result table ordered by <code>employee_id</code>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Employees table:
|
||||
+-------------+---------+------------+-----+
|
||||
| employee_id | name | reports_to | age |
|
||||
+-------------+---------+------------+-----+
|
||||
| 9 | Hercy | null | 43 |
|
||||
| 6 | Alice | 9 | 41 |
|
||||
| 4 | Bob | 9 | 36 |
|
||||
| 2 | Winston | null | 37 |
|
||||
+-------------+---------+------------+-----+
|
||||
<strong>Output:</strong>
|
||||
+-------------+-------+---------------+-------------+
|
||||
| employee_id | name | reports_count | average_age |
|
||||
+-------------+-------+---------------+-------------+
|
||||
| 9 | Hercy | 2 | 39 |
|
||||
+-------------+-------+---------------+-------------+
|
||||
<strong>Explanation:</strong> Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.
|
||||
</pre>
|
@@ -0,0 +1,68 @@
|
||||
<p>Table: <code>Activity</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| machine_id | int |
|
||||
| process_id | int |
|
||||
| activity_type | enum |
|
||||
| timestamp | float |
|
||||
+----------------+---------+
|
||||
The table shows the user activities for a factory website.
|
||||
(machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.
|
||||
machine_id is the ID of a machine.
|
||||
process_id is the ID of a process running on the machine with ID machine_id.
|
||||
activity_type is an ENUM (category) of type ('start', 'end').
|
||||
timestamp is a float representing the current time in seconds.
|
||||
'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp.
|
||||
The 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair.</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>There is a factory website that has several machines each running the <strong>same number of processes</strong>. Write a solution to find the <strong>average time</strong> each machine takes to complete a process.</p>
|
||||
|
||||
<p>The time to complete a process is the <code>'end' timestamp</code> minus the <code>'start' timestamp</code>. The average time is calculated by the total time to complete every process on the machine divided by the number of processes that were run.</p>
|
||||
|
||||
<p>The resulting table should have the <code>machine_id</code> along with the <strong>average time</strong> as <code>processing_time</code>, which should be <strong>rounded to 3 decimal places</strong>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Activity table:
|
||||
+------------+------------+---------------+-----------+
|
||||
| machine_id | process_id | activity_type | timestamp |
|
||||
+------------+------------+---------------+-----------+
|
||||
| 0 | 0 | start | 0.712 |
|
||||
| 0 | 0 | end | 1.520 |
|
||||
| 0 | 1 | start | 3.140 |
|
||||
| 0 | 1 | end | 4.120 |
|
||||
| 1 | 0 | start | 0.550 |
|
||||
| 1 | 0 | end | 1.550 |
|
||||
| 1 | 1 | start | 0.430 |
|
||||
| 1 | 1 | end | 1.420 |
|
||||
| 2 | 0 | start | 4.100 |
|
||||
| 2 | 0 | end | 4.512 |
|
||||
| 2 | 1 | start | 2.500 |
|
||||
| 2 | 1 | end | 5.000 |
|
||||
+------------+------------+---------------+-----------+
|
||||
<strong>Output:</strong>
|
||||
+------------+-----------------+
|
||||
| machine_id | processing_time |
|
||||
+------------+-----------------+
|
||||
| 0 | 0.894 |
|
||||
| 1 | 0.995 |
|
||||
| 2 | 1.456 |
|
||||
+------------+-----------------+
|
||||
<strong>Explanation:</strong>
|
||||
There are 3 machines running 2 processes each.
|
||||
Machine 0's average time is ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894
|
||||
Machine 1's average time is ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995
|
||||
Machine 2's average time is ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456
|
||||
</pre>
|
@@ -0,0 +1,48 @@
|
||||
<p>Table: <code>Transactions</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| country | varchar |
|
||||
| state | enum |
|
||||
| amount | int |
|
||||
| trans_date | date |
|
||||
+---------------+---------+
|
||||
id is the primary key of this table.
|
||||
The table has information about incoming transactions.
|
||||
The state column is an enum of type ["approved", "declined"].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The query result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Transactions table:
|
||||
+------+---------+----------+--------+------------+
|
||||
| id | country | state | amount | trans_date |
|
||||
+------+---------+----------+--------+------------+
|
||||
| 121 | US | approved | 1000 | 2018-12-18 |
|
||||
| 122 | US | declined | 2000 | 2018-12-19 |
|
||||
| 123 | US | approved | 2000 | 2019-01-01 |
|
||||
| 124 | DE | approved | 2000 | 2019-01-07 |
|
||||
+------+---------+----------+--------+------------+
|
||||
<strong>Output:</strong>
|
||||
+----------+---------+-------------+----------------+--------------------+-----------------------+
|
||||
| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
|
||||
+----------+---------+-------------+----------------+--------------------+-----------------------+
|
||||
| 2018-12 | US | 2 | 1 | 3000 | 1000 |
|
||||
| 2019-01 | US | 1 | 1 | 2000 | 2000 |
|
||||
| 2019-01 | DE | 1 | 1 | 2000 | 2000 |
|
||||
+----------+---------+-------------+----------------+--------------------+-----------------------+
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,46 @@
|
||||
<p>Table: <code>Activity</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| player_id | int |
|
||||
| device_id | int |
|
||||
| event_date | date |
|
||||
| games_played | int |
|
||||
+--------------+---------+
|
||||
(player_id, event_date) is the primary key (combination of columns with unique values) of this table.
|
||||
This table shows the activity of players of some games.
|
||||
Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to report the <strong>fraction</strong> of players that logged in again on the day after the day they first logged in, <strong>rounded to 2 decimal places</strong>. In other words, you need to count the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Activity table:
|
||||
+-----------+-----------+------------+--------------+
|
||||
| player_id | device_id | event_date | games_played |
|
||||
+-----------+-----------+------------+--------------+
|
||||
| 1 | 2 | 2016-03-01 | 5 |
|
||||
| 1 | 2 | 2016-03-02 | 6 |
|
||||
| 2 | 3 | 2017-06-25 | 1 |
|
||||
| 3 | 1 | 2016-03-02 | 0 |
|
||||
| 3 | 4 | 2018-07-03 | 5 |
|
||||
+-----------+-----------+------------+--------------+
|
||||
<strong>Output:</strong>
|
||||
+-----------+
|
||||
| fraction |
|
||||
+-----------+
|
||||
| 0.33 |
|
||||
+-----------+
|
||||
<strong>Explanation:</strong>
|
||||
Only the player with id 1 logged back in after the first day he had logged in so the answer is 1/3 = 0.33
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
103
leetcode-cn/problem (English)/电影评分(English) [movie-rating].html
Normal file
103
leetcode-cn/problem (English)/电影评分(English) [movie-rating].html
Normal file
@@ -0,0 +1,103 @@
|
||||
<p>Table: <code>Movies</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| movie_id | int |
|
||||
| title | varchar |
|
||||
+---------------+---------+
|
||||
movie_id is the primary key (column with unique values) for this table.
|
||||
title is the name of the movie.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| user_id | int |
|
||||
| name | varchar |
|
||||
+---------------+---------+
|
||||
user_id is the primary key (column with unique values) for this table.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>MovieRating</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| movie_id | int |
|
||||
| user_id | int |
|
||||
| rating | int |
|
||||
| created_at | date |
|
||||
+---------------+---------+
|
||||
(movie_id, user_id) is the primary key (column with unique values) for this table.
|
||||
This table contains the rating of a movie by a user in their review.
|
||||
created_at is the user's review date.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.</li>
|
||||
<li>Find the movie name with the <strong>highest average</strong> rating in <code>February 2020</code>. In case of a tie, return the lexicographically smaller movie name.</li>
|
||||
</ul>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Movies table:
|
||||
+-------------+--------------+
|
||||
| movie_id | title |
|
||||
+-------------+--------------+
|
||||
| 1 | Avengers |
|
||||
| 2 | Frozen 2 |
|
||||
| 3 | Joker |
|
||||
+-------------+--------------+
|
||||
Users table:
|
||||
+-------------+--------------+
|
||||
| user_id | name |
|
||||
+-------------+--------------+
|
||||
| 1 | Daniel |
|
||||
| 2 | Monica |
|
||||
| 3 | Maria |
|
||||
| 4 | James |
|
||||
+-------------+--------------+
|
||||
MovieRating table:
|
||||
+-------------+--------------+--------------+-------------+
|
||||
| movie_id | user_id | rating | created_at |
|
||||
+-------------+--------------+--------------+-------------+
|
||||
| 1 | 1 | 3 | 2020-01-12 |
|
||||
| 1 | 2 | 4 | 2020-02-11 |
|
||||
| 1 | 3 | 2 | 2020-02-12 |
|
||||
| 1 | 4 | 1 | 2020-01-01 |
|
||||
| 2 | 1 | 5 | 2020-02-17 |
|
||||
| 2 | 2 | 2 | 2020-02-01 |
|
||||
| 2 | 3 | 2 | 2020-03-01 |
|
||||
| 3 | 1 | 3 | 2020-02-22 |
|
||||
| 3 | 2 | 4 | 2020-02-25 |
|
||||
+-------------+--------------+--------------+-------------+
|
||||
<strong>Output:</strong>
|
||||
+--------------+
|
||||
| results |
|
||||
+--------------+
|
||||
| Daniel |
|
||||
| Frozen 2 |
|
||||
+--------------+
|
||||
<strong>Explanation:</strong>
|
||||
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically.
|
||||
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically.
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,82 @@
|
||||
<p>Table: <code>Signups</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+----------+
|
||||
| Column Name | Type |
|
||||
+----------------+----------+
|
||||
| user_id | int |
|
||||
| time_stamp | datetime |
|
||||
+----------------+----------+
|
||||
user_id is the column of unique values for this table.
|
||||
Each row contains information about the signup time for the user with ID user_id.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Confirmations</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+----------+
|
||||
| Column Name | Type |
|
||||
+----------------+----------+
|
||||
| user_id | int |
|
||||
| time_stamp | datetime |
|
||||
| action | ENUM |
|
||||
+----------------+----------+
|
||||
(user_id, time_stamp) is the primary key (combination of columns with unique values) for this table.
|
||||
user_id is a foreign key (reference column) to the Signups table.
|
||||
action is an ENUM (category) of the type ('confirmed', 'timeout')
|
||||
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed ('confirmed') or expired without confirming ('timeout').
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>The <strong>confirmation rate</strong> of a user is the number of <code>'confirmed'</code> messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is <code>0</code>. Round the confirmation rate to <strong>two decimal</strong> places.</p>
|
||||
|
||||
<p>Write a solution to find the <strong>confirmation rate</strong> of each user.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Signups table:
|
||||
+---------+---------------------+
|
||||
| user_id | time_stamp |
|
||||
+---------+---------------------+
|
||||
| 3 | 2020-03-21 10:16:13 |
|
||||
| 7 | 2020-01-04 13:57:59 |
|
||||
| 2 | 2020-07-29 23:09:44 |
|
||||
| 6 | 2020-12-09 10:39:37 |
|
||||
+---------+---------------------+
|
||||
Confirmations table:
|
||||
+---------+---------------------+-----------+
|
||||
| user_id | time_stamp | action |
|
||||
+---------+---------------------+-----------+
|
||||
| 3 | 2021-01-06 03:30:46 | timeout |
|
||||
| 3 | 2021-07-14 14:00:00 | timeout |
|
||||
| 7 | 2021-06-12 11:57:29 | confirmed |
|
||||
| 7 | 2021-06-13 12:58:28 | confirmed |
|
||||
| 7 | 2021-06-14 13:59:27 | confirmed |
|
||||
| 2 | 2021-01-22 00:00:00 | confirmed |
|
||||
| 2 | 2021-02-28 23:59:59 | timeout |
|
||||
+---------+---------------------+-----------+
|
||||
<strong>Output:</strong>
|
||||
+---------+-------------------+
|
||||
| user_id | confirmation_rate |
|
||||
+---------+-------------------+
|
||||
| 6 | 0.00 |
|
||||
| 3 | 0.00 |
|
||||
| 7 | 1.00 |
|
||||
| 2 | 0.50 |
|
||||
+---------+-------------------+
|
||||
<strong>Explanation:</strong>
|
||||
User 6 did not request any confirmation messages. The confirmation rate is 0.
|
||||
User 3 made 2 requests and both timed out. The confirmation rate is 0.
|
||||
User 7 made 3 requests and all were confirmed. The confirmation rate is 1.
|
||||
User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is 1 / 2 = 0.5.
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,43 @@
|
||||
<p>Given a string <code>s</code>, return <em>the number of <strong>homogenous</strong> substrings of </em><code>s</code><em>.</em> Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A string is <strong>homogenous</strong> if all the characters of the string are the same.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abbcccaa"
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> The homogenous substrings are listed as below:
|
||||
"a" appears 3 times.
|
||||
"aa" appears 1 time.
|
||||
"b" appears 2 times.
|
||||
"bb" appears 1 time.
|
||||
"c" appears 3 times.
|
||||
"cc" appears 2 times.
|
||||
"ccc" appears 1 time.
|
||||
3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "xy"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The homogenous substrings are "x" and "y".</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "zzzzz"
|
||||
<strong>Output:</strong> 15
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase letters.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,48 @@
|
||||
<p>Table: <code>Employee</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| id | int |
|
||||
| name | varchar |
|
||||
| department | varchar |
|
||||
| managerId | int |
|
||||
+-------------+---------+
|
||||
id is the primary key (column with unique values) for this table.
|
||||
Each row of this table indicates the name of an employee, their department, and the id of their manager.
|
||||
If managerId is null, then the employee does not have a manager.
|
||||
No employee will be the manager of themself.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find managers with at least <strong>five direct reports</strong>.</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Employee table:
|
||||
+-----+-------+------------+-----------+
|
||||
| id | name | department | managerId |
|
||||
+-----+-------+------------+-----------+
|
||||
| 101 | John | A | null |
|
||||
| 102 | Dan | A | 101 |
|
||||
| 103 | James | A | 101 |
|
||||
| 104 | Amy | A | 101 |
|
||||
| 105 | Anne | A | 101 |
|
||||
| 106 | Ron | B | 101 |
|
||||
+-----+-------+------------+-----------+
|
||||
<strong>Output:</strong>
|
||||
+------+
|
||||
| name |
|
||||
+------+
|
||||
| John |
|
||||
+------+
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user