Processing math: 100%

Leetcode 1732 - Find the Highest Altitude

題目

Problem#

有一個人從 0 開始往上走,給你一個 gain 整數陣列,其中 gain[i] 代表 ii+1 的相差,問你他走完全程,途中最大的高度是多少?

測資限制#

  • 1n100
  • 100gain[i]100

想法#

掃一遍,累加找最大

  • 時間複雜度: O(n)
  • 空間複雜度: O(1)

AC Code#

Copy
class Solution {
public:
int largestAltitude(vector<int>& gain) {
int tmp = 0;
int ans = 0;
for(int i : gain)
{
tmp += i;
ans = max(ans, tmp);
}
return ans;
}
};
view raw leetcode/1732.cpp delivered with ❤ by emgithub