leetcode1797. Design a verification system (java)

Question description

Difficulty - Medium
leetcode1797. Design a verification system

You need to design a verification system that includes verification codes. During each verification, the user will receive a new verification code, which expires timeToLive seconds after currentTime. If the verification code is updated, it will extend timeToLive seconds by currentTime (which may be different from the previous currentTime).

Please implement the AuthenticationManager class:
AuthenticationManager(int timeToLive) Construct AuthenticationManager and set the timeToLive parameter.
generate(string tokenId, int currentTime) Given tokenId, generate a new verification code at the current time currentTime.
renew(string tokenId, int currentTime) updates the unexpired verification code with the given tokenId at currentTime. If the verification code corresponding to the given tokenId does not exist or has expired, please ignore this operation and no update operation will occur.
countUnexpiredTokens(int currentTime) Please return the number of unexpired verification codes at the given currentTime.
If a verification code expires at time t, and another operation happens to occur at time t (renew or countUnexpiredTokens operation), the expiration event takes precedence over the other operations.

Example 1:
Insert image description hereInput:
["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"] [[5], ["aaa", 1
] , ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]] Output: [null, null,
null
, 1, null, null, null, 0]
Explanation:
AuthenticationManager authenticationManager = new AuthenticationManager(5); // Construct AuthenticationManager and set timeToLive = 5 seconds.
authenticationManager.renew("aaa", 1); // At time 1, the tokenId without verification code is "aaa", and no verification code is updated.
authenticationManager.generate("aaa", 2); // At time 2, generate a new verification code with tokenId "aaa".
authenticationManager.countUnexpiredTokens(6); // At time 6, only the verification code with tokenId "aaa" has not expired, so 1 is returned.
authenticationManager.generate("bbb", 7); // At time 7, generate a new verification code with tokenId "bbb".
authenticationManager.renew("aaa", 8); // The verification code with tokenId "aaa" expires at time 7, and 8 >= 7, so the renew operation at time 8 is ignored and no verification code is updated.
authenticationManager.renew("bbb", 10); // The verification code with tokenId "bbb" has not expired at time 10, so the renew operation will be executed and the token will expire at time 15.
authenticationManager.countUnexpiredTokens(15); // The verification code with tokenId "bbb" expires at time 15, and the verification code with tokenId "aaa" expires at time 7. All verification codes have expired, so 0 is returned.

Tips:
1 <= timeToLive <= 108
1 <= currentTime <= 108
1 <= tokenId.length <= 5
tokenId only contains lowercase English letters.
All calls to the generate function will contain a unique tokenId value.
The value of currentTime is strictly incremented in all function calls.
All functions may be called a total of no more than 2000 times.

Insert image description here

Hash table

The data range is only 20. We use a hash table to record the expiration time ct of each ID, and traverse the entire hash table during each query to count the number of unexpired verification codes.
This question is quite simple, but it is troublesome to read. It is a reading question.

Question description

class AuthenticationManager {
    
    

    HashMap<String, Integer> map = new HashMap<>();
    int timeOut;
    public AuthenticationManager(int timeToLive) {
    
    
        timeOut = timeToLive;
    }
    
    public void generate(String tokenId, int currentTime) {
    
    
    	//直接放进去
        map.put(tokenId,currentTime + timeOut);
    }
    
    public void renew(String tokenId, int currentTime) {
    
    
    	//如果不包含key 就不用更新,如果过期时间在当前时间之前,那么也不用更新。
        if(!map.containsKey(tokenId) || map.get(tokenId) <= currentTime){
    
    
            return;
        }
        //赋值。
        map.put(tokenId,currentTime + timeOut);
    }

    
    public int countUnexpiredTokens(int currentTime) {
    
    
        int ans = 0;
        //统计未过期的数量。
        for(String id : map.keySet()){
    
    
            if(map.get(id) > currentTime){
    
    
                ans++;
            }
        }
        return ans;
    }
}

Guess you like

Origin blog.csdn.net/SP_1024/article/details/132879064