[LeetCode]121. Best Time to Buy and Sell Stock

Python实战:用nltk库5步搭建你的第一个n-gram文本生成器(附古诗生成案例) 本文详细介绍了如何使用Python的nltk库5步搭建n-gram文本生成器,并附有古诗生成案例。从环境准备、n-gram原理讲解到完整实现,逐步指导开发者掌握自然语言生成技术。特别针对中文古诗生成进行了优化,包括分词处理和韵律控制,帮助读者快速上手文本生成项目。 阅读详情

[LeetCode]121. Best Time to Buy and Sell Stock

题目描述

这里写图片描述

思路

  1. 对于原数组,生成一个新数组,对应位置的值表示当前位置到结尾的最大值,之后遍历,获取两个数组的差返回最大值

2.另一种思路,遍历一次,关键:curMax += max(0, curMax += [i] - [i - 1]) ,含义是 更新当前最大值
比如对 1, 2, 3 在指向3时,将当前最大值更新为2,对2, 3,1,指向1时,将当前最大值更新为0
之后每次对结果更新最大值

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        /* 第一种思路
        int len = prices.size();
        if (!len)
            return 0;
        vector<int> afterMax(len, 0);
        afterMax[len - 1] = prices[len - 1];
        for (int i = len - 2; i >= 0; --i)
            afterMax[i] = max(prices[i], afterMax[i + 1]);
        int res = 0;
        for (int i = 0; i < len; ++i)
            res = max(res, afterMax[i] - prices[i]);
        return res > 0 ? res : 0;
        */
        /*第二种思路*/
        int maxCur = 0, maxAll = 0;
        for (int i = 1; i < prices.size(); ++i) {
            maxCur = max(0, maxCur += prices[i] - prices[i - 1]);
            maxAll = max(maxCur, maxAll);
        }
        return maxAll;
    }
};

int main() {
    vector<int> prices = { 7,3,4,1,6 };
    Solution s;
    cout << s.maxProfit(prices) << endl;

    system("pause");
    return 0;
}
【Go语言实战】(2) Gin+Vue 电子商城 618来!!不用纠结去哪里剁手了!直接自己安排一个商场不香吗?? 阅读详情

相关推荐

Rust练手:从零手撸RISC-V架构虚拟机(一)(附完整代码)

使用Rust实现一个简单的RISC-V架构的CPU雏形,支持add,sub,addi三个指令。

qq_45983373的博客 2569

LeetCode 121. 买卖股票的最佳时机

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。 示例 1: 输入:[7,1,5,3,6,4] 输出:5 解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 注意利润不能是 7-1

tomjobs的博客 1764

告别“盲开”:手把手教你用MCU内置比较器搞定BLDC无感方波启动(附代码片段)

本文详细介绍了如何利用MCU内置比较器实现BLDC无感方波启动,解决传统无传感器控制中的'盲开'问题。通过虚拟中性点电路设计和三段式启动流程,结合反电动势检测和过零检测技术,提供了一套经济高效的解决方案,并附有可直接移植的代码片段,助力工程师快速实现可靠的无感控制。

weixin_33694172的博客 502

121. Best Time to Buy and Sell Stock

题意:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), d

xjcvip007的博客 287

本地部署 Bolt.new - 小学生都能轻松搭建AI编程环境

本地部署 Bolt.new - 小学生都能轻松搭建AI编程环境

engchina的专栏 8624

C# 上位机内存泄漏排查指南:3 个工具 + 4 个技巧,从 GC 频繁到稳定运行 720 小时

C# 上位机内存泄漏排查的核心是「先定位泄漏存在,再追溯引用根源,最后修复验证通过「Visual Studio 诊断工具(快速定位)+ dotMemory(深度分析)+ PerfView(生产监控)」三个工具的配合,结合「复现场景、排查静态成员+事件、检查非托管资源、GC 日志验证」四个技巧,可高效解决绝大多数泄漏问题。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 735

leetcode 121. Best Time to Buy and Sell Stock

Description: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of...

weixin_39979073的博客 317

解决Android API 30+视频播放抖动问题

摘要:本文探讨了Android开发中使用ExoPlayer在API 30+设备上出现的视频抖动问题。作者发现API 30以下版本运行正常,但API 30及以上版本会出现画面不稳定现象。解决方案是改用TextureView替代SurfaceView,通过修改PlayerView配置并设置setUseTextureView(true)来增强播放稳定性。这一简单改动有效解决了API兼容性问题,为开发者提供了实用的解决方案。

m0_62153576的博客 114

LeetCode算法题之121. Best Time to Buy and Sell Stock(easy)【Python3动态规划题解】

题目描述: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the st...

DaMoWangZQ的博客 444

基于深度学习的堰塞坝表层颗粒物质识别研究

本文提出一种基于深度学习的堰塞坝表层颗粒物质智能识别方法,针对颗粒分布散乱、背景复杂的问题,采用"大颗粒逐个识别、密集小颗粒分块识别"策略。结合Mask R-CNN、SLIC和AlexNet构建识别模型,通过无人机影像实现自动识别。实验表明该方法精度超过90%,具备迁移能力。文章提供了Python实现代码,涵盖数据预处理、SLIC分割、模型构建(Mask R-CNN和AlexNet)及训练流程,为堰塞坝应急抢险提供技术支持。

huanghm88的专栏 181

YOLOv26与其他目标检测算法的对比分析

