diff --git a/README.md b/README.md index 77d82dc..1fcd44e 100644 --- a/README.md +++ b/README.md @@ -466,6 +466,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 3151. Special Array I | [Link](https://leetcode.com/problems/special-array-i/) | [Link](./lib/easy/3151_special_array_i.rb) | [Link](./test/easy/test_3151_special_array_i.rb) | | 3210. Find the Encrypted String | [Link](https://leetcode.com/problems/find-the-encrypted-string/) | [Link](./lib/easy/3210_find_the_encrypted_string.rb) | [Link](./test/easy/test_3210_find_the_encrypted_string.rb) | | 3280. Convert Date to Binary | [Link](https://leetcode.com/problems/convert-date-to-binary/) | [Link](./lib/easy/3280_convert_date_to_binary.rb) | [Link](./test/easy/test_3280_convert_date_to_binary.rb) | +| 3386. Button with Longest Push Time | [Link](https://leetcode.com/problems/button-with-longest-push-time/) | [Link](./lib/easy/3386_button_with_longest_push_time.rb) | [Link](./test/easy/test_3386_button_with_longest_push_time.rb) | | 3498. Reverse Degree of a String | [Link](https://leetcode.com/problems/reverse-degree-of-a-string/) | [Link](./lib/easy/3498_reverse_degree_of_a_string.rb) | [Link](./test/easy/test_3498_reverse_degree_of_a_string.rb) | | 3516. Find Closest Person | [Link](https://leetcode.com/problems/find-closest-person/) | [Link](./lib/easy/3516_find_closest_person.rb) | [Link](./test/easy/test_3516_find_closest_person.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index f3bf9e6..f0cee8b 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 = '9.3.6' + s.version = '9.3.7' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/easy/3386_button_with_longest_push_time.rb b/lib/easy/3386_button_with_longest_push_time.rb new file mode 100644 index 0000000..3221077 --- /dev/null +++ b/lib/easy/3386_button_with_longest_push_time.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/button-with-longest-push-time/ +# @param {Integer[][]} events +# @return {Integer} +def button_with_longest_time(events) + max_time = events[0][1] + result = events[0][0] + + (1...events.size).each do |i| + press_time = events[i][1] - events[i - 1][1] + + next unless press_time > max_time || press_time == max_time && events[i][0] < result + + max_time = press_time + + result = events[i][0] + end + + result +end diff --git a/test/easy/test_3386_button_with_longest_push_time.rb b/test/easy/test_3386_button_with_longest_push_time.rb new file mode 100644 index 0000000..9e8b3d6 --- /dev/null +++ b/test/easy/test_3386_button_with_longest_push_time.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/3386_button_with_longest_push_time' +require 'minitest/autorun' + +class ButtonWithLongestPushTimeTest < ::Minitest::Test + def test_default_one + assert_equal( + 1, + button_with_longest_time( + [ + [1, 2], + [2, 5], + [3, 9], + [1, 15] + ] + ) + ) + end + + def test_default_two + assert_equal( + 10, + button_with_longest_time( + [ + [10, 5], + [1, 7] + ] + ) + ) + end +end