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 gaini 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 <= gaini <= 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());
    }
};
相关推荐
fqbqrr2 小时前
2606C++,C++构的多态
开发语言·c++
小欣加油2 小时前
leetcode56 合并区间
c++·算法·leetcode·职场和发展
lqqjuly2 小时前
前沿算法深度解析(二)
人工智能·算法·机器学习
Yolo_TvT3 小时前
C++:析构函数
c++
徐小夕4 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
akunkuntaimei4 小时前
2026年高考数学各省真题及答案(完整版)
算法·高考
Hello:CodeWorld5 小时前
C 风格变参 vs C++ 变参模板:核心区别与选型指南
c语言·c++·算法
8Qi86 小时前
LeetCode 516:最长回文子序列
算法·leetcode·职场和发展·动态规划
搬砖魁首7 小时前
基础能力系列 - 多线程2 - 条件变量
c++·rust·条件变量·原子类型·线程同步互斥
youngerwang7 小时前
【从搬运工到协处理器:网卡芯片架构、算法、验证与边缘演进深度剖析】
网络·算法·架构·芯片