作者Rushia (みけねこ的鼻屎)
看板Marginalman
標題Re: [閒聊] 每日LeetCode
時間Mon Jun 19 21:12:06 2023
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
推 JIWP: 大師 06/19 21:12
→ JIWP: 你不是都hard起手 06/19 21:12
→ Rushia: 我hard題數只有20 對不起 06/19 21:13
推 NTHUlagka: 你應該嘗試開始刷hard?感覺medium的題目對你已經沒難 06/20 09:40
→ NTHUlagka: 度了 06/20 09:40
※ 編輯: Rushia (122.100.75.86 臺灣), 06/21/2023 00:32:19