Similar Problems

Similar Problems not available

Not Boring Movies - Leetcode Solution

Companies:

  • amazon

LeetCode:  Not Boring Movies Leetcode Solution

Difficulty: Easy

Topics: database  

Problem Statement:

You are given a list of movies along with their quality ratings. Your task is to sort the movies in such a way that the difference between the quality ratings of subsequent movies is as small as possible. If multiple solutions are possible, then select the solution which has a lower sum of all quality ratings.

Note: The list of movies can have duplicate entries.

Example 1:

Input: ["day","fish","elephant","tiger","dog","cat","bird"], [1, 2, 3, 4, 5, 6, 7]

Output: ["fish","cat","dog","bird","tiger","elephant","day"]

Explanation: The solution with the smallest difference is ["fish","cat","dog","bird","tiger","elephant","day"] with a difference of 1 and total rating of 28.

Solution:

To solve this problem, we need to sort the movies based on their quality rating. After sorting the movies, we can then use a two-pointer approach to swap adjacent movies to minimize the difference between their quality ratings. We also need to keep track of the sum of all quality ratings to get the solution with the least sum.

Steps:

  1. Create a hashmap to store the mapping of movie names to their quality ratings.
  2. Sort the movies based on their quality ratings.
  3. Use a two-pointer approach to swap adjacent movies to minimize the difference between their quality ratings.
  4. While swapping, keep track of the sum of all quality ratings to get the solution with the least sum.
  5. Return the list of movies after sorting and swapping them.

Python code:

class Solution: def notBoringMovies(self, movie_list: List[str], rating_list: List[int]) -> List[str]:

    # create a hashmap to store mapping of movie names to their quality ratings
    movie_rating = dict(zip(movie_list, rating_list))
    
    # sort the movies based on their quality ratings
    movie_list = sorted(movie_list, key=lambda x: movie_rating[x])
    
    # use a two-pointer approach to swap adjacent movies to minimize the difference in their quality ratings
    i, j = 0, 1
    min_diff = float('inf')
    result = []
    sum_rating = sum(rating_list)
    
    while j < len(movie_list):
        
        # calculate the difference in quality ratings
        diff = movie_rating[movie_list[j]] - movie_rating[movie_list[i]]
        
        # check if the difference is smaller than the current minimum difference
        if diff < min_diff:
            min_diff = diff
            result = movie_list[:]
            i, j = i+1, j+1
        
        # if the difference is the same as the current minimum difference, check the sum of ratings
        elif diff == min_diff:
            if sum(movie_rating[x] for x in movie_list[i:j+1]) < sum_rating:
                sum_rating = sum(movie_rating[x] for x in movie_list[i:j+1])
                result = movie_list[:]
            i, j = i+1, j+1
        
        # if the difference is greater than the current minimum difference, move the pointers
        else:
            i, j = i+1, j+1
            
    return result

Time Complexity:

The time complexity of this solution is O(n log n), where n is the number of movies. The sorting operation takes O(n log n) time, and the two-pointer approach takes O(n) time.

Space Complexity:

The space complexity of this solution is O(n), where n is the number of movies, as we need to store the mapping of movie names to their quality ratings and also the result list.

Not Boring Movies Solution Code

1