Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
17 changes: 17 additions & 0 deletions lib/medium/852_peak_index_in_a_mountain_array.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions test/medium/test_852_peak_index_in_a_mountain_array.rb
Original file line number Diff line number Diff line change
@@ -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
Loading