LeetCode 1495: Friendly Movies Streamed Last Month

Database

Problem Description

Explanation:

To solve this problem, we need to find the movies that were streamed by at least two friends in the last month. We can achieve this by using a HashMap to store the count of unique friends who streamed each movie. Then, we iterate through the HashMap and count the movies that were streamed by at least two friends.

  • Create a HashMap to store the count of unique friends who streamed each movie.
  • Iterate through the streaming history and update the count in the HashMap.
  • Iterate through the HashMap and count the movies streamed by at least two friends.

Time Complexity: O(n) where n is the number of streaming records
Space Complexity: O(n) for the HashMap

:

Solutions

import java.util.*;

class Solution {
    public int numMoviesViewedLastMonth(List<List<Integer>> movies) {
        Map<Integer, Integer> movieCount = new HashMap<>();
        int result = 0;
        
        for (List<Integer> movie : movies) {
            for (int friend : movie) {
                movieCount.put(friend, movieCount.getOrDefault(friend, 0) + 1);
            }
        }
        
        for (int count : movieCount.values()) {
            if (count >= 2) {
                result++;
            }
        }
        
        return result;
    }
}

Loading editor...