aboutsummaryrefslogtreecommitdiff
path: root/other/find_highest_altitude.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/find_highest_altitude.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/find_highest_altitude.cpp')
-rw-r--r--other/find_highest_altitude.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/other/find_highest_altitude.cpp b/other/find_highest_altitude.cpp
new file mode 100644
index 0000000..c67dbac
--- /dev/null
+++ b/other/find_highest_altitude.cpp
@@ -0,0 +1,31 @@
+// 1732. Find the Highest Altitude
+#include "leetcode.h"
+
+/*
+ * 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 {
+public:
+ int largestAltitude(vector<int> &gain) {
+ int ans = INT_MIN, pre_sum = 0;
+ for (int i = 0; i < gain.size(); i++) {
+ ans = max(pre_sum, ans);
+ pre_sum += gain[i];
+ }
+ if (pre_sum > ans)
+ ans = pre_sum;
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> g1 = {-5, 1, 5, 0, -7}, g2 = {-4, -3, -2, -1, 4, 3, 2};
+ printf("%d\n", obj.largestAltitude(g1)); // expect: 1
+ printf("%d\n", obj.largestAltitude(g2)); // expect: 0
+}