本文对YOLOv26与其他主流目标检测算法进行了全面对比分析。YOLOv26在COCO数据集上达到96.0% mAP@0.5和79.0% mAP@0.5:0.95,显著优于其他YOLO系列算法,同时保持85FPS的推理速度和35MB的轻量级模型。相比两阶段算法(如Faster R-CNN),YOLOv26在速度、模型大小和内存占用方面优势明显;相比Anchor-free算法(如CenterNet),YOLOv26在速度和模型压缩方面更优。其核心创新包括动态锚框分配、自注意力机制和轻量级设计,使其成为适用于复

一键难忘的博客 993

【Leetcode】121. 最佳买卖股票的时间 I(Best Time to Buy and Sell Stock)

Leetcode - 121 Best Time to Buy and Sell Stock (Easy) 题目描述:最多交易一次。 解法一:DP public int maxProfit(int[] prices) { if(prices == null || prices.length == 0) return 0; int[][] mp = new int[prices.le...

厉兵秣码 175

【华为OD机试真题E卷】474、推荐多样性 | 机试真题+思路参考+代码解析(E卷复用)(C++、Java、Py)

题目描述 >推荐多样性需要从多个列表中选择元素,一次性要返回 N 屏数据(窗口数量),每屏展示 K 个元素(窗口大小),选择策略:   各个列表元素需要做穿插处理,即先从第一个列表中为每屏选择一个元素,再从第二个列表中为每屏选择一个元素,依次类推   每个列表的元素尽量均分为 N 份,如果不够 N 个,也要全部分配完,参考样例图:

KJ.JK 4434

【leetcode】40-best-time-to-buy-and-sell-stock 力扣 121. 买卖股票的最佳时机

解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5。给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。核心的一点:最大的利润,卖出之前则必须是买入的最小值、卖出的最大值。b1 买入时,我们初始化为 -prices[0];输入:prices = [7,6,4,3,1]输入:[7,1,5,3,6,4]

917

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode 121. Best Time to Buy and Sell Stock

LeetCode 121. Best Time to Buy and Sell StockMaSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie,

nianhua120的专栏 425

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

leetcode题解-121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell St && 66. Plus One

今天的两道题都很简单,就直接在一篇博客里进行记录即可。 首先说一下66. Plus One这道题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading

呜呜哈的博客 479

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

leetcode 121. Best Time to Buy and Sell Stock

leetcode-121 Best Time to Buy and Sell Stock (Easy) Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transacti...

Night_Thief的博客 166

LeetCode 121. Best Time to Buy and Sell Stock 题解

本题是贪心算法的经典应用,也是股票系列问题的入门题目。贪心:维护最低价格,计算最大利润动态规划:状态定义为持有/不持有股票空间优化:只需要维护两个状态通过本题可以深入理解贪心算法和动态规划在股票问题中的应用。

cannonjinx的博客 4040

Leetcode 121 Best Time to Buy and Sell Stock

Leetcode 121 Best Time to Buy and Sell Stock#include <vector> #include <math.h> using namespace std;class Solution { public: int maxProfit(vector<int>& prices)//brute force time limit exceeded

自己的学习笔记 217

leetcode 121 Best Time to Buy and Sell Stock 详细解答

leetcode 121 Best Time to Buy and Sell Stock 详细解答 此题的思路比较简单, 思路1:在遍历数组的时候保存一个最小值,同时保存一个当前值与最小值的差值,最后的结果就是保留最大的差值 代码如下: 思路2: 从股票的角度来理解,买入一支股票P就相当于自己本金亏损了P, 卖出一支股票P1就相当于自己赚了P1。代码跟上述代码几乎一致,只是思路不同。 ...

qq_22210659的博客 208

Leetcode 121. Best Time to Buy and Sell Stock

Leetcode 121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e...

fengran_mark的博客 140

闯关leetcode——121. Best Time to Buy and Sell Stock

实际我们在考虑这个问题时,需要抓住一个点:起始位置(目前的最小值)后如果小于该值的值,则它和后面出现的最大值的差就是最大差;如果出现了比它小的值,就要拿这个小的值向后去尝试。以此类推,可以得出最大的差值。这样它的复杂度就是O(n)。一种直观的思路就是找到一个值后,去寻找它后面最大的值,然后将差保存起来。不停迭代这个过程,得到最大的差值。但是这个思路是O(n。这题就是要找到在一个区间中,计算一个值与后面最大值的差,然后找到最大的差。

方亮的专栏 1349

LeetCode力扣 121. 买卖股票的最佳时机 Best Time to Buy and Sell Stock 题解代码 JavaScript

问题https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 练习使用JavaScript解答 /** * @param {number[]} prices * @return {number} */ var maxProfit = function(prices) { if(prices.length < 2) return 0; var arr = []; arr[pr.

Coding home - 漂流瓶jz 512

【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Python)

【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Python)题目描述解法 题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题目描述 Say you have an array for which the ith element is the price ...

ttinch的博客 174

[leetcode]121. Best Time to Buy and Sell Stock

[leetcode]121. Best Time to Buy and Sell Stock Analysis 周五啦周五啦,网上买的小南瓜到啦—— [六一快乐,嘻嘻~] Say you have an array for which the ith element is the price of a given stock on day i. If you were onl...

weixin_32135877的博客 88
上一篇: [LeetCode]53. Maximum Subarray
下一篇: [LeetCode]122. Best Time to Buy and Sell Stock II
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值