New Problem Solution - "Find the Highest Altitude"
This commit is contained in:
parent
bbe0ead70a
commit
1483a6e992
@ -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|
|
||||
|
@ -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<int>& gain) {
|
||||
int result = 0;
|
||||
int sum = 0;
|
||||
for (auto& n : gain) {
|
||||
sum += n;
|
||||
result = max(result, sum);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user