Skip to content

Commit 5da2693

Browse files
authored
2025-02-20 v. 8.6.4: added "1472. Design Browser History"
2 parents 42fc663 + 1778265 commit 5da2693

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
697697
| 1448. Count Good Nodes in Binary Tree | [Link](https://leetcode.com/problems/count-good-nodes-in-binary-tree/) | [Link](./lib/medium/1448_count_good_nodes_in_binary_tree.rb) | [Link](./test/medium/test_1448_count_good_nodes_in_binary_tree.rb) |
698698
| 1457. Pseudo-Palindromic Paths in a Binary Tree | [Link](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | [Link](./lib/medium/1457_pseudo_palindromic_paths_in_a_binary_tree.rb) | [Link](./test/medium/test_1457_pseudo_palindromic_paths_in_a_binary_tree.rb) |
699699
| 1461. Check If a String Contains All Binary Codes of Size K | [Link](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) | [Link](./lib/medium/1461_check_if_a_string_contains_all_binary_codes_of_size_k.rb) | [Link](./test/medium/test_1461_check_if_a_string_contains_all_binary_codes_of_size_k.rb) |
700+
| 1472. Design Browser History | [Link](https://leetcode.com/problems/design-browser-history/) | [Link](./lib/medium/1472_design_browser_history.rb) | [Link](./test/medium/test_1472_design_browser_history.rb) |
700701
| 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) |
701702
| 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) |
702703
| 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

Lines changed: 1 addition & 1 deletion
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.6.3'
8+
s.version = '8.6.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/design-browser-history/
4+
class BrowserHistory
5+
# @param {String} homepage
6+
def initialize(homepage)
7+
@history = [homepage]
8+
@current = 0
9+
end
10+
11+
# @param {String} url
12+
# @return {Void}
13+
def visit(url)
14+
if @current < @history.size - 1
15+
size = @history.size - 1
16+
17+
(@current...size).each { |_| @history.pop }
18+
end
19+
20+
@history << url
21+
@current += 1
22+
end
23+
24+
# @param {Integer} steps
25+
# @return {String}
26+
def back(steps)
27+
@current =
28+
if @current < steps
29+
0
30+
else
31+
@current - steps
32+
end
33+
34+
@history[@current]
35+
end
36+
37+
# @param {Integer} steps
38+
# @return {String}
39+
def forward(steps)
40+
@current =
41+
if @current + steps >= @history.size
42+
@history.size - 1
43+
else
44+
@current + steps
45+
end
46+
47+
@history[@current]
48+
end
49+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1472_design_browser_history'
5+
require 'minitest/autorun'
6+
7+
class DesignBrowserHistoryTest < ::Minitest::Test
8+
def test_default_one
9+
browser_history = ::BrowserHistory.new('leetcode.com')
10+
11+
browser_history.visit('google.com')
12+
browser_history.visit('twitter.com')
13+
browser_history.visit('youtube.com')
14+
15+
assert_equal('twitter.com', browser_history.back(1))
16+
assert_equal('google.com', browser_history.back(1))
17+
assert_equal('twitter.com', browser_history.forward(1))
18+
19+
browser_history.visit('linkedin.com')
20+
21+
assert_equal('linkedin.com', browser_history.forward(2))
22+
assert_equal('google.com', browser_history.back(2))
23+
assert_equal('leetcode.com', browser_history.back(7))
24+
end
25+
end

0 commit comments

Comments
 (0)