Similar Problems

Similar Problems not available

Leetflex Banned Accounts - Leetcode Solution

Companies:

LeetCode:  Leetflex Banned Accounts Leetcode Solution

Difficulty: Medium

Topics: database  

Leetflex Banned Accounts problem is a problem on LeetCode that deals with identifying the banned accounts and finding the number of valid accounts. The problem statement is as follows:

You are given a list of login attempts. Each attempt is a dictionary with two keys: 'username' and 'timestamp'. The 'timestamp' is a positive integer representing the time the login attempt occurred in seconds since 00:00:00 on 01/01/1970. A login attempt is considered valid if it occurred at least 1 second after the most recent login attempt from the same user. A user's account is considered banned if there have been at least 3 invalid login attempts in a row.

Write a function to iterate through the login attempts and identify any banned accounts. The function should return the number of valid accounts.

Input: The input to the function is a list of dictionaries where each dictionary represents a login attempt. Each dictionary has two keys - 'username' and 'timestamp'. The 'username' key is a string representing the username and the 'timestamp' key is a positive integer representing the time the login attempt occurred.

Output: The output of the function is an integer representing the number of valid accounts.

Solution: To solve this problem, we need to keep track of the last login attempt time for each user and whether the user's account is banned. We can accomplish this by using a dictionary to map usernames to tuples, where each tuple contains the last login time and the number of consecutive invalid login attempts.

The algorithm for solving this problem can be summarized as follows:

  1. Initialize a dictionary, user_dict, with an empty value for each username.
  2. Iterate through each login attempt in the list.
  3. If the user's account is not banned, update the last login time and number of consecutive invalid login attempts in user_dict.
  4. If the user's account is banned, increment a counter variable, banned_count.
  5. Return the number of valid accounts, which is the length of the user_dict minus the banned_count.

Here is the Python code for the solution:

def count_valid_accounts(login_attempts): user_dict = {} banned_count = 0 for attempt in login_attempts: username = attempt['username'] timestamp = attempt['timestamp'] if username not in user_dict: user_dict[username] = (0, 0) last_login_time, num_consecutive_invalid_attempts = user_dict[username] if timestamp - last_login_time >= 1: if num_consecutive_invalid_attempts >= 3: banned_count += 1 user_dict[username] = (last_login_time, 0) else: user_dict[username] = (timestamp, num_consecutive_invalid_attempts) else: user_dict[username] = (last_login_time, num_consecutive_invalid_attempts + 1) return len(user_dict) - banned_count

The time complexity of this algorithm is O(n), where n is the number of login attempts. The space complexity is also O(n), as we need to store information for each login attempt in the dictionary.

Overall, this is a good solution to the Leetflex Banned Accounts problem.

Leetflex Banned Accounts Solution Code

1