LeetCode 3358: Books with NULL Ratings Solution
Master LeetCode problem 3358 (Books with NULL Ratings), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3358. Books with NULL Ratings
Problem Explanation
Explanation
To solve this problem, we need to find all the books with NULL ratings and return their IDs in a sorted order. We can iterate through the given list of books and check if the rating is NULL. If so, we add the book ID to our result list. Finally, we sort the result list before returning it.
Algorithmic Idea
- Initialize an empty list to store the IDs of books with NULL ratings.
- Iterate through the given list of books, check if the rating is NULL, and if so, add the book ID to the result list.
- Sort the result list in ascending order.
- Return the sorted list of book IDs.
Time Complexity
The time complexity of this solution is O(nlogn), where n is the number of books in the input list. The sorting operation accounts for the O(nlogn) complexity.
Space Complexity
The space complexity of this solution is O(n), where n is the number of books with NULL ratings.
Solution Code
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
public List<Integer> getBooksNullRatings(List<Book> books) {
List<Integer> result = new ArrayList<>();
for (Book book : books) {
if (book.getRating() == null) {
result.add(book.getId());
}
}
Collections.sort(result);
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3358 (Books with NULL Ratings)?
This page provides optimized solutions for LeetCode problem 3358 (Books with NULL Ratings) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3358 (Books with NULL Ratings)?
The time complexity for LeetCode 3358 (Books with NULL Ratings) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3358 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3358 in Java, C++, or Python.