Skip to content

Commit 2cc6509

Browse files
committed
leetcode
1 parent 3087452 commit 2cc6509

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
4+
-* 1732. Find the Highest Altitude *-
5+
6+
7+
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.
8+
9+
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.
10+
11+
12+
13+
Example 1:
14+
15+
Input: gain = [-5,1,5,0,-7]
16+
Output: 1
17+
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
18+
Example 2:
19+
20+
Input: gain = [-4,-3,-2,-1,4,3,2]
21+
Output: 0
22+
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
23+
24+
25+
Constraints:
26+
27+
n == gain.length
28+
1 <= n <= 100
29+
-100 <= gain[i] <= 100
30+
31+
32+
*/
33+
34+
import 'dart:math';
35+
36+
class Solution {
37+
int largestAltitude(List<int> gain) {
38+
int currentAltitude = 0;
39+
int maxAltitude = 0;
40+
for (int i = 0; i < gain.length; i++) {
41+
currentAltitude = gain[i];
42+
maxAltitude = max(maxAltitude, currentAltitude);
43+
}
44+
return maxAltitude;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
func largestAltitude(gain []int) int {
4+
var maxAltitude int = 0
5+
var currentAltitude int = 0
6+
7+
for i := 0; i < len(gain); i++ {
8+
currentAltitude += gain[i]
9+
maxAltitude = max(maxAltitude, currentAltitude)
10+
}
11+
return maxAltitude
12+
13+
}
14+
15+
func max(first int, second int) int {
16+
if first > second {
17+
return first
18+
}
19+
return second
20+
}

FindTheHighestAltitude/find_the_highest_altitude.md

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
233233
- [**2352.** Equal Row and Column Pairs](EqualRowAndColumnPairs/equal_row_and_column_pairs.dart)
234234
- [**1569.** Number of Ways to Reorder Array to Get Same BST](NumberOfWaysToReorderArrayToGetSameBst/number_of_ways_to_reorder_array_to_get_same_bst.dart)
235235
- [**1187.** Make Array Strictly Increasing](MakeArrayStrictlyIncreasing\make_array_strictly_increasing.dart)
236+
- [**1732.** Find the Highest Altitude](FindTheHighestAltitude/find_the_highest_altitude.dart)
236237

237238
## Reach me via
238239

0 commit comments

Comments
 (0)