diff --git a/README.md b/README.md index 752de644..1984f94d 100644 --- a/README.md +++ b/README.md @@ -626,6 +626,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 817. Linked List Components | [Link](https://leetcode.com/problems/linked-list-components/) | [Link](./lib/medium/817_linked_list_components.rb) | [Link](./test/medium/test_817_linked_list_components.rb) | | 823. Binary Trees With Factors | [Link](https://leetcode.com/problems/binary-trees-with-factors/) | [Link](./lib/medium/823_binary_trees_with_factors.rb) | [Link](./test/medium/test_823_binary_trees_with_factors.rb) | | 841. Keys and Rooms | [Link](https://leetcode.com/problems/keys-and-rooms/) | [Link](./lib/medium/841_keys_and_rooms.rb) | [Link](./test/medium/test_841_keys_and_rooms.rb) | +| 852. Peak Index in a Mountain Array | [Link](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Link](./lib/medium/852_peak_index_in_a_mountain_array.rb) | [Link](./test/medium/test_852_peak_index_in_a_mountain_array.rb) | ### Hard diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 3a72ce84..44e245c6 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 = '7.6.5' + s.version = '7.6.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/852_peak_index_in_a_mountain_array.rb b/lib/medium/852_peak_index_in_a_mountain_array.rb new file mode 100644 index 00000000..2936672d --- /dev/null +++ b/lib/medium/852_peak_index_in_a_mountain_array.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/peak-index-in-a-mountain-array/ +# @param {Integer[]} arr +# @return {Integer} +def peak_index_in_mountain_array(arr) + result = 0 + max = 0 + arr.each_with_index do |num, i| + next unless num > max + + max = num + result = i + end + + result +end diff --git a/test/medium/test_852_peak_index_in_a_mountain_array.rb b/test/medium/test_852_peak_index_in_a_mountain_array.rb new file mode 100644 index 00000000..b71153c4 --- /dev/null +++ b/test/medium/test_852_peak_index_in_a_mountain_array.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/852_peak_index_in_a_mountain_array' +require 'minitest/autorun' + +class PeakIndexInAMountainArrayTest < ::Minitest::Test + def test_default_one + assert_equal( + 1, + peak_index_in_mountain_array( + [0, 1, 0] + ) + ) + end + + def test_default_two + assert_equal( + 1, + peak_index_in_mountain_array( + [0, 2, 1, 0] + ) + ) + end + + def test_default_three + assert_equal( + 1, + peak_index_in_mountain_array( + [0, 10, 5, 2] + ) + ) + end +end