Table: Cinema
+----------------+----------+ | Column Name | Type | +----------------+----------+ | id | int | | movie | varchar | | description | varchar | | rating | float | +----------------+----------+ id is the primary key (column with unique values) for this table. Each row contains information about the name of a movie, its genre, and its rating. rating is a 2 decimal places float in the range [0, 10]
Write a solution to report the movies with an odd-numbered ID and a description that is not "boring".
Return the result table ordered by rating in descending order.
The result format is in the following example.
Example 1:
Input: Cinema table: +----+------------+-------------+--------+ | id | movie | description | rating | +----+------------+-------------+--------+ | 1 | War | great 3D | 8.9 | | 2 | Science | fiction | 8.5 | | 3 | irish | boring | 6.2 | | 4 | Ice song | Fantacy | 8.6 | | 5 | House card | Interesting | 9.1 | +----+------------+-------------+--------+ Output: +----+------------+-------------+--------+ | id | movie | description | rating | +----+------------+-------------+--------+ | 5 | House card | Interesting | 9.1 | | 1 | War | great 3D | 8.9 | +----+------------+-------------+--------+ Explanation: We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 is boring so we do not include it in the answer.
This approach involves using a straightforward SQL query to filter movies based on conditions specified in the question. We will look for movies with an ID that is odd and where the description is not 'boring'. Once filtered, results will be sorted according to the ratings in descending order.
We use SQLAlchemy to define a table 'Cinema' and insert the sample data. We query this table using conditions specified: id as odd and a non-'boring' description, finally ordering by ratings in descending order. The results are printed one by one.
The time complexity of this approach is O(n log n) due to sorting of the movies after filtering, and space complexity is O(1) as we do not use extra space apart from input size.
This approach directly uses SQL logic to filter out the records based on conditions specified in the question: selecting only those entries with an odd-numbered ID and a non-boring description, followed by sorting the result set based on ratings in descending order.
We begin by defining a Movie class and a list of Movie objects, after which we filter through this list in a LINQ query to fetch movies with odd-numbered IDs while excluding descriptions marked 'boring'. Finally, results are ordered by rating before being printed.
The time complexity of this approach is O(n log n) due to sorting, and the space complexity is O(n) because we store the list of filtered movies.
This approach involves breaking down the problem into smaller sub-problems, solving each one individually, and then combining their solutions to solve the original problem. This is a powerful technique used in algorithms like Merge Sort and Quick Sort. It allows us to efficiently tackle the problem by utilizing recursive problem-solving.
This is a Merge Sort implementation in C, which follows the Divide and Conquer technique. The array is recursively split into halves until each sub-array has one element. Then, the sub-arrays are merged back together in a sorted manner. This algorithm is efficient for large datasets.
Python
Time Complexity: O(n log n)
Space Complexity: O(n)
Dynamic programming is a technique used for solving complex problems by breaking them down into simpler subproblems. Tabulation involves solving the subproblems and storing their results in a table (usually an array) to avoid redundant calculations. This approach is efficient for problems where overlapping subproblems and optimal substructure properties exist.
This JavaScript function calculates the nth Fibonacci number using a bottom-up dynamic programming approach. It uses an array to store and reuse previously computed Fibonacci numbers, efficiently computing the result in O(n) time.
Java
Time Complexity: O(n)
Space Complexity: O(n)
| Approach | Complexity |
|---|---|
| SQL Query Using Simple Filtering | The time complexity of this approach is O(n log n) due to sorting of the movies after filtering, and space complexity is O(1) as we do not use extra space apart from input size. |
| Query Using SQL Conditions | The time complexity of this approach is O(n log n) due to sorting, and the space complexity is O(n) because we store the list of filtered movies. |
| Divide and Conquer | Time Complexity: O(n log n) |
| Dynamic Programming (Tabulation) | Time Complexity: O(n) |
Not Boring Movies | Leetcode 620 | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 4,817 views views
Watch 9 more video solutions →Practice Not Boring Movies with our built-in code editor and test cases.
Practice on FleetCode