Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Get Watched Videos by Your Friends

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1311" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i. Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest. Example 1: Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1 Output: ["B","C"] Explanation: You have id = 0 (green color in the figure) and your friends are (yellow color in the figure): Person with id = 1 -> watchedVideos = ["C"] Person with id = 2 -> watchedVideos = ["B","C"] The frequencies of watchedVideos by your friends are: B -> 1 C -> 2 Example 2: Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2 Output: ["D"] Explanation: You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure). Constraints: n == watchedVideos.length == friends.length 2 <= n <= 100 1 <= watchedVideos[i].length <= 100 1 <= watchedVideos[i][j].length <= 8 0 <= friends[i].length < n 0 <= friends[i][j] < n 0 <= id < n 1 <= level < n if friends[i] contains j, then friends[j] contains i

Explanation

  • Breadth-First Search (BFS): Use BFS to find all friends at the specified level.
    • Frequency Counting: Count the frequency of videos watched by those friends.
    • Sorting: Sort the videos based on frequency (ascending) and then alphabetically (ascending).
  • Runtime Complexity: O(N + V log V), where N is the number of people, and V is the total number of videos watched by the friends at the given level. The N comes from the BFS and the V log V comes from sorting the videos.
  • Storage Complexity: O(N + V), where N is the number of people, and V is the number of distinct videos watched by the friends at the given level.

Code

    from collections import defaultdict, deque

def watchedVideosByFriends(watchedVideos, friends, id, level):
    n = len(watchedVideos)
    q = deque([id])
    visited = {id}
    distance = 0

    while q and distance < level:
        next_level = deque()
        while q:
            u = q.popleft()
            for v in friends[u]:
                if v not in visited:
                    visited.add(v)
                    next_level.append(v)
        q = next_level
        distance += 1

    video_counts = defaultdict(int)
    for friend_id in q:
        for video in watchedVideos[friend_id]:
            video_counts[video] += 1

    sorted_videos = sorted(video_counts.items(), key=lambda item: (item[1], item[0]))
    return [video for video, count in sorted_videos]

More from this blog

C

Chatmagic blog

2894 posts