Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/main/java/g0201_0300/s0262_trips_and_users/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
262\. Trips and Users

Hard

SQL Schema

Table: `Trips`

+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| client_id | int |
| driver_id | int |
| city_id | int |
| status | enum |
| request_at | date |
+-------------+----------+
id is the primary key for this table.
The table holds all taxi trips. Each trip has a unique id, while client_id and driver_id are foreign keys to the users_id at the Users table.
Status is an ENUM type of ('completed', 'cancelled_by_driver', 'cancelled_by_client').

Table: `Users`

+-------------+----------+
| Column Name | Type |
+-------------+----------+
| users_id | int |
| banned | enum |
| role | enum |
+-------------+----------+
users_id is the primary key for this table.
The table holds all users. Each user has a unique users_id, and role is an ENUM type of ('client', 'driver', 'partner').
banned is an ENUM type of ('Yes', 'No').

The **cancellation rate** is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.

Write a SQL query to find the **cancellation rate** of requests with unbanned users (**both client and driver must not be banned**) each day between `"2013-10-01"` and `"2013-10-03"`. Round `Cancellation Rate` to **two decimal** points.

Return the result table in **any order**.

The query result format is in the following example.

**Example 1:**

**Input:**

Trips table:
+----+-----------+-----------+---------+---------------------+------------+
| id | client_id | driver_id | city_id | status | request_at |
+----+-----------+-----------+---------+---------------------+------------+
| 1 | 1 | 10 | 1 | completed | 2013-10-01 |
| 2 | 2 | 11 | 1 | cancelled_by_driver | 2013-10-01 |
| 3 | 3 | 12 | 6 | completed | 2013-10-01 |
| 4 | 4 | 13 | 6 | cancelled_by_client | 2013-10-01 |
| 5 | 1 | 10 | 1 | completed | 2013-10-02 |
| 6 | 2 | 11 | 6 | completed | 2013-10-02 |
| 7 | 3 | 12 | 6 | completed | 2013-10-02 |
| 8 | 2 | 12 | 12 | completed | 2013-10-03 |
| 9 | 3 | 10 | 12 | completed | 2013-10-03 |
| 10 | 4 | 13 | 12 | cancelled_by_driver | 2013-10-03 |
+----+-----------+-----------+---------+---------------------+------------+

Users table:

+----------+--------+--------+
| users_id | banned | role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+

**Output:**

+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+

**Explanation:**

On 2013-10-01:
- There were 4 requests in total, 2 of which were canceled.
- However, the request with Id=2 was made by a banned client (User_Id=2), so it is ignored in the calculation.
- Hence there are 3 unbanned requests in total, 1 of which was canceled.
- The Cancellation Rate is (1 / 3) = 0.33
On 2013-10-02:
- There were 3 requests in total, 0 of which were canceled.
- The request with Id=6 was made by a banned client, so it is ignored.
- Hence there are 2 unbanned requests in total, 0 of which were canceled.
- The Cancellation Rate is (0 / 2) = 0.00
On 2013-10-03:
- There were 3 requests in total, 1 of which was canceled.
- The request with Id=8 was made by a banned client, so it is ignored.
- Hence there are 2 unbanned request in total, 1 of which were canceled.
- The Cancellation Rate is (1 / 2) = 0.50
2 changes: 2 additions & 0 deletions src/main/java/g0201_0300/s0262_trips_and_users/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Write your MySQL query statement below
SELECT T.Request_at AS Day, ROUND(SUM(if(T.Status <> 'completed', 1,0))/COUNT(*),2) AS `Cancellation Rate` FROM Trips AS T LEFT JOIN (SELECT * FROM Users WHERE Role = 'client') AS C ON T.Client_Id = C.Users_Id LEFT JOIN (SELECT * FROM Users WHERE Role = 'driver') AS D ON T.Driver_Id = D.Users_Id WHERE C.Banned <> 'YES' AND D.Banned <> 'YES' AND T.Request_at BETWEEN '2013-10-01' AND '2013-10-03' GROUP BY T.Request_at ORDER BY Day;
18 changes: 18 additions & 0 deletions src/main/java/g0201_0300/s0263_ugly_number/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0201_0300.s0263_ugly_number;

public class Solution {
public boolean isUgly(int n) {
if (n == 1) {
return true;
} else if (n <= 0) {
return false;
}
int[] factors = new int[] {2, 3, 5};
for (int factor : factors) {
while (n > 1 && n % factor == 0) {
n /= factor;
}
}
return n == 1;
}
}
43 changes: 43 additions & 0 deletions src/main/java/g0201_0300/s0263_ugly_number/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
263\. Ugly Number

Easy

An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`.

Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_.

**Example 1:**

**Input:** n = 6

**Output:** true

**Explanation:** 6 = 2 × 3

**Example 2:**

**Input:** n = 8

**Output:** true

**Explanation:** 8 = 2 × 2 × 2

**Example 3:**

**Input:** n = 14

**Output:** false

**Explanation:** 14 is not ugly since it includes the prime factor 7.

**Example 4:**

**Input:** n = 1

**Output:** true

**Explanation:** 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.

**Constraints:**

* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code>
32 changes: 32 additions & 0 deletions src/main/java/g0201_0300/s0264_ugly_number_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g0201_0300.s0264_ugly_number_ii;

public class Solution {
public int nthUglyNumber(int n) {
int[] ugly = new int[n];
int i2 = 0;
int i3 = 0;
int i5 = 0;
int ugly2 = 2;
int ugly3 = 3;
int ugly5 = 5;
int nextugly = 1;
ugly[0] = 1;
for (int i = 1; i < n; i++) {
nextugly = Math.min(Math.min(ugly2, ugly3), ugly5);
ugly[i] = nextugly;
if (nextugly == ugly2) {
i2++;
ugly2 = ugly[i2] * 2;
}
if (nextugly == ugly3) {
i3++;
ugly3 = ugly[i3] * 3;
}
if (nextugly == ugly5) {
i5++;
ugly5 = ugly[i5] * 5;
}
}
return ugly[n - 1];
}
}
27 changes: 27 additions & 0 deletions src/main/java/g0201_0300/s0264_ugly_number_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
264\. Ugly Number II

Medium

An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`.

Given an integer `n`, return _the_ <code>n<sup>th</sup></code> _**ugly number**_.

**Example 1:**

**Input:** n = 10

**Output:** 12

**Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers.

**Example 2:**

**Input:** n = 1

**Output:** 1

**Explanation:** 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.

**Constraints:**

* `1 <= n <= 1690`
28 changes: 28 additions & 0 deletions src/test/java/g0201_0300/s0263_ugly_number/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g0201_0300.s0263_ugly_number;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void isUgly() {
assertThat(new Solution().isUgly(6), equalTo(true));
}

@Test
public void isUgly2() {
assertThat(new Solution().isUgly(8), equalTo(true));
}

@Test
public void isUgly3() {
assertThat(new Solution().isUgly(14), equalTo(false));
}

@Test
public void isUgly4() {
assertThat(new Solution().isUgly(1), equalTo(true));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0201_0300/s0264_ugly_number_ii/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0201_0300.s0264_ugly_number_ii;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

public class SolutionTest {
@Test
public void nthUglyNumber() {
assertThat(new Solution().nthUglyNumber(10), equalTo(12));
}

@Test
public void nthUglyNumber2() {
assertThat(new Solution().nthUglyNumber(1), equalTo(1));
}
}