Re: [閒聊] 每日LeetCode已回收
https://leetcode.com/problems/find-the-highest-altitude/description/
1732. Find the Highest Altitude
有一個自行車手從高度為0的位置開始騎車,他要經過 n 個點,每個點的海拔
距離上個點為 gain[i],求出他騎到終點後最高海拔是多少。
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.
思路:
1.從0開始不斷計算新的點的海拔高度,並更新最大值。
Java Code:
----------------------------------------
class Solution {
public int largestAltitude(int[] gain) {
int res = 0;
int height = 0;
for (int h : gain) {
height += h;
res = Math.max(res, height);
}
return res;
}
}
----------------------------------------
我這輩子只寫得出easy了 紫鯊
--
https://i.imgur.com/PIoxddO.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.75.86 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1687180328.A.827.html
推
06/19 21:12,
2年前
, 1F
06/19 21:12, 1F
→
06/19 21:12,
2年前
, 2F
06/19 21:12, 2F
→
06/19 21:13,
2年前
, 3F
06/19 21:13, 3F
推
06/20 09:40,
2年前
, 4F
06/20 09:40, 4F
→
06/20 09:40,
2年前
, 5F
06/20 09:40, 5F
※ 編輯: Rushia (122.100.75.86 臺灣), 06/21/2023 00:32:19
討論串 (同標題文章)
完整討論串 (本文為第 351 之 719 篇):