diff --git a/README.md b/README.md index 9cd5c0c..6dafb12 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ LeetCode |1750|[Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) | [C++](./algorithms/cpp/minimumLengthOfStringAfterDeletingSimilarEnds/MinimumLengthOfStringAfterDeletingSimilarEnds.cpp)|Medium| |1749|[Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/) | [C++](./algorithms/cpp/maximumAbsoluteSumOfAnySubarray/MaximumAbsoluteSumOfAnySubarray.cpp)|Medium| |1748|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/) | [C++](./algorithms/cpp/sumOfUniqueElements/SumOfUniqueElements.cpp)|Easy| +|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [C++](./algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp)|Easy| |1605|[Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](./algorithms/cpp/FindValidMatrixGivenRowAndColumnSums/FindValidMatrixGivenRowAndColumnSums.cpp)|Medium| |1573|[Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](./algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp)|Medium| |1556|[Thousand Separator](https://leetcode.com/problems/thousand-separator/) | [C++](./algorithms/cpp/thousandSeparator/ThousandSeparator.cpp)|Easy| diff --git a/algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp b/algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp new file mode 100644 index 0000000..ec382d9 --- /dev/null +++ b/algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp @@ -0,0 +1,43 @@ +// Source : https://leetcode.com/problems/find-the-highest-altitude/ +// Author : Hao Chen +// Date : 2021-02-17 + +/***************************************************************************************************** + * + * 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 equal 0. + * + * You are given an integer array gain of length n where gain[i] is the net gain in altitude between + * points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point. + * + * 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. + * + * Constraints: + * + * n == gain.length + * 1 <= n <= 100 + * -100 <= gain[i] <= 100 + ******************************************************************************************************/ + +class Solution { +public: + int largestAltitude(vector& gain) { + int result = 0; + int sum = 0; + for (auto& n : gain) { + sum += n; + result = max(result, sum); + } + return result; + } +};