当前位置: 首页 > article >正文

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中的最大值。

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());
    }
};

http://www.kler.cn/news/106793.html

相关文章:

  • 浏览器下载视频插件使用
  • postgis ST_ClipByBox2D用法
  • centos7安装mysql
  • asp.net core获取config和env
  • 推荐一本书《变速领导力》
  • 论文阅读 - Learning Human Interactions with the Influence Model
  • Go 语言操作 MongoDb
  • 23 行为型模式-迭代器模式
  • node实战——搭建带swagger接口文档的后端koa项目(node后端就业储备知识)
  • 使用pycharm远程调试
  • MySQL视图的使用和优化
  • Spring Cloud之微服务
  • Milvus 入门教程
  • 机器学习笔记:逆置换
  • 鸿蒙ArkUI-X跨端应用开发,一套代码构建多平台应用
  • Day38 Qchart绘制灰度直方图
  • C#序列化与反序列化详解
  • 04-流媒体-ffmpeg.c源码分析
  • Corel Products Keygen-X-FORCE 2023(Corel会声会影2023注册机)
  • 【计算机网络笔记】Cookie技术
  • B F C
  • 浏览器事件循环 (event loop)
  • Centos安装gitlabce
  • Go学习第十章——文件操作,Json和测试
  • CVE-2021-41773/42013 apache路径穿越漏洞
  • Unity - 导出的FBX模型,无法将 vector4 保存在 uv 中(使用 Unity Mesh 保存即可)
  • 【蓝桥每日一题]-前缀和与差分(保姆级教程 篇1)
  • 拷贝音频、视频、word等二进制文件的实现方法,不掉帧
  • 业务设计——分库分表下多种登录方式实现【用户名、邮箱、手机号】
  • [17]JAVAEE-HTTP协议