Notice
250x250
Recent Posts
Recent Comments
Link
넘치게 채우기
[LeetCode] 1732. Find the Highest Altitude 본문
728x90
반응형
https://leetcode.com/problems/find-the-highest-altitude/description/
문제 난이도 : Easy
문제
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.
높이 증감값 배열 gain이 주어진다. 가장 높은 고도를 구하여라.
풀이
현재 고도와 가장 높은 고도를 저장하는 변수를 담아서 비교해주면 된다.
코드(C++)
class Solution {
public:
int largestAltitude(vector<int>& gain) {
int answer = 0;
int current_height = 0;
for(int i = 0; i < gain.size(); i++){
current_height += gain[i];
answer = max(current_height, answer);
}
return answer;
}
};
728x90
반응형
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] 58. Length of Last Word (0) | 2023.06.27 |
---|---|
[LeetCode] 121. Best Time to Buy and Sell Stock (0) | 2023.06.26 |
[LeetCode] 215. Kth Largest Element in an Array (0) | 2023.06.09 |
[LeetCode] 1351. Count Negative Numbers in a Sorted Matrix (0) | 2023.06.08 |
[LeetCode] 1502. Can Make Arithmetic Progression From Sequence (0) | 2023.06.06 |