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());
    }
};
相关推荐
fashion 道格3 分钟前
从地图导航到数据结构:解锁带权有向图的邻接链表奥秘
c语言·数据结构·链表
2401_893326626 分钟前
力扣1971.寻找图中是否存在路径
算法·leetcode·职场和发展
专注API从业者9 分钟前
构建分布式京东商品数据采集器:基于微服务的架构设计与实现
数据结构·数据库·分布式·微服务·架构
zs宝来了15 分钟前
HOT100-技巧类型题
数据结构·算法
Ayanami_Reii15 分钟前
进阶数据结构-FenwickTree
数据结构·算法·树状数组·fenwick tree
chenyuhao202419 分钟前
MySQL事务
开发语言·数据库·c++·后端·mysql
爪哇部落算法小助手22 分钟前
每日两题day59
数据结构·c++·算法
D_evil__30 分钟前
[C++高频精进] 现代C++特性:右值引用和移动语义
c++
Brduino脑机接口技术答疑30 分钟前
脑机接口数据处理连载(二) 数据源头解析:脑电信号的采集原理与数据特性
人工智能·python·算法·数据分析·脑机接口
吃着火锅x唱着歌36 分钟前
LeetCode 1010.总持续时间可被60整除的歌曲
算法·leetcode·职场和发展