当前位置: 首页 > 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/a/106793.html

相关文章:

  • 生成自签名证书并配置 HTTPS 使用自签名证书
  • 官方压测工具memtier-benchmark压测redis
  • 微信小程序自定义顶部导航栏(适配各种机型)
  • Keil基于ARM Compiler 5的工程迁移为ARM Compiler 6的工程
  • ⾃动化运维利器Ansible-基础
  • C++中的std::tuple和std::pair
  • 浏览器下载视频插件使用
  • 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技术