#620

Not Boring Movies

Easy
DatabaseFiltering with WHERE clauseSorting results with ORDER BY
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n)
O(n log n)
Space
O(n)
O(n)
💡

Intuition

Time O(n log n)Space O(n)

The optimal solution leverages SQL's filtering and sorting capabilities directly. By using a single query that checks for odd IDs and non-boring descriptions, we can efficiently retrieve and sort the desired results in one go.

⚙️

Algorithm

3 steps
  1. 1Step 1: Use a SELECT statement to get the required columns from the Cinema table.
  2. 2Step 2: Apply a WHERE clause to filter for odd IDs and descriptions that are not 'boring'.
  3. 3Step 3: Use ORDER BY to sort the results by rating in descending order.
solution.py1 lines
1SELECT id, movie, description, rating FROM Cinema WHERE id % 2 = 1 AND description != 'boring' ORDER BY rating DESC;

Complexity note: The time complexity is O(n log n) due to the sorting operation, while the space complexity remains O(n) for storing the results.

  • 1Filtering based on conditions (odd ID and non-boring description) is crucial for the solution.
  • 2Sorting the results by rating ensures we return the highest-rated movies first.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.