Skip to content

Commit 4c94420

Browse files
committedMay 5, 2023
1732. Find the Highest Altitude
1 parent 9f4662e commit 4c94420

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
| 1680 | Concatenation of Consecutive Binary Numbers | [Ruby](./algorithms/ruby/1680-concatenation-of-consecutive-binary-numbers.rb) | Medium |
176176
| 1697 | Checking Existence of Edge Length Limited Paths | [Ruby](./algorithms/ruby/1697-checking-existence-of-edge-length-limited-paths.rb) | Hard |
177177
| 1706 | Where Will the Ball Fall | [Ruby](./algorithms/ruby/1706-where-will-the-ball-fall.rb) | Medium |
178+
| 1732 | Find the Highest Altitude | [Ruby](./algorithms/ruby/1732-find-the-highest-altitude.rb) | Easy |
178179
| 1768 | Merge Strings Alternately | [Ruby](./algorithms/ruby/1768-merge-strings-alternately.rb) | Easy |
179180
| 1822 | Sign of the Product of an Array | [Ruby](./algorithms/ruby/1822-sign-of-the-product-of-an-array.rb) | Easy |
180181
| 1857 | Largest Color Value in a Directed Graph | [Ruby](./algorithms/ruby/1857-largest-color-value-in-a-directed-graph.rb) | Hard |

‎algorithms/ruby/0724-find-pivot-index.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
# 724. Find Pivot Index
44
# https://leetcode.com/problems/find-pivot-index
5+
# Easy
56

67
=begin
7-
88
Given an array of integers nums, calculate the pivot index of this array.
99
1010
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
@@ -38,7 +38,6 @@
3838
### Constraints:
3939
* 1 <= nums.length <= 104
4040
* -1000 <= nums[i] <= 1000
41-
4241
=end
4342

4443
# Runtime 109 ms
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
# 1732. Find the Highest Altitude
4+
# https://leetcode.com/problems/find-the-highest-altitude
5+
# Easy
6+
7+
=begin
8+
There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.
9+
10+
You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). Return the highest altitude of a point.
11+
12+
Example 1:
13+
Input: gain = [-5,1,5,0,-7]
14+
Output: 1
15+
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
16+
17+
Example 2:
18+
Input: gain = [-4,-3,-2,-1,4,3,2]
19+
Output: 0
20+
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
21+
22+
Constraints:
23+
n == gain.length
24+
1 <= n <= 100
25+
-100 <= gain[i] <= 100
26+
=end
27+
28+
# @param {Integer[]} gain
29+
# @return {Integer}
30+
def largest_altitude(gain)
31+
prefix = gain.inject([]) { |acc, value| acc << acc.last.to_i + value.to_i }
32+
max_gain = prefix.max
33+
max_gain < 0 ? 0 : max_gain
34+
end
35+
36+
# **************** #
37+
# TEST #
38+
# **************** #
39+
40+
require "test/unit"
41+
class Test_largest_altitude < Test::Unit::TestCase
42+
def test_
43+
assert_equal 1, largest_altitude([-5, 1, 5, 0, -7])
44+
assert_equal 0, largest_altitude([-4, -3, -2, -1, 4, 3, 2])
45+
end
46+
end

0 commit comments

Comments
 (0)
Please sign in to comment.