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());
    }
};
相关推荐
一拳一个呆瓜1 天前
【MFC】对话框属性:字体 (Font Name) 和 大小 (Font Size)
c++·mfc
etcix1 天前
dmenux.c: integrate dmenu project as one file
c语言·前端·算法
papership1 天前
【入门级-算法-6、排序算法:选择排序】
数据结构·算法·排序算法
郝学胜-神的一滴1 天前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
啊?啊?1 天前
14 C++ STL 容器实战:stack/list 模拟实现指南 + priority_queue 用法及避坑技巧
c++·
汉克老师1 天前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(4、最大空白区)
c++·算法·蓝桥杯·蓝桥杯c++·c++蓝桥杯
共享家95271 天前
优先搜索(DFS)实战
算法·leetcode·深度优先
一只懒洋洋1 天前
中值滤波、方框滤波、高斯滤波、均值滤波、膨胀、腐蚀、开运算、闭运算
算法·均值算法
shellvon1 天前
你怎么被识别的?从TLS到Canvas的设备追踪术
后端·算法
薛定谔的算法1 天前
JavaScript栈的实现与应用:从基础到实战
前端·javascript·算法