LeetCode75——Day18

文章目录

一、题目

1732. Find the Highest Altitude

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

二、题解

利用前缀和的思想解决,使用*max_element()函数可以返回vector中的最大值。

cpp 复制代码
class Solution {
public:
    int largestAltitude(vector<int>& gain) {
        int n = gain.size();
        vector<int> prefixSum(n + 1,0);
        for(int i = 1;i <= n;i++){
            prefixSum[i] = prefixSum[i-1] + gain[i-1];
        }
        return *max_element(prefixSum.begin(),prefixSum.end());
    }
};
相关推荐
yunhuibin1 小时前
无锁化编程——c++内存序使用
c++
zzzyyy5383 小时前
C++之vector容器
开发语言·c++
小安同学iter4 小时前
SQL50+Hot100系列(11.9)
算法·leetcode·职场和发展
uotqwkn89469s4 小时前
如果Visual Studio不支持C++14,应该如何解决?
c++·ide·visual studio
炼金士5 小时前
基于多智能体技术的码头车辆最快行驶路径方案重构
算法·路径规划·集装箱码头
Maple_land5 小时前
Linux复习:冯·诺依曼体系下的计算机本质:存储分级与IO效率的底层逻辑
linux·运维·服务器·c++·centos
ue星空5 小时前
UE核心架构概念
网络·c++·ue5
小刘max6 小时前
最长递增子序列(LIS)详解:从 dp[i] 到 O(n²) 动态规划
算法·动态规划
王璐WL7 小时前
【数据结构】双向链表
数据结构
谢景行^顾7 小时前
数据结构知识掌握
linux·数据结构·算法