commit 4c4549eedbe400b9311a0cc67215baeca7c3eb8f
parent 63ae98196da1bf92088c2cead76fcf69ab39d48c
Author: Jack Sangdahl <jack@pngu.org>
Date: Fri, 19 Jun 2026 08:16:50 -0600
1732. find the highest altitude
Diffstat:
2 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/find_highest_altitude.c b/find_highest_altitude.c
@@ -1,7 +1,5 @@
// 1732. Find the Highest Altitude
-#include <limits.h>
-#include <math.h>
-#include <stdio.h>
+#include "leetcode.h"
/*
* there is a biker going on a road trip. the road trip consists of 'n + 1'
@@ -11,19 +9,23 @@
* return the highest altitude of a point
*/
-int largestAltitude(int *gain, int gain_size) {
- int ans = INT_MIN, pre_sum = 0;
- for (int i = 0; i < gain_size; i++) {
- ans = fmax(pre_sum, ans);
- pre_sum += gain[i];
+int largestAltitude(int *gain, int gainSize) {
+ int ans = 0, sum = 0;
+ for (int i = 0; i < gainSize; i++) {
+ sum += gain[i];
+ int diff = sum - ans;
+ ans += diff & ~(diff >> 0x1F);
}
- if (pre_sum > ans)
- ans = pre_sum;
return ans;
}
int main() {
- int g1[] = {-5, 1, 5, 0, -7}, g2[] = {-4, -3, -2, -1, 4, 3, 2};
- printf("%d\n", largestAltitude(g1, 5)); // expect: 1
- printf("%d\n", largestAltitude(g2, 7)); // expect: 0
+ int g1[] = {-5, 1, 5, 0, -7};
+ int g2[] = {-4, -3, -2, -1, 4, 3, 2};
+ int r1 = largestAltitude(g1, ARRAY_SIZE(g1));
+ int r2 = largestAltitude(g2, ARRAY_SIZE(g2));
+ printf("%d\n", r1);
+ assert(r1 == 1);
+ printf("%d\n", r2);
+ assert(r2 == 0);
}
diff --git a/find_highest_altitude.py b/find_highest_altitude.py
@@ -0,0 +1,22 @@
+# 1732. Find the Highest Altitude
+from itertools import accumulate
+from typing import List
+
+"""
+there is a biker going on a road trip. the road trip consists of 'n + 1'
+points at different altitudes. the biker starts his trip on point 0 with
+altitude 0. given an integer array 'ain' of length 'n' where 'gain[i]' is the
+net gain in altidude between points 'i' and 'i + 1' for all '0 <= i < n'.
+return the highest altitude of a point
+"""
+
+
+class Solution:
+ def largestAltitude(self, gain: List[int]) -> int:
+ return max(0, max(accumulate(gain)))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.largestAltitude(gain=[-5, 1, 5, 0, -7]))
+ print(obj.largestAltitude(gain=[-4, -3, -2, -1, 4, 3, 2]))