Skip to content

Commit 8da4215

Browse files
authored
Added task 3564
1 parent 08be811 commit 8da4215

File tree

4 files changed

+245
-9
lines changed
  • src

4 files changed

+245
-9
lines changed

src/main/kotlin/g3501_3600/s3563_lexicographically_smallest_string_after_adjacent_removals/Solution.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ class Solution {
1010
return diffVal == 1 || (char1 == 'a' && char2 == 'z') || (char1 == 'z' && char2 == 'a')
1111
}
1212

13-
fun lexicographicallySmallestString(sIn: String): String? {
13+
fun lexicographicallySmallestString(sIn: String): String {
1414
val nVal = sIn.length
1515
if (nVal == 0) {
1616
return ""
1717
}
18-
val remTable = Array<BooleanArray?>(nVal) { BooleanArray(nVal) }
18+
val remTable = Array<BooleanArray>(nVal) { BooleanArray(nVal) }
1919
var len = 2
2020
while (len <= nVal) {
2121
for (idx in 0..nVal - len) {
2222
val j = idx + len - 1
2323
if (checkPair(sIn[idx], sIn[j])) {
2424
if (len == 2) {
25-
remTable[idx]!![j] = true
25+
remTable[idx][j] = true
2626
} else {
27-
if (remTable[idx + 1]!![j - 1]) {
28-
remTable[idx]!![j] = true
27+
if (remTable[idx + 1][j - 1]) {
28+
remTable[idx][j] = true
2929
}
3030
}
3131
}
32-
if (remTable[idx]!![j]) {
32+
if (remTable[idx][j]) {
3333
continue
3434
}
3535
var pSplit = idx + 1
3636
while (pSplit < j) {
37-
if (remTable[idx]!![pSplit] && remTable[pSplit + 1]!![j]) {
38-
remTable[idx]!![j] = true
37+
if (remTable[idx][pSplit] && remTable[pSplit + 1][j]) {
38+
remTable[idx][j] = true
3939
break
4040
}
4141
pSplit += 2
@@ -52,7 +52,7 @@ class Solution {
5252
val middleVanishes: Boolean = if (kMatch - 1 < idx + 1) {
5353
true
5454
} else {
55-
remTable[idx + 1]!![kMatch - 1]
55+
remTable[idx + 1][kMatch - 1]
5656
}
5757
if (middleVanishes) {
5858
val candidate = dpArr[kMatch + 1]
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
3564\. Seasonal Sales Analysis
2+
3+
Medium
4+
5+
Table: `sales`
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| sale_id | int |
11+
| product_id | int |
12+
| sale_date | date |
13+
| quantity | int |
14+
| price | decimal |
15+
+---------------+---------+
16+
sale_id is the unique identifier for this table.
17+
Each row contains information about a product sale including the product_id,
18+
date of sale, quantity sold, and price per unit.
19+
20+
Table: `products`
21+
22+
+---------------+---------+
23+
| Column Name | Type |
24+
+---------------+---------+
25+
| product_id | int |
26+
| product_name | varchar |
27+
| category | varchar |
28+
+---------------+---------+
29+
product_id is the unique identifier for this table.
30+
Each row contains information about a product including its name and category.
31+
32+
Write a solution to find the most popular product category for each season. The seasons are defined as:
33+
34+
* **Winter**: December, January, February
35+
* **Spring**: March, April, May
36+
* **Summer**: June, July, August
37+
* **Fall**: September, October, November
38+
39+
The **popularity** of a **category** is determined by the **total quantity sold** in that **season**. If there is a **tie**, select the category with the highest **total revenue** (`quantity × price`).
40+
41+
Return _the result table ordered by season in **ascending** order_.
42+
43+
The result format is in the following example.
44+
45+
**Example:**
46+
47+
**Input:**
48+
49+
sales table:
50+
51+
+---------+------------+------------+----------+-------+
52+
| sale_id | product_id | sale_date | quantity | price |
53+
+---------+------------+------------+----------+-------+
54+
| 1 | 1 | 2023-01-15 | 5 | 10.00 |
55+
| 2 | 2 | 2023-01-20 | 4 | 15.00 |
56+
| 3 | 3 | 2023-03-10 | 3 | 18.00 |
57+
| 4 | 4 | 2023-04-05 | 1 | 20.00 |
58+
| 5 | 1 | 2023-05-20 | 2 | 10.00 |
59+
| 6 | 2 | 2023-06-12 | 4 | 15.00 |
60+
| 7 | 5 | 2023-06-15 | 5 | 12.00 |
61+
| 8 | 3 | 2023-07-24 | 2 | 18.00 |
62+
| 9 | 4 | 2023-08-01 | 5 | 20.00 |
63+
| 10 | 5 | 2023-09-03 | 3 | 12.00 |
64+
| 11 | 1 | 2023-09-25 | 6 | 10.00 |
65+
| 12 | 2 | 2023-11-10 | 4 | 15.00 |
66+
| 13 | 3 | 2023-12-05 | 6 | 18.00 |
67+
| 14 | 4 | 2023-12-22 | 3 | 20.00 |
68+
| 15 | 5 | 2024-02-14 | 2 | 12.00 |
69+
+---------+------------+------------+----------+-------+
70+
71+
products table:
72+
73+
+------------+-----------------+----------+
74+
| product_id | product_name | category |
75+
+------------+-----------------+----------+
76+
| 1 | Warm Jacket | Apparel |
77+
| 2 | Designer Jeans | Apparel |
78+
| 3 | Cutting Board | Kitchen |
79+
| 4 | Smart Speaker | Tech |
80+
| 5 | Yoga Mat | Fitness |
81+
+------------+-----------------+----------+
82+
83+
**Output:**
84+
85+
+---------+----------+----------------+---------------+
86+
| season | category | total_quantity | total_revenue |
87+
+---------+----------+----------------+---------------+
88+
| Fall | Apparel | 10 | 120.00 |
89+
| Spring | Kitchen | 3 | 54.00 |
90+
| Summer | Tech | 5 | 100.00 |
91+
| Winter | Apparel | 9 | 110.00 |
92+
+---------+----------+----------------+---------------+
93+
94+
**Explanation:**
95+
96+
* **Fall (Sep, Oct, Nov):**
97+
* Apparel: 10 items sold (6 Jackets in Sep, 4 Jeans in Nov), revenue $120.00 (6×$10.00 + 4×$15.00)
98+
* Fitness: 3 Yoga Mats sold in Sep, revenue $36.00
99+
* Most popular: Apparel with highest total quantity (10)
100+
* **Spring (Mar, Apr, May):**
101+
* Kitchen: 3 Cutting Boards sold in Mar, revenue $54.00
102+
* Tech: 1 Smart Speaker sold in Apr, revenue $20.00
103+
* Apparel: 2 Warm Jackets sold in May, revenue $20.00
104+
* Most popular: Kitchen with highest total quantity (3) and highest revenue ($54.00)
105+
* **Summer (Jun, Jul, Aug):**
106+
* Apparel: 4 Designer Jeans sold in Jun, revenue $60.00
107+
* Fitness: 5 Yoga Mats sold in Jun, revenue $60.00
108+
* Kitchen: 2 Cutting Boards sold in Jul, revenue $36.00
109+
* Tech: 5 Smart Speakers sold in Aug, revenue $100.00
110+
* Most popular: Tech and Fitness both have 5 items, but Tech has higher revenue ($100.00 vs $60.00)
111+
* **Winter (Dec, Jan, Feb):**
112+
* Apparel: 9 items sold (5 Jackets in Jan, 4 Jeans in Jan), revenue $110.00
113+
* Kitchen: 6 Cutting Boards sold in Dec, revenue $108.00
114+
* Tech: 3 Smart Speakers sold in Dec, revenue $60.00
115+
* Fitness: 2 Yoga Mats sold in Feb, revenue $24.00
116+
* Most popular: Apparel with highest total quantity (9) and highest revenue ($110.00)
117+
118+
The result table is ordered by season in ascending order.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2025_05_26_Time_505_ms_(100.00%)_Space_0.0_MB_(100.00%)
3+
WITH cte AS (
4+
SELECT CASE
5+
WHEN MONTH(sale_date) IN (1, 2, 12) THEN 'Winter'
6+
WHEN MONTH(sale_date) IN (3, 4, 5) THEN 'Spring'
7+
WHEN MONTH(sale_date) IN (6, 7, 8) THEN 'Summer'
8+
WHEN MONTH(sale_date) IN (9, 10, 11) THEN 'Fall'
9+
END AS season,
10+
category, SUM(quantity) AS total_quantity, SUM(quantity * price) AS total_revenue
11+
FROM sales s
12+
JOIN products p ON s.product_id = p.product_id
13+
GROUP BY season, category
14+
),
15+
cte2 AS (
16+
SELECT season, category, total_quantity, total_revenue,
17+
RANK() OVER (PARTITION BY season ORDER BY total_quantity DESC, total_revenue DESC) AS ranking
18+
FROM cte
19+
)
20+
SELECT
21+
season, category, total_quantity, total_revenue
22+
FROM cte2
23+
WHERE ranking = 1
24+
ORDER BY season ASC;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package g3501_3600.s3564_seasonal_sales_analysis
2+
3+
import org.hamcrest.CoreMatchers
4+
import org.hamcrest.MatcherAssert
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+
(
20+
"CREATE TABLE sales(sale_id INTEGER, product_id INTEGER" +
21+
", sale_date DATE, quantity INTEGER, price DECIMAL); " +
22+
"INSERT INTO sales (sale_id, product_id, sale_date, quantity, price) VALUES" +
23+
"(1, 1, '2023-01-15', 5, 10.00)," +
24+
"(2, 2, '2023-01-20', 4, 15.00)," +
25+
"(3, 3, '2023-03-10', 3, 18.00)," +
26+
"(4, 4, '2023-04-05', 1, 20.00)," +
27+
"(5, 1, '2023-05-20', 2, 10.00)," +
28+
"(6, 2, '2023-06-12', 4, 15.00)," +
29+
"(7, 5, '2023-06-15', 5, 12.00)," +
30+
"(8, 3, '2023-07-24', 2, 18.00)," +
31+
"(9, 4, '2023-08-01', 5, 20.00)," +
32+
"(10, 5, '2023-09-03', 3, 12.00)," +
33+
"(11, 1, '2023-09-25', 6, 10.00)," +
34+
"(12, 2, '2023-11-10', 4, 15.00)," +
35+
"(13, 3, '2023-12-05', 6, 18.00)," +
36+
"(14, 4, '2023-12-22', 3, 20.00)," +
37+
"(15, 5, '2024-02-14', 2, 12.00);" +
38+
"CREATE TABLE products(product_id INTEGER, product_name VARCHAR(255)" +
39+
", category VARCHAR(255)); " +
40+
"INSERT INTO products (product_id, product_name, category) VALUES" +
41+
"(1, 'Warm Jacket', 'Apparel')," +
42+
"(2, 'Designer Jeans', 'Apparel')," +
43+
"(3, 'Cutting Board', 'Kitchen')," +
44+
"(4, 'Smart Speaker', 'Tech')," +
45+
"(5, 'Yoga Mat', 'Fitness');"
46+
),
47+
],
48+
)
49+
internal class MysqlTest {
50+
@Test
51+
@Throws(SQLException::class, FileNotFoundException::class)
52+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
53+
dataSource.connection.use { connection ->
54+
connection.createStatement().use { statement ->
55+
statement.executeQuery(
56+
BufferedReader(
57+
FileReader(
58+
(
59+
"src/main/kotlin/g3501_3600/" +
60+
"s3564_seasonal_sales_analysis/" +
61+
"script.sql"
62+
),
63+
),
64+
)
65+
.lines()
66+
.collect(Collectors.joining("\n"))
67+
.replace("#.*?\\r?\\n".toRegex(), ""),
68+
).use { resultSet ->
69+
MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true))
70+
MatcherAssert.assertThat<String>(resultSet.getNString(1), CoreMatchers.equalTo<String>("Fall"))
71+
MatcherAssert.assertThat<String>(resultSet.getNString(2), CoreMatchers.equalTo<String>("Apparel"))
72+
MatcherAssert.assertThat<String>(resultSet.getNString(3), CoreMatchers.equalTo<String>("10"))
73+
MatcherAssert.assertThat<String>(resultSet.getNString(4), CoreMatchers.equalTo<String>("120"))
74+
MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true))
75+
MatcherAssert.assertThat<String>(resultSet.getNString(1), CoreMatchers.equalTo<String>("Spring"))
76+
MatcherAssert.assertThat<String>(resultSet.getNString(2), CoreMatchers.equalTo<String>("Kitchen"))
77+
MatcherAssert.assertThat<String>(resultSet.getNString(3), CoreMatchers.equalTo<String>("3"))
78+
MatcherAssert.assertThat<String>(resultSet.getNString(4), CoreMatchers.equalTo<String>("54"))
79+
MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true))
80+
MatcherAssert.assertThat<String>(resultSet.getNString(1), CoreMatchers.equalTo<String>("Summer"))
81+
MatcherAssert.assertThat<String>(resultSet.getNString(2), CoreMatchers.equalTo<String>("Tech"))
82+
MatcherAssert.assertThat<String>(resultSet.getNString(3), CoreMatchers.equalTo<String>("5"))
83+
MatcherAssert.assertThat<String>(resultSet.getNString(4), CoreMatchers.equalTo<String>("100"))
84+
MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true))
85+
MatcherAssert.assertThat<String>(resultSet.getNString(1), CoreMatchers.equalTo<String>("Winter"))
86+
MatcherAssert.assertThat<String>(resultSet.getNString(2), CoreMatchers.equalTo<String>("Apparel"))
87+
MatcherAssert.assertThat<String>(resultSet.getNString(3), CoreMatchers.equalTo<String>("9"))
88+
MatcherAssert.assertThat<String>(resultSet.getNString(4), CoreMatchers.equalTo<String>("110"))
89+
MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(false))
90+
}
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)