You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

dp_00 第i天 当前没有股票 状态是0

dp_01 第i天 当前持有股票 状态是1

class Solution {
public:
int maxProfit(vector<int>& prices) {
//经典的动态规划题目
if(prices.size()==0) return 0;
int dp_00=0;
int dp_01=INT_MIN;
for(auto it:prices)
{
dp_01=max(dp_01,-it);
dp_00=max(dp_00,dp_01+it); } return dp_00; }
};

【leetcode】121. Best Time to Buy and Sell Stock(股票问题)的更多相关文章

  1. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  2. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  3. [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 ...

  4. 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 ...

  5. Java for 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 ...

  6. leetcode 121. Best Time to Buy and Sell Stock ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. Python [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 ...

  8. [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 ...

  9. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

  10. LeetCode 121. Best Time to Buy and Sell Stock (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

随机推荐

  1. PicGo插件

    前言:主要介绍PicGo插件,这里的图床上传软件是PicGo-Core,使用命令行操作 PicGo_Path:自己的PicGo安装路径,如果通过Typora一般安装位置位于 C:\Users\自己的主 ...

  2. (四)php连接apache ,使用php-fpm方式

    上面各篇记录了编译安装lamp的各个部分,下面主要解决php和apache的连接问题.通过 php-fpm 连接. 连接前环境检查: php -v PHP 5.6.30 (cli) (built: O ...

  3. WSL2学习和使用汇集

    接触WSL2过程中整理沉淀的一些知识点,大纲如下,内容比较多,详细内容参考https://www.yuque.com/wushifengcn/kb/mbg1b5 欢迎感兴趣者补充和提出问题,共同学习. ...

  4. C++ IO基础

    一:c++I/O处理,按照数据输入输出的过程,形象的将其看做流.数据在流中进行传播. 所有的流有两个基类:ios和streambuf类 streambuf:提供对缓冲区的基本操作,设置缓冲区等 ios ...

  5. SpringBoot 整合thymeleaf

    1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...

  6. 浅讲.Net 6 并与之前版本写法对比

    介绍 昨天vs2022正式版已经推出了,估计很多人已经下载并开始创建.Net 6 开始尝鲜了, 本节我简要的给大家介绍一下.Net 6的一些改动. 正文 本次.Net6带来最明显的变化就是: 采用顶级 ...

  7. RabbitMQ保证消息的顺序性

    当我们的系统中引入了MQ之后,不得不考虑的一个问题是如何保证消息的顺序性,这是一个至关重要的事情,如果顺序错乱了,就会导致数据的不一致.       比如:业务场景是这样的:我们需要根据mysql的b ...

  8. 暑假算法练习Day2

    第二天啦!大家一起冲冲冲!! 1004 成绩排名 (20 分) 读入 n(>0)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用 ...

  9. 🏆【Alibaba中间件技术系列】「RocketMQ技术专题」小白专区之领略一下RocketMQ基础之最!

    应一些小伙伴们的私信,希望可以介绍一下RocketMQ的基础,那么我们现在就从0开始,进入RocketMQ的基础学习及概念介绍,为学习和使用RocketMQ打好基础! RocketMQ的定位 Rock ...

  10. 菜鸡的Java笔记 日期操作类

    日期操作类        Date 类与 long 数据类型的转换        SimpleDateFormat 类的使用        Calendar 类的使用                如 ...