题目地址:
https://leetcode.com/problems/find-the-highest-altitude/
给定一个长 n n n数组 A A A,求其前缀和数组的最大值(前缀和数组里含长度为 0 0 0的前缀)。
代码如下:
public class Solution {
public int largestAltitude(int[] gain) {
int res = 0, sum = 0;
for (int x : gain) {
sum += x;
res = Math.max(res, sum);
}
return res;
}
}
时间复杂度 O ( n ) O(n) O(n),空间 O ( 1 ) O(1) O(1)。