#3570

Find Books with No Available Copies

Easy
DatabaseHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal 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
  1. 1Step 1: Join library_books and borrowing_records on book_id.
  2. 2Step 2: Filter for books where total_copies is zero and return_date is NULL.
  3. 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.