Skip to content

Commit 35a3f11

Browse files
authored
Added tasks 1907, 1934
1 parent ed2282a commit 35a3f11

File tree

7 files changed

+311
-0
lines changed

7 files changed

+311
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18981898
| 1937 |[Maximum Number of Points with Cost](src/main/kotlin/g1901_2000/s1937_maximum_number_of_points_with_cost/Solution.kt)| Medium | Array, Dynamic_Programming | 886 | 100.00
18991899
| 1936 |[Add Minimum Number of Rungs](src/main/kotlin/g1901_2000/s1936_add_minimum_number_of_rungs/Solution.kt)| Medium | Array, Greedy | 405 | 100.00
19001900
| 1935 |[Maximum Number of Words You Can Type](src/main/kotlin/g1901_2000/s1935_maximum_number_of_words_you_can_type/Solution.kt)| Easy | String, Hash_Table | 178 | 37.50
1901+
| 1934 |[Confirmation Rate](src/main/kotlin/g1901_2000/s1934_confirmation_rate/script.sql)| Medium | Database | 1602 | 35.78
19011902
| 1932 |[Merge BSTs to Create Single BST](src/main/kotlin/g1901_2000/s1932_merge_bsts_to_create_single_bst/Solution.kt)| Hard | Hash_Table, Depth_First_Search, Tree, Binary_Search, Binary_Tree | 1146 | 100.00
19021903
| 1931 |[Painting a Grid With Three Different Colors](src/main/kotlin/g1901_2000/s1931_painting_a_grid_with_three_different_colors/Solution.kt)| Hard | Dynamic_Programming | 135 | 100.00
19031904
| 1930 |[Unique Length-3 Palindromic Subsequences](src/main/kotlin/g1901_2000/s1930_unique_length_3_palindromic_subsequences/Solution.kt)| Medium | String, Hash_Table, Prefix_Sum | 273 | 100.00
@@ -1918,6 +1919,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
19181919
| 1911 |[Maximum Alternating Subsequence Sum](src/main/kotlin/g1901_2000/s1911_maximum_alternating_subsequence_sum/Solution.kt)| Medium | Array, Dynamic_Programming | 531 | 100.00
19191920
| 1910 |[Remove All Occurrences of a Substring](src/main/kotlin/g1901_2000/s1910_remove_all_occurrences_of_a_substring/Solution.kt)| Medium | String | 177 | 100.00
19201921
| 1909 |[Remove One Element to Make the Array Strictly Increasing](src/main/kotlin/g1901_2000/s1909_remove_one_element_to_make_the_array_strictly_increasing/Solution.kt)| Easy | Array | 176 | 50.00
1922+
| 1907 |[Count Salary Categories](src/main/kotlin/g1901_2000/s1907_count_salary_categories/script.sql)| Medium | Database | 2358 | 73.89
19211923
| 1906 |[Minimum Absolute Difference Queries](src/main/kotlin/g1901_2000/s1906_minimum_absolute_difference_queries/Solution.kt)| Medium | Array, Hash_Table | 1069 | 50.00
19221924
| 1905 |[Count Sub Islands](src/main/kotlin/g1901_2000/s1905_count_sub_islands/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 866 | 100.00
19231925
| 1904 |[The Number of Full Rounds You Have Played](src/main/kotlin/g1901_2000/s1904_the_number_of_full_rounds_you_have_played/Solution.kt)| Medium | String, Math | 149 | 100.00
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
1907\. Count Salary Categories
2+
3+
Medium
4+
5+
SQL Schema
6+
7+
Table: `Accounts`
8+
9+
+-------------+------+
10+
| Column Name | Type |
11+
+-------------+------+
12+
| account_id | int |
13+
| income | int |
14+
+-------------+------+
15+
account_id is the primary key for this table.
16+
Each row contains information about the monthly income for one bank account.
17+
18+
Write an SQL query to report the number of bank accounts of each salary category. The salary categories are:
19+
20+
* `"Low Salary"`: All the salaries **strictly less** than `$20000`.
21+
* `"Average Salary"`: All the salaries in the **inclusive** range `[$20000, $50000]`.
22+
* `"High Salary"`: All the salaries **strictly greater** than `$50000`.
23+
24+
The result table **must** contain all three categories. If there are no accounts in a category, then report `0`.
25+
26+
Return the result table in **any order**.
27+
28+
The query result format is in the following example.
29+
30+
**Example 1:**
31+
32+
**Input:**
33+
34+
Accounts table:
35+
+------------+--------+
36+
| account_id | income |
37+
+------------+--------+
38+
| 3 | 108939 |
39+
| 2 | 12747 |
40+
| 8 | 87709 |
41+
| 6 | 91796 |
42+
+------------+--------+
43+
44+
**Output:**
45+
46+
+----------------+----------------+
47+
| category | accounts_count |
48+
+----------------+----------------+
49+
| Low Salary | 1 |
50+
| Average Salary | 0 |
51+
| High Salary | 3 |
52+
+----------------+----------------+
53+
54+
**Explanation:**
55+
56+
Low Salary: Account 2.
57+
58+
Average Salary: No accounts.
59+
60+
High Salary: Accounts 3, 6, and 8.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2023_06_22_Time_2358_ms_(73.89%)_Space_0B_(100.00%)
3+
with cte1 as (SELECT 'Low Salary' AS category,SUM(CASE WHEN INCOME<20000 THEN 1 ELSE 0 END ) AS accounts_count from Accounts),
4+
cte2 as(SELECT 'Average Salary' AS category,SUM(CASE WHEN INCOME BETWEEN 20000 AND 50000 THEN 1 ELSE 0 END ) AS accounts_count from Accounts),
5+
cte3 as (SELECT 'High Salary' AS category,SUM(CASE WHEN INCOME> 50000 THEN 1 ELSE 0 END) AS accounts_count
6+
FROM Accounts)
7+
SELECT * from cte3
8+
UNION ALL
9+
SELECT * FROM cte1
10+
UNION ALL
11+
SELECT * FROM cte2
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
1934\. Confirmation Rate
2+
3+
Medium
4+
5+
SQL Schema
6+
7+
Table: `Signups`
8+
9+
+----------------+----------+
10+
| Column Name | Type |
11+
+----------------+----------+
12+
| user_id | int |
13+
| time_stamp | datetime |
14+
+----------------+----------+
15+
user_id is the primary key for this table.
16+
Each row contains information about the signup time for the user with ID user_id.
17+
18+
Table: `Confirmations`
19+
20+
+----------------+----------+
21+
| Column Name | Type |
22+
+----------------+----------+
23+
| user_id | int |
24+
| time_stamp | datetime |
25+
| action | ENUM |
26+
+----------------+----------+
27+
(user_id, time_stamp) is the primary key for this table.
28+
user_id is a foreign key with a reference to the Signups table.
29+
action is an ENUM of the type ('confirmed', 'timeout')
30+
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').
31+
32+
The **confirmation rate** of a user is the number of `'confirmed'` messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is `0`. Round the confirmation rate to **two decimal** places.
33+
34+
Write an SQL query to find the **confirmation rate** of each user.
35+
36+
Return the result table in **any order**.
37+
38+
The query result format is in the following example.
39+
40+
**Example 1:**
41+
42+
**Input:**
43+
44+
Signups table:
45+
+---------+---------------------+
46+
| user_id | time_stamp |
47+
+---------+---------------------+
48+
| 3 | 2020-03-21 10:16:13 |
49+
| 7 | 2020-01-04 13:57:59 |
50+
| 2 | 2020-07-29 23:09:44 |
51+
| 6 | 2020-12-09 10:39:37 |
52+
+---------+---------------------+
53+
54+
Confirmations table:
55+
+---------+---------------------+-----------+
56+
| user_id | time_stamp | action |
57+
+---------+---------------------+-----------+
58+
| 3 | 2021-01-06 03:30:46 | timeout |
59+
| 3 | 2021-07-14 14:00:00 | timeout |
60+
| 7 | 2021-06-12 11:57:29 | confirmed |
61+
| 7 | 2021-06-13 12:58:28 | confirmed |
62+
| 7 | 2021-06-14 13:59:27 | confirmed |
63+
| 2 | 2021-01-22 00:00:00 | confirmed |
64+
| 2 | 2021-02-28 23:59:59 | timeout |
65+
+---------+---------------------+-----------+
66+
67+
**Output:**
68+
69+
+---------+-------------------+
70+
| user_id | confirmation_rate |
71+
+---------+-------------------+
72+
| 6 | 0.00 |
73+
| 3 | 0.00 |
74+
| 7 | 1.00 |
75+
| 2 | 0.50 |
76+
+---------+-------------------+
77+
78+
**Explanation:**
79+
80+
User 6 did not request any confirmation messages. The confirmation rate is 0.
81+
82+
User 3 made 2 requests and both timed out. The confirmation rate is 0.
83+
84+
User 7 made 3 requests and all were confirmed. The confirmation rate is 1.
85+
86+
User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is 1 / 2 = 0.5.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2023_06_22_Time_1602_ms_(35.78%)_Space_0B_(100.00%)
3+
select
4+
s.user_id
5+
, round(count(case when action = 'confirmed' then 1 end)
6+
/ cast(count(*) as decimal(7,2)), 2) as confirmation_rate
7+
from
8+
Signups s
9+
left join
10+
Confirmations c
11+
on c.user_id = s.user_id
12+
group by 1
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g1901_2000.s1907_count_salary_categories
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
"CREATE TABLE Accounts(account_id INTEGER, income INTEGER); " +
20+
"INSERT INTO Accounts(account_id, income)" +
21+
" VALUES (3, 108939); " +
22+
"INSERT INTO Accounts(account_id, income)" +
23+
" VALUES (2, 12747); " +
24+
"INSERT INTO Accounts(account_id, income)" +
25+
" VALUES (8, 87709); " +
26+
"INSERT INTO Accounts(account_id, income)" +
27+
" VALUES (6, 91796); "
28+
]
29+
)
30+
internal class MysqlTest {
31+
@Test
32+
@Throws(SQLException::class, FileNotFoundException::class)
33+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
34+
dataSource.connection.use { connection ->
35+
connection.createStatement().use { statement ->
36+
statement.executeQuery(
37+
BufferedReader(
38+
FileReader(
39+
"src/main/kotlin/g1901_2000/" +
40+
"s1907_count_salary_categories/script.sql"
41+
)
42+
)
43+
.lines()
44+
.collect(Collectors.joining("\n"))
45+
.replace("#.*?\\r?\\n".toRegex(), "")
46+
).use { resultSet ->
47+
assertThat(resultSet.next(), equalTo(true))
48+
assertThat(resultSet.getNString(1), equalTo("High Salary"))
49+
assertThat(resultSet.getInt(2), equalTo(3))
50+
assertThat(resultSet.next(), equalTo(true))
51+
assertThat(resultSet.getNString(1), equalTo("Low Salary"))
52+
assertThat(resultSet.getInt(2), equalTo(1))
53+
assertThat(resultSet.next(), equalTo(true))
54+
assertThat(resultSet.getNString(1), equalTo("Average Salary"))
55+
assertThat(resultSet.getInt(2), equalTo(0))
56+
assertThat(resultSet.next(), equalTo(false))
57+
}
58+
}
59+
}
60+
}
61+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package g1901_2000.s1934_confirmation_rate
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
"CREATE TABLE Signups(user_id INTEGER, time_stamp DATETIME); " +
20+
"INSERT INTO Signups(user_id, time_stamp)" +
21+
" VALUES (3, '2020-03-21 10:16:13'); " +
22+
"INSERT INTO Signups(user_id, time_stamp)" +
23+
" VALUES (7, '2020-01-04 13:57:59'); " +
24+
"INSERT INTO Signups(user_id, time_stamp)" +
25+
" VALUES (2, '2020-07-29 23:09:44'); " +
26+
"INSERT INTO Signups(user_id, time_stamp)" +
27+
" VALUES (6, '2020-12-09 10:39:37'); " +
28+
"CREATE TABLE Confirmations(user_id INTEGER, time_stamp DATETIME, action VARCHAR); " +
29+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
30+
" VALUES (3, '2021-01-06 03:30:46', 'timeout'); " +
31+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
32+
" VALUES (3, '2021-07-14 14:00:00', 'timeout'); " +
33+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
34+
" VALUES (7, '2021-06-12 11:57:29', 'confirmed'); " +
35+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
36+
" VALUES (7, '2021-06-13 12:58:28', 'confirmed'); " +
37+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
38+
" VALUES (7, '2021-06-14 13:59:27', 'confirmed'); " +
39+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
40+
" VALUES (2, '2021-01-22 00:00:00', 'confirmed'); " +
41+
"INSERT INTO Confirmations(user_id, time_stamp, action)" +
42+
" VALUES (2, '2021-02-28 23:59:59', 'timeout'); "
43+
]
44+
)
45+
internal class MysqlTest {
46+
@Test
47+
@Throws(SQLException::class, FileNotFoundException::class)
48+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
49+
dataSource.connection.use { connection ->
50+
connection.createStatement().use { statement ->
51+
statement.executeQuery(
52+
BufferedReader(
53+
FileReader(
54+
"src/main/kotlin/g1901_2000/" +
55+
"s1934_confirmation_rate/script.sql"
56+
)
57+
)
58+
.lines()
59+
.collect(Collectors.joining("\n"))
60+
.replace("#.*?\\r?\\n".toRegex(), "")
61+
).use { resultSet ->
62+
assertThat(resultSet.next(), equalTo(true))
63+
assertThat(resultSet.getInt(1), equalTo(2))
64+
assertThat(resultSet.getDouble(2), equalTo(0.50))
65+
assertThat(resultSet.next(), equalTo(true))
66+
assertThat(resultSet.getInt(1), equalTo(3))
67+
assertThat(resultSet.getDouble(2), equalTo(0.0))
68+
assertThat(resultSet.next(), equalTo(true))
69+
assertThat(resultSet.getInt(1), equalTo(6))
70+
assertThat(resultSet.getDouble(2), equalTo(0.0))
71+
assertThat(resultSet.next(), equalTo(true))
72+
assertThat(resultSet.getInt(1), equalTo(7))
73+
assertThat(resultSet.getDouble(2), equalTo(1.0))
74+
assertThat(resultSet.next(), equalTo(false))
75+
}
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)