Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Mentions Per User

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3433" 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

You are given an integer numberOfUsers representing the total number of users and an array events of size n x 3. Each events[i] can be either of the following two types: Message Event: ["MESSAGE", "timestampi", "mentions_stringi"] This event indicates that a set of users was mentioned in a message at timestampi. The mentions_stringi string can contain one of the following tokens: id: where is an integer in range [0,numberOfUsers - 1]. There can be multiple ids separated by a single whitespace and may contain duplicates. This can mention even the offline users. ALL: mentions all users. HERE: mentions all online users. Offline Event: ["OFFLINE", "timestampi", "idi"] This event indicates that the user idi had become offline at timestampi for 60 time units. The user will automatically be online again at time timestampi + 60. Return an array mentions where mentions[i] represents the number of mentions the user with id i has across all MESSAGE events. All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp. Note that a user can be mentioned multiple times in a single message event, and each mention should be counted separately. Example 1: Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]] Output: [2,2] Explanation: Initially, all users are online. At timestamp 10, id1 and id0 are mentioned. mentions = [1,1] At timestamp 11, id0 goes offline. At timestamp 71, id0 comes back online and "HERE" is mentioned. mentions = [2,2] Example 2: Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]] Output: [2,2] Explanation: Initially, all users are online. At timestamp 10, id1 and id0 are mentioned. mentions = [1,1] At timestamp 11, id0 goes offline. At timestamp 12, "ALL" is mentioned. This includes offline users, so both id0 and id1 are mentioned. mentions = [2,2] Example 3: Input: numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]] Output: [0,1] Explanation: Initially, all users are online. At timestamp 10, id0 goes offline. At timestamp 12, "HERE" is mentioned. Because id0 is still offline, they will not be mentioned. mentions = [0,1] Constraints: 1 <= numberOfUsers <= 100 1 <= events.length <= 100 events[i].length == 3 events[i][0] will be one of MESSAGE or OFFLINE. 1 <= int(events[i][1]) <= 105 The number of id mentions in any "MESSAGE" event is between 1 and 100. 0 <= <= numberOfUsers - 1 It is guaranteed that the user id referenced in the OFFLINE event is online at the time the event occurs.

Explanation

Here's the breakdown of the solution:

  • Simulate Events: Iterate through the events array, processing each event in chronological order (based on timestamp). Maintain the online/offline status of each user using a dictionary or array.
  • Handle Message Events: For "MESSAGE" events, parse the mentions string. If it contains "ALL", increment the mention count for all users. If it contains "HERE", increment the mention count only for currently online users. If it contains "id" mentions, parse the IDs and increment the mention count for the corresponding users.
  • Handle Offline Events: For "OFFLINE" events, record the offline timestamp for the specified user. When processing message events, check if each user is online based on their offline timestamp and the current event timestamp.

  • Runtime Complexity: O(n * m), where n is the number of events and m is the maximum number of users or mentions processed in a single event.

  • Storage Complexity: O(numberOfUsers) to store user online statuses and mention counts.

Code

    def solve():
    numberOfUsers = int(input())
    events = []
    num_events = int(input())
    for _ in range(num_events):
        events.append(input().split())

    mentions = [0] * numberOfUsers
    offline_times = {}

    for event in events:
        event_type = event[0]
        timestamp = int(event[1])

        if event_type == "MESSAGE":
            mentions_string = event[2]
            mentioned_users = set()

            if "ALL" in mentions_string:
                for user_id in range(numberOfUsers):
                    mentions[user_id] += 1
            elif "HERE" in mentions_string:
                for user_id in range(numberOfUsers):
                    if user_id not in offline_times or timestamp >= offline_times[user_id] + 60:
                        mentions[user_id] += 1
            else:
                ids = mentions_string.split()
                for id_str in ids:
                    user_id = int(id_str[2:])
                    mentions[user_id] += 1

        elif event_type == "OFFLINE":
            user_id = int(event[2])
            offline_times[user_id] = timestamp

    print(mentions)


# Get input
numberOfUsers = int(input())
num_events = int(input())
events = []
for _ in range(num_events):
    events.append(input().split())


def get_mentions(numberOfUsers, events):
    mentions = [0] * numberOfUsers
    offline_times = {}

    for event in events:
        event_type = event[0]
        timestamp = int(event[1])

        if event_type == "MESSAGE":
            mentions_string = event[2]
            mentioned_users = set()

            if "ALL" in mentions_string:
                for user_id in range(numberOfUsers):
                    mentions[user_id] += 1
            elif "HERE" in mentions_string:
                for user_id in range(numberOfUsers):
                    if user_id not in offline_times or timestamp >= offline_times[user_id] + 60:
                        mentions[user_id] += 1
            else:
                ids = mentions_string.split()
                for id_str in ids:
                    user_id = int(id_str[2:])
                    mentions[user_id] += 1

        elif event_type == "OFFLINE":
            user_id = int(event[2])
            offline_times[user_id] = timestamp
    return mentions

print(get_mentions(numberOfUsers, events))

More from this blog

C

Chatmagic blog

2894 posts