From 17782654d4b1f7fb601e434b3b9c417927b6a75e Mon Sep 17 00:00:00 2001 From: fartem Date: Thu, 20 Feb 2025 09:01:32 +0300 Subject: [PATCH] 2025-02-20 v. 8.6.4: added "1472. Design Browser History" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/1472_design_browser_history.rb | 49 +++++++++++++++++++ .../test_1472_design_browser_history.rb | 25 ++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1472_design_browser_history.rb create mode 100644 test/medium/test_1472_design_browser_history.rb diff --git a/README.md b/README.md index e5592656..e525824d 100644 --- a/README.md +++ b/README.md @@ -697,6 +697,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | | 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) | | 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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 0fbfed59..c33a9ec5 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '8.6.3' + s.version = '8.6.4' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1472_design_browser_history.rb b/lib/medium/1472_design_browser_history.rb new file mode 100644 index 00000000..80cd6ec9 --- /dev/null +++ b/lib/medium/1472_design_browser_history.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/design-browser-history/ +class BrowserHistory + # @param {String} homepage + def initialize(homepage) + @history = [homepage] + @current = 0 + end + + # @param {String} url + # @return {Void} + def visit(url) + if @current < @history.size - 1 + size = @history.size - 1 + + (@current...size).each { |_| @history.pop } + end + + @history << url + @current += 1 + end + + # @param {Integer} steps + # @return {String} + def back(steps) + @current = + if @current < steps + 0 + else + @current - steps + end + + @history[@current] + end + + # @param {Integer} steps + # @return {String} + def forward(steps) + @current = + if @current + steps >= @history.size + @history.size - 1 + else + @current + steps + end + + @history[@current] + end +end diff --git a/test/medium/test_1472_design_browser_history.rb b/test/medium/test_1472_design_browser_history.rb new file mode 100644 index 00000000..22815917 --- /dev/null +++ b/test/medium/test_1472_design_browser_history.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1472_design_browser_history' +require 'minitest/autorun' + +class DesignBrowserHistoryTest < ::Minitest::Test + def test_default_one + browser_history = ::BrowserHistory.new('leetcode.com') + + browser_history.visit('google.com') + browser_history.visit('twitter.com') + browser_history.visit('youtube.com') + + assert_equal('twitter.com', browser_history.back(1)) + assert_equal('google.com', browser_history.back(1)) + assert_equal('twitter.com', browser_history.forward(1)) + + browser_history.visit('linkedin.com') + + assert_equal('linkedin.com', browser_history.forward(2)) + assert_equal('google.com', browser_history.back(2)) + assert_equal('leetcode.com', browser_history.back(7)) + end +end