Skip to content

Commit 74fa51c

Browse files
committed
Added task 3570
1 parent 3e6bc51 commit 74fa51c

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
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+
| 3570 |[Find Books with No Available Copies](src/main/java/g3501_3600/s3570_find_books_with_no_available_copies)| Easy | Database | 512 | 100.00
20912092
| 3569 |[Maximize Count of Distinct Primes After Split](src/main/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split)| Hard | Array, Math, Segment_Tree, Number_Theory | 280 | 97.30
20922093
| 3568 |[Minimum Moves to Clean the Classroom](src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix, Bit_Manipulation | 94 | 99.86
20932094
| 3567 |[Minimum Absolute Difference in Sliding Submatrix](src/main/java/g3501_3600/s3567_minimum_absolute_difference_in_sliding_submatrix)| Medium | Array, Sorting, Matrix | 7 | 99.69
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3570\. Find Books with No Available Copies
5+
6+
Easy
7+
8+
Table: `library_books`
9+
10+
+------------------+---------+
11+
| Column Name | Type |
12+
+------------------+---------+
13+
| book_id | int |
14+
| title | varchar |
15+
| author | varchar |
16+
| genre | varchar |
17+
| publication_year | int |
18+
| total_copies | int |
19+
+------------------+---------+
20+
book_id is the unique identifier for this table.
21+
Each row contains information about a book in the library, including the total number of copies owned by the library.
22+
23+
Table: `borrowing_records`
24+
25+
+---------------+---------+
26+
| Column Name | Type |
27+
|----------------|---------|
28+
| record_id | int |
29+
| book_id | int |
30+
| borrower_name | varchar |
31+
| borrow_date | date |
32+
| return_date | date |
33+
+----------------+---------+
34+
record_id is the unique identifier for this table.
35+
Each row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.
36+
37+
Write a solution to find **all books** that are **currently borrowed (not returned)** and have **zero copies available** in the library.
38+
39+
* A book is considered **currently borrowed** if there exists a borrowing record with a **NULL** `return_date`
40+
41+
Return _the result table ordered by current borrowers in **descending** order, then by book title in **ascending** order._
42+
43+
The result format is in the following example.
44+
45+
**Example:**
46+
47+
**Input:**
48+
49+
library\_books table:
50+
51+
+---------+--------------------------+----------------+-----------+------------------+--------------+
52+
| book_id | Title | Author | Genre | Publication Year | Total Copies |
53+
|---------|--------------------------|----------------|-----------|------------------|--------------|
54+
| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |
55+
| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 |
56+
| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |
57+
| 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 |
58+
| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 |
59+
| 6 | Brave New World | Aldous Huxley | Dystopian | 1932 | 4 |
60+
+---------+--------------------------+----------------+-----------+------------------+--------------+
61+
62+
borrowing\_records table:
63+
64+
+-----------+---------+---------------+-------------+-------------+
65+
| record_id | book_id | borrower_name | borrow_date | return_date |
66+
|-----------|---------|---------------|-------------|-------------|
67+
| 1 | 1 | Alice Smith | 2024-01-15 | NULL |
68+
| 2 | 1 | Bob Johnson | 2024-01-20 | NULL |
69+
| 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 |
70+
| 4 | 3 | David Brown | 2024-02-01 | NULL |
71+
| 5 | 4 | Emma Wilson | 2024-01-05 | NULL |
72+
| 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 |
73+
| 7 | 1 | Grace Miller | 2024-02-05 | NULL |
74+
| 8 | 6 | Henry Taylor | 2024-01-12 | NULL |
75+
| 9 | 2 | Ivan Clark | 2024-02-12 | NULL |
76+
| 10 | 2 | Jane Adams | 2024-02-15 | NULL |
77+
+-----------+---------+---------------+-------------+-------------+
78+
79+
**Output:**
80+
81+
+---------+-------------------+----------------+-----------+------------------+-------------------+
82+
| book_id | Title | Author | Genre | Publication Year | Current Borrowers |
83+
|---------|-------------------|----------------|-----------|------------------|-------------------|
84+
| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |
85+
| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |
86+
+---------+-------------------+----------------+-----------+------------------+-------------------+
87+
88+
**Explanation:**
89+
90+
* **The Great Gatsby (book\_id = 1):**
91+
* Total copies: 3
92+
* Currently borrowed by Alice Smith, Bob Johnson, and Grace Miller (3 borrowers)
93+
* Available copies: 3 - 3 = 0
94+
* Included because available\_copies = 0
95+
* **1984 (book\_id = 3):**
96+
* Total copies: 1
97+
* Currently borrowed by David Brown (1 borrower)
98+
* Available copies: 1 - 1 = 0
99+
* Included because available\_copies = 0
100+
* **Books not included:**
101+
* To Kill a Mockingbird (book\_id = 2): Total copies = 3, current borrowers = 2, available = 1
102+
* Pride and Prejudice (book\_id = 4): Total copies = 2, current borrowers = 1, available = 1
103+
* The Catcher in the Rye (book\_id = 5): Total copies = 1, current borrowers = 0, available = 1
104+
* Brave New World (book\_id = 6): Total copies = 4, current borrowers = 1, available = 3
105+
* **Result ordering:**
106+
* The Great Gatsby appears first with 3 current borrowers
107+
* 1984 appears second with 1 current borrower
108+
109+
Output table is ordered by current\_borrowers in descending order, then by book\_title in ascending order.
110+
111+
## Solution
112+
113+
```sql
114+
# Write your MySQL query statement below
115+
SELECT
116+
book_id,
117+
MAX(title) AS title,
118+
MAX(author) AS author,
119+
MAX(genre) AS genre,
120+
MAX(publication_year) AS publication_year,
121+
MAX(total_copies) AS current_borrowers
122+
FROM (
123+
SELECT
124+
book_id,
125+
title,
126+
author,
127+
genre,
128+
publication_year,
129+
total_copies,
130+
total_copies AS total_remain
131+
FROM library_books
132+
UNION ALL
133+
SELECT
134+
book_id,
135+
'' AS title,
136+
'' AS author,
137+
'' AS genre,
138+
1000 AS publication_year,
139+
0 AS total_copies,
140+
-1 AS total_remain
141+
FROM borrowing_records
142+
WHERE return_date IS NULL
143+
) AS sub
144+
GROUP BY
145+
book_id
146+
HAVING
147+
SUM(total_remain) = 0
148+
ORDER BY
149+
current_borrowers DESC,
150+
title ASC;
151+
```

0 commit comments

Comments
 (0)