Skip to content

Commit 68b37dc

Browse files
committed
Added task 3626
1 parent d59ae46 commit 68b37dc

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3626 |[Find Stores with Inventory Imbalance](src/main/kotlin/g3601_3700/s3626_find_stores_with_inventory_imbalance)| Medium || 516 | 100.00
20912092
| 3625 |[Count Number of Trapezoids II](src/main/kotlin/g3601_3700/s3625_count_number_of_trapezoids_ii)| Hard | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 377 | 100.00
20922093
| 3624 |[Number of Integers With Popcount-Depth Equal to K II](src/main/kotlin/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii)| Hard | Array, Segment_Tree, Weekly_Contest_459 | 38 | 100.00
20932094
| 3623 |[Count Number of Trapezoids I](src/main/kotlin/g3601_3700/s3623_count_number_of_trapezoids_i)| Medium | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 58 | 68.00

src/main/kotlin/g3601_3700/s3620_network_recovery_pathways/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Solution {
111111
q.offer(i)
112112
}
113113
}
114-
while (!q.isEmpty()) {
114+
while (q.isNotEmpty()) {
115115
val u = q.poll()
116116
ts.add(u)
117117
for (v in g[u]) {
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3626\. Find Stores with Inventory Imbalance
5+
6+
Medium
7+
8+
Table: `stores`
9+
10+
+------------+----------+
11+
| Column Name| Type |
12+
+------------+----------+
13+
| store_id | int |
14+
| store_name | varchar |
15+
| location | varchar |
16+
+------------+----------+
17+
18+
store_id is the unique identifier for this table.
19+
Each row contains information about a store and its location.
20+
21+
Table: `inventory`
22+
23+
+--------------+----------+
24+
| Column Name | Type |
25+
+--------------+----------+
26+
| inventory_id | int |
27+
| store_id | int |
28+
| product_name | varchar |
29+
| quantity | int |
30+
| price | decimal |
31+
+--------------+----------+
32+
33+
inventory_id is the unique identifier for this table.
34+
Each row represents the inventory of a specific product at a specific store.
35+
36+
Write a solution to find stores that have **inventory imbalance** - stores where the most expensive product has lower stock than the cheapest product.
37+
38+
* For each store, identify the **most expensive product** (highest price) and its quantity
39+
* For each store, identify the **cheapest product** (lowest price) and its quantity
40+
* A store has inventory imbalance if the most expensive product's quantity is **less than** the cheapest product's quantity
41+
* Calculate the **imbalance ratio** as (cheapest\_quantity / most\_expensive\_quantity)
42+
* **Round** the imbalance ratio to **2** decimal places
43+
* Only include stores that have **at least** `3` **different products**
44+
45+
Return _the result table ordered by imbalance ratio in **descending** order, then by store name in **ascending** order_.
46+
47+
The result format is in the following example.
48+
49+
**Example:**
50+
51+
**Input:**
52+
53+
stores table:
54+
55+
+----------+----------------+-------------+
56+
| store_id | store_name | location |
57+
+----------+----------------+-------------+
58+
| 1 | Downtown Tech | New York |
59+
| 2 | Suburb Mall | Chicago |
60+
| 3 | City Center | Los Angeles |
61+
| 4 | Corner Shop | Miami |
62+
| 5 | Plaza Store | Seattle |
63+
+----------+----------------+-------------+
64+
65+
inventory table:
66+
67+
+--------------+----------+--------------+----------+--------+
68+
| inventory_id | store_id | product_name | quantity | price |
69+
+--------------+----------+--------------+----------+--------+
70+
| 1 | 1 | Laptop | 5 | 999.99 |
71+
| 2 | 1 | Mouse | 50 | 19.99 |
72+
| 3 | 1 | Keyboard | 25 | 79.99 |
73+
| 4 | 1 | Monitor | 15 | 299.99 |
74+
| 5 | 2 | Phone | 3 | 699.99 |
75+
| 6 | 2 | Charger | 100 | 25.99 |
76+
| 7 | 2 | Case | 75 | 15.99 |
77+
| 8 | 2 | Headphones | 20 | 149.99 |
78+
| 9 | 3 | Tablet | 2 | 499.99 |
79+
| 10 | 3 | Stylus | 80 | 29.99 |
80+
| 11 | 3 | Cover | 60 | 39.99 |
81+
| 12 | 4 | Watch | 10 | 299.99 |
82+
| 13 | 4 | Band | 25 | 49.99 |
83+
| 14 | 5 | Camera | 8 | 599.99 |
84+
| 15 | 5 | Lens | 12 | 199.99 |
85+
+--------------+----------+--------------+----------+--------+
86+
87+
**Output:**
88+
89+
+----------+----------------+-------------+------------------+--------------------+------------------+
90+
| store_id | store_name | location | most_exp_product | cheapest_product | imbalance_ratio |
91+
+----------+----------------+-------------+------------------+--------------------+------------------+
92+
| 3 | City Center | Los Angeles | Tablet | Stylus | 40.00 |
93+
| 1 | Downtown Tech | New York | Laptop | Mouse | 10.00 |
94+
| 2 | Suburb Mall | Chicago | Phone | Case | 25.00 |
95+
+----------+----------------+-------------+------------------+--------------------+------------------+
96+
97+
**Explanation:**
98+
99+
* **Downtown Tech (store\_id = 1):**
100+
* Most expensive product: Laptop ($999.99) with quantity 5
101+
* Cheapest product: Mouse ($19.99) with quantity 50
102+
* Inventory imbalance: 5 < 50 (expensive product has lower stock)
103+
* Imbalance ratio: 50 / 5 = 10.00
104+
* Has 4 products (≥ 3), so qualifies
105+
* **Suburb Mall (store\_id = 2):**
106+
* Most expensive product: Phone ($699.99) with quantity 3
107+
* Cheapest product: Case ($15.99) with quantity 75
108+
* Inventory imbalance: 3 < 75 (expensive product has lower stock)
109+
* Imbalance ratio: 75 / 3 = 25.00
110+
* Has 4 products (≥ 3), so qualifies
111+
* **City Center (store\_id = 3):**
112+
* Most expensive product: Tablet ($499.99) with quantity 2
113+
* Cheapest product: Stylus ($29.99) with quantity 80
114+
* Inventory imbalance: 2 < 80 (expensive product has lower stock)
115+
* Imbalance ratio: 80 / 2 = 40.00
116+
* Has 3 products (≥ 3), so qualifies
117+
* **Stores not included:**
118+
* Corner Shop (store\_id = 4): Only has 2 products (Watch, Band) - doesn't meet minimum 3 products requirement
119+
* Plaza Store (store\_id = 5): Only has 2 products (Camera, Lens) - doesn't meet minimum 3 products requirement
120+
121+
The Results table is ordered by imbalance ratio in descending order, then by store name in ascending order
122+
123+
## Solution
124+
125+
```sql
126+
# Write your MySQL query statement below
127+
WITH store_product_check AS (
128+
SELECT
129+
s.store_id,
130+
s.store_name,
131+
s.location,
132+
COUNT(i.inventory_id) AS store_product_ct
133+
FROM
134+
stores s
135+
JOIN inventory i ON s.store_id = i.store_id
136+
GROUP BY
137+
s.store_id,
138+
s.store_name,
139+
s.location
140+
HAVING
141+
COUNT(i.inventory_id) >= 3
142+
),
143+
store_product_ranked AS (
144+
SELECT
145+
s.store_id,
146+
s.store_name,
147+
s.location,
148+
i.inventory_id,
149+
i.product_name,
150+
i.quantity,
151+
i.price,
152+
ROW_NUMBER() OVER (PARTITION BY s.store_id ORDER BY i.price ASC) AS low_price_rk,
153+
ROW_NUMBER() OVER (PARTITION BY s.store_id ORDER BY i.price DESC) AS high_price_rk
154+
FROM
155+
stores s
156+
JOIN inventory i ON s.store_id = i.store_id
157+
),
158+
high_low_price AS (
159+
SELECT
160+
spc.store_id,
161+
spc.store_name,
162+
spc.location,
163+
lp.product_name AS low_price_product_name,
164+
lp.quantity + 0.0 AS low_price_quantity,
165+
hp.product_name AS high_price_product_name,
166+
hp.quantity + 0.0 AS high_price_quantity
167+
FROM
168+
store_product_check spc
169+
JOIN store_product_ranked lp
170+
ON spc.store_id = lp.store_id AND lp.low_price_rk = 1
171+
JOIN store_product_ranked hp
172+
ON spc.store_id = hp.store_id AND hp.high_price_rk = 1
173+
)
174+
SELECT
175+
hlp.store_id,
176+
hlp.store_name,
177+
hlp.location,
178+
hlp.high_price_product_name AS most_exp_product,
179+
hlp.low_price_product_name AS cheapest_product,
180+
ROUND(hlp.low_price_quantity / hlp.high_price_quantity, 2) AS imbalance_ratio
181+
FROM
182+
high_low_price hlp
183+
WHERE
184+
hlp.high_price_quantity < hlp.low_price_quantity
185+
ORDER BY
186+
imbalance_ratio DESC,
187+
hlp.store_name ASC;
188+
```

0 commit comments

Comments
 (0)