LeetCode-1732.找到最高海拔(Find the Highest Altitude)

2021-02-13 極客算法

前言

問世間是否此山最高, 或者另有高處比天高

認準幾個領域,在該領域不斷的前行,等待機遇的到來

專注是一種能力,尤其是在要過年的時候

閒著也是閒著,得學點東西,學東西得學點難的

1732. 找到最高海拔

有一個自行車騎行愛好者,打算沿著公路騎行。路上有N + 1個檢查點,每個檢查點有不同的海拔高度

該騎行愛好者的起始點海拔高度為0, 給一個長度為N的海拔差值數組gain。gain[i]的值是點 i 和點 i + 1 的海拔高度差值, 其中0 <= i < n, 請返回騎手經過的最高海拔高度。

示例 1:

輸入:gain = [-5,1,5,0,-7]輸出:1解釋:海拔高度依次為 [0,-5,-4,1,1,-6] 。最高海拔為 1 。

示例 2:

輸入:gain = [-4,-3,-2,-1,4,3,2]輸出:0解釋:海拔高度依次為 [0,-4,-7,-9,-10,-6,-3,-1] 。最高海拔為 0 。

提示:

n == gain.length1 <= n <= 100-100 <= gain[i] <= 100

來源:力扣(LeetCode)

連結:https://leetcode-cn.com/problems/find-the-highest-altitude

Link:https://leetcode.com/problems/find-the-highest-altitude/

數組遍歷

這道題其實是求前綴和,找出其中最大的那個

class Solution:    def largestAltitude(self, gain: List[int]) -> int:
high = 0 pre = 0 for i in range(len(gain)): pre += gain[i] high = max(high, pre)
return high

--End--

相關焦點

  • 2018中考單詞易錯點解析:Attitude and Altitude
    Altitude     the height above sea level 海拔,高度     1. The altitude of that mountain is about 3000 meters.     那座山的海拔高度大約為3000米。
  • 數據結構與算法:05 Leetcode同步練習(一)
    Solution {    public int[] TwoSum(int[] nums, int target)     {        int[] result = new int[2];        for (int i = 0; i < nums.Length - 1; i++)        {            int find
  • 給你代碼:leetcode隨筆
    請你找到並返回這個整數example 1 :輸入:arr =[1,2,2,6,6,6,6,7,10]輸出:6來源:力扣(LeetCode)這題也不難。/*** @param {number[]} arr* @return {number}*/var findSpecialInteger = function(arr) {let last = arr[0], count = 1;if (arr.length === 1) {return
  • LeetCode145|數組中數字出現的次數II
    map.put(nums[i], 1); } } return map.entrySet() .stream() .filter(x -> x.getValue() == 1) .findFirst
  • [LeetCode] 918. Maximum Sum Circular Subarray 環形子數組的最大和
    Given a circular array C of integers represented by A, find the maximum
  • LeetCode-66.加一(Plus One)
    最高位數字存放在數組的首位, 數組中每個元素只存儲單個數字。你可以假設除了整數 0 之外,這個整數不會以零開頭。示例 1:輸入: [1,2,3]輸出: [1,2,4]解釋: 輸入數組表示數字 123。來源:力扣(LeetCode)連結:https://leetcode-cn.com/problems/plus-oneLink:https://leetcode.com/problems/plus-one/模擬加O(N)加後反轉數組分別放入個位,十位,百位...
  • leetCode第4題:Median of Two Sorted Arrays
    原題連結:https://leetcode.com/problems/median-of-two-sorted-arrays/解題思路:(1)借用findKthNumber的思想,先實現findKthNumber,如果是偶數個,則把中間2個加起來平均,奇數就用中間的。
  • ​LeetCode刷題實戰53:最大子序和
    今天和大家聊的問題叫做 最大子序和,我們先來看題面:https://leetcode-cn.com/problems/maximum-subarray/Given an integer array nums, find the contiguous subarray (containing at least one number
  • LeetCode刷題第三周【數組(簡單)】
    這裡C++可以通過冒泡排序先找到最大值及其對應的下標(索引)。而python則可以很方便的啊使用方法 max 與 index 分別取得最大值與下標。4.1 解題思路圖中是一個F(5)計算的遞歸樹(圖片來自leetcode官方[7])。
  • leetcode-406 根據身高重建隊列
    題目https://leetcode-cn.com/problems/queue-reconstruction-by-height
  • leetcode刷對了麼
    今天,逆行君就帶你從世界觀和方法論兩方面走進「leetcode」 簡單來說,leetcode是一個美國的在線編程網站,它收集了各大公司的經典算法面試題,用戶可以選擇不同的語言進行代碼的在線編寫、編譯和調試。
  • find out與find與look for的區別
    一.find out意為「查明;弄清……(情況);發現」,多指通過調查、詢問和研究之後搞清楚、弄明白某事。例如:Can you find out when the sports meeting will be held?你能查清楚運動會什麼時候開始嗎?
  • leetcode雞蛋掉落問題(egg drop)
    搜索後發現是leetcode上的一道經典面試題~因為過於經典,已經被踢出google面試題庫了(。)那我們就直接看看leetcode上的題目叭!leetcode現在有中文站,看起來更方便了:https://leetcode-cn.com/problems/super-egg-drop/solution/--你將獲得 K 個雞蛋,並可以使用一棟從 1 到 N  共有 N 層樓的建築。每個蛋的功能都是一樣的,如果一個蛋碎了,你就不能再把它掉下去。
  • 中國海拔最高的山峰排行!
    中國最高的山峰,是海拔高度為8844.43米的珠穆朗瑪峰,也是世界第一高峰。那麼中國山峰高度排名詳情如何?入選榜單的山峰,                                                                                有排名第二的海拔高度為8611米的K2,第三是海拔高度為8516米的洛子峰……以下為大家盤點中國十大最高山峰,帶來中國海拔最高的山峰排名,希望對大家有所幫助。
  • 每天一道leetcode234-回文鍊表
    正當班主任要繼續發話,只聽到角落默默想起來一個聲音:」K2」前言 2018.11.6號打卡明天的題目:https://leetcode-cn.com/problems/remove-linked-list-elements/以後明天的題目提取公布一下哈,因為有些朋友想提前做一下~題目 leetcode234-回文鍊表中文連結:https