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

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.lryc.cn/news/208887.html

相关文章:

  • Java NIO 高并发开发
  • 8.循环神经网络
  • [C++随想录] map和set的使用
  • 公网IP怎么设置?公网ip有哪些优点和缺点?
  • 蓝桥杯第 2 场算法双周赛 第2题 铺地板【算法赛】c++ 数学思维
  • APScheduler-调度器 BackgroundScheduler
  • 浅谈UI自动化测试
  • golang 工程组件 grpc-gateway—yaml定义http规则,和自定义实现网关路由
  • 在NLP中一下常见的任务,可以用作baseline;MRPC,CoLA,STS-B,RTE
  • 【计算机网络笔记】Cookie技术
  • 在虚拟环境中,通过pip安装tensorflow
  • 【Django restframework】django跨域问题,解决PUT/PATCH/DELETE用ajax请求无法提交数据的问题
  • 神经网络与深度学习第四章前馈神经网络习题解答
  • Go 语言操作 MongoDb
  • UE4/5 竖排文字文本
  • centos jdk 安装
  • 【计算机网络】什么是HTTPS?HTTPS为什么是安全的?
  • Windows-Oracle19c 安装详解-含Navicate远程连接配置 - 同时连接Oracle11g和Oracle19c
  • 文件权限详解
  • 在声明和定义的一些小坑
  • 浏览器事件循环 (event loop)
  • P1868 饥饿的奶牛
  • 【软考系统架构设计师】2021年系统架构师综合知识真题及解析
  • 如何在忘记手机密码或图案时重置 Android 手机?
  • LeetCode每日一题——2520. Count the Digits That Divide a Number
  • 论文阅读——DistilBERT
  • 202212 青少年等级考试机器人实操真题三级
  • token正确不报错,token失效后却出现报跨域错误
  • STM32中除零运算,为何程序不崩溃?
  • sprinbboot 2.7启动不生成日志文件