[LeetCode] Find the Highest Altitude
코테연습2025. 4. 21. 20:57
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.
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.
Example 1:
Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
Example 2:
Input: gain = [-4,-3,-2,-1,4,3,2]
Output: 0
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
Constraints:
n == gain.length
1 <= n <= 100
-100 <= gain[i] <= 100
const largestAltitude = function(gain) {
// 자전거 여행을 갈건데 그 경로는 각기 다른 고도를 갖는 n+1개의 포인트를 갖고 있다.
// 바이커는 고도가 0인 포인트 0에서 출발한다.
// n의 길이를 갖는 gain이라는 배열이 존재하고 순 고도 증가량을 갖는 정수 배열이 주어진다.
// 가장 높은 고도를 반환
let init = 0;
let result = 0;
for(let i=0; i < gain.length; i++){
init += gain[i];
result = init > result ? init : result;
}
return result > 0 ? result : 0 ;
};
넘 별 거 없는 문제라서 올리기도 머쓱하나...
이래저래 여유가 없는 날이었는데 쉬운 단계라도 하나 풀었다는 걸 남기기 위해~.~
'코테연습' 카테고리의 다른 글
[LeetCode] Find Pivot Index (0) | 2025.04.22 |
---|---|
[LeetCode] Longest Subarray of 1's After Deleting One Element (2) | 2025.04.17 |
[LeetCode] Max Consecutive Ones III (0) | 2025.04.16 |
댓글()