#3570
Find Books with No Available Copies
EasyDatabaseHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
Use a single query to find books with zero copies and currently borrowed, leveraging SQL's ability to join tables efficiently.
⚙️
Algorithm
3 steps- 1Step 1: Join library_books and borrowing_records on book_id.
- 2Step 2: Filter for books where total_copies is zero and return_date is NULL.
- 3Step 3: Select the required columns from the result.
solution.py1 lines
1SELECT lb.book_id, lb.title FROM library_books lb JOIN borrowing_records br ON lb.book_id = br.book_id WHERE lb.total_copies = 0 AND br.return_date IS NULL;ℹ
Complexity note: The join operation is linear with respect to the number of records, making it efficient.
- 1Understanding joins can simplify complex queries.
- 2Filtering early reduces the amount of data processed.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.