FastPrepFastPrep
Problem Brief

Count Valid Tokens

FULLTIMEOA

The other question asked in the same batch is LC647.

After a user logs in, they receive a token. If the token exceeds the system-defined expiration period (expiryLimit), it becomes invalid. However, if the token is reset within the period, the expiration time is extended.

Valid tokens can be reset repeatedly. Expired or non-existent tokens will have their reset operations ignored. Expired tokens cannot be used again.

Command format: [type, token_id, T]

  • type 0 (create): Create a token with an expiration time set to T + expiryLimit.
  • type 1 (reset): Extend the token's expiration time to T + expiryLimit.
  • Initially, there are no tokens. Process the requests in order and determine how many tokens are still valid at the maximum time point.

    Function Description

    Complete the function countValidTokens in the editor.

    countValidTokens has the following parameters:

    1. 1. int expiryLimit: the system-defined expiration period
    2. 2. int[][] commands: a 2D array of commands where each command is of the format [type, token_id, T]

    Returns

    int: the number of valid tokens at the maximum time point

    1Example 1

    Input
    expiryLimit = 4, commands = [[0, 1, 1], [0, 2, 2], [1, 1, 5], [1, 2, 7]]
    Output
    1
    Explanation
  • [0, 1, 1]: At T = 1, create token_id = 1 with an expiration time of 1 + 4 = 5.
  • [0, 2, 2]: At T = 2, create token_id = 2 with an expiration time of 2 + 4 = 6.
  • [1, 1, 5]: At T = 5, reset token_id = 1. Since it is still valid (T ≤ 5), the new expiration time becomes 5 + 4 = 9.
  • [1, 2, 7]: At T = 7, reset token_id = 2. However, this token expired at T = 6, so the reset operation is ignored.
  • At T = 7, only token_id = 1 is still valid. The result returns 1.
    public int countValidTokens(int expiryLimit, int[][] commands) {
      // write your code here
    }
    
    Input

    expiryLimit

    4

    commands

    [[0, 1, 1], [0, 2, 2], [1, 1, 5], [1, 2, 7]]

    Output

    1

    Sign in to submit your solution.