{"id":8003,"date":"2021-01-23T11:27:40","date_gmt":"2021-01-23T19:27:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8003"},"modified":"2021-01-23T11:28:14","modified_gmt":"2021-01-23T19:28:14","slug":"leetcode1732-find-the-highest-altitude","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode1732-find-the-highest-altitude\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode1732. Find the Highest Altitude"},"content":{"rendered":"\n<p>There is a biker going on a road trip. The road trip consists of&nbsp;<code>n + 1<\/code>&nbsp;points at different altitudes. The biker starts his trip on point&nbsp;<code>0<\/code>&nbsp;with altitude equal&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p>You are given an integer array&nbsp;<code>gain<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where&nbsp;<code>gain[i]<\/code>&nbsp;is the&nbsp;<strong>net gain in altitude<\/strong>&nbsp;between points&nbsp;<code>i<\/code>\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;<code>i + 1<\/code>&nbsp;for all (<code>0 &lt;= i &lt; n)<\/code>. Return&nbsp;<em>the&nbsp;<strong>highest altitude<\/strong>&nbsp;of a point.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> gain = [-5,1,5,0,-7]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> gain = [-4,-3,-2,-1,4,3,2]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == gain.length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>-100 &lt;= gain[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Running Max<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int largestAltitude(vector<int>& gain) {\n    int ans = 0;\n    int cur = 0;\n    for (int diff : gain)\n      ans = max(ans, cur += diff);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a biker going on a road trip. The road trip consists of&nbsp;n + 1&nbsp;points at different altitudes. The biker starts his trip on&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,141],"class_list":["post-8003","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-max","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8003"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8003\/revisions"}],"predecessor-version":[{"id":8006,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8003\/revisions\/8006"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}