Skip to content

Commit d10d7c4

Browse files
authored
2025-03-05 v. 8.8.2: added "1797. Design Authentication Manager"
2 parents 82636a8 + fd49349 commit d10d7c4

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
715715
| 1706. Where Will the Ball Fall | [Link](https://leetcode.com/problems/where-will-the-ball-fall/) | [Link](./lib/medium/1706_where_will_the_ball_fall.rb) | [Link](./test/medium/test_1706_where_will_the_ball_fall.rb) |
716716
| 1721. Swapping Nodes in a Linked List | [Link](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/) | [Link](./lib/medium/1721_swapping_nodes_in_a_linked_list.rb) | [Link](./test/medium/test_1721_swapping_nodes_in_a_linked_list.rb) |
717717
| 1780. Check if Number is a Sum of Powers of Three | [Link](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | [Link](./lib/medium/1780_check_if_number_is_a_sum_of_powers_of_three.rb) | [Link](./test/medium/test_1780_check_if_number_is_a_sum_of_powers_of_three.rb) |
718+
| 1797. Design Authentication Manager | [Link](https://leetcode.com/problems/design-authentication-manager/) | [Link](./lib/medium/1797_design_authentication_manager.rb) | [Link](./test/medium/test_1797_design_authentication_manager.rb) |
718719
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
719720
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
720721
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.8.1'
8+
s.version = '8.8.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/design-authentication-manager/
4+
class AuthenticationManager
5+
# @param {Integer} time_to_live
6+
def initialize(time_to_live)
7+
@time_to_live = time_to_live
8+
@auth = {}
9+
@tokens = ::Set.new
10+
end
11+
12+
# @param {String} token_id
13+
# @param {Integer} current_time
14+
# @return {Void}
15+
def generate(token_id, current_time)
16+
@auth[token_id] = current_time + @time_to_live
17+
@tokens << token_id
18+
end
19+
20+
# @param {String} token_id
21+
# @param {Integer} current_time
22+
# @return {Void}
23+
def renew(token_id, current_time)
24+
time = @auth.fetch(token_id, 0)
25+
26+
return unless time > current_time
27+
28+
generate(token_id, current_time)
29+
30+
@tokens << token_id
31+
end
32+
33+
# @param {Integer} current_time
34+
# @return {Integer}
35+
def count_unexpired_tokens(current_time)
36+
@tokens.count { |token| @auth[token] > current_time }
37+
end
38+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1797_design_authentication_manager'
5+
require 'minitest/autorun'
6+
7+
class DesignAuthenticationManagerTest < ::Minitest::Test
8+
def test_default_one
9+
authentication_manager = ::AuthenticationManager.new(5)
10+
authentication_manager.renew('aaa', 1)
11+
authentication_manager.generate('aaa', 2)
12+
13+
assert_equal(
14+
1,
15+
authentication_manager.count_unexpired_tokens(6)
16+
)
17+
18+
authentication_manager.generate('bbb', 7)
19+
authentication_manager.renew('aaa', 8)
20+
authentication_manager.renew('bbb', 10)
21+
22+
assert_equal(
23+
0,
24+
authentication_manager.count_unexpired_tokens(15)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)