728x90
1. 문제
2. 요구사항
1) 고도 변화 기록을 나타내는 정수 리스트 gain이 주어진다.
2) gain[i]는 i번째 구간에서 얻은 고도 변화를 의미한다.
3) 자전거 타는 사람이 처음 고도 0에서 시작한다고 할 때, 가장 높은 고도를 구한다.
4) 처음 고도는 0이다.
3. 코드
class Solution {
int largestAltitude(List<int> gain) {
int maxAltitude = 0; // 최대 고도를 저장할 변수, 초기값은 0
int currentAltitude = 0; // 현재 고도를 추적하는 변수
for (int i = 0; i < gain.length; i++) {
currentAltitude += gain[i]; // 현재 고도에 gain[i]만큼 변화 적용
if (maxAltitude < currentAltitude) {
maxAltitude = currentAltitude; // 더 높은 고도를 발견하면 maxAltitude 갱신
}
}
return maxAltitude;
}
}728x90
'Programming > LeetCode' 카테고리의 다른 글
| [Dart] 3. Longest Substring Without Repeating Characters (0) | 2025.05.08 |
|---|---|
| [Dart] LeetCode 2215. Find the Difference of Two Arrays (0) | 2025.05.07 |
| [Dart] LeetCode 1431. Kids With the Greatest Number of Candies (0) | 2025.05.01 |
| [Dart] 1207. Unique Number of Occurrences (0) | 2025.04.29 |
| [Dart] 1071. Greatest Common Divisor of Strings (0) | 2025.04.25 |