You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
 
class Solution {
public:
int maxProfit(vector<int>& prices) {
//经典动态规划
//如何表示当天进行的交易
// 注意一点为啥是这个顺序
int a_i0=0; //无股票
int a_i1=INT_MIN; //有股票
for(auto pp:prices){
a_i0=max(a_i0,a_i1+pp);
a_i1=max(a_i1,a_i0-pp);
}
return a_i0; }
};

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

  1. 31. leetcode 122. Best Time to Buy and Sell Stock II

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

  2. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

  3. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

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

  4. LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)

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

  5. leetcode 122. Best Time to Buy and Sell Stock II ----- java

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

  6. Java [Leetcode 122]Best Time to Buy and Sell Stock II

    题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  7. LeetCode 122 Best Time to Buy and Sell Stock II 解题报告

    题目要求 Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  8. [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二

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

  9. Java for LeetCode 122 Best Time to Buy and Sell Stock II

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

  10. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

随机推荐

  1. 彻底解决SLF4J的日志冲突的问题

    今天公司同事上线时发现,有的机器打印了日志,而有的机器则一条日志也没有打.以往都是没有问题的. 因此猜测是这次开发间接引入新的日志jar包,日志冲突导致未打印. 排查代码发现,系统使用的是SLF4J框 ...

  2. 使用jax加速Hamming Distance的计算

    技术背景 一般认为Jax是谷歌为了取代TensorFlow而推出的一款全新的端到端可微的框架,但是Jax同时也集成了绝大部分的numpy函数,这就使得我们可以更加简便的从numpy的计算习惯中切换到G ...

  3. Kali安装Parallels Tools过程记录

    最近两天又参加了公司一年一度的网络安全劳动竞赛,之前用过的一个 Kali 忘记密码进不去了 -_- .重新安装了 Kali 2021.3a 之后发现 Parallels Tools 安装失败,记录了一 ...

  4. linux网络编程 IO多路复用 select epoll

    本文以我的小型聊天室为例,对于服务器端的代码,做了三次改进,我将分别介绍阻塞式IO,select,epoll . 一:阻塞式IO 对于聊天室这种程序,我们最容易想到的是在服务器端accept之后,然后 ...

  5. prometheus(7)之数据类型与PromQL语法

    Prometheus的四种数据类型 counter (只增不减 计数) Gauge (常规数值 可变化大小) histogram (柱状图 小于上边界的 总数与次数) summary (分位数  小于 ...

  6. 攻防世界 WEB 高手进阶区 tinyctf-2014 NaNNaNNaNNaN-Batman Writeup

    攻防世界 WEB 高手进阶区 tinyctf-2014 NaNNaNNaNNaN-Batman Writeup 题目介绍 题目考点 了解js代码(eval函数.splice函数) 了解正则 Write ...

  7. 【数据结构&算法】11-树基础&二叉树遍历

    目录 前言 树的定义 树的存储结构 双亲表示法 孩子表示法 孩子兄弟表示法 二叉树 定义 特点 形态 特殊二叉树 斜树 满二叉树 完全二叉树 二叉树的性质 二叉树的存储结构 二叉树的顺序存储结构 二叉 ...

  8. sqlalchemy create single table

    User.__table__.drop(engine) User.__table__.create(engine) https://stackoverflow.com/a/45287771/80250 ...

  9. RabbitMQ (五):死信队列

    什么是TTL RabbitMQ的TTL全称为Time-To-Live,表示的是消息的有效期.消息如果在队列中一直没有被消费并且存在时间超过了TTL,消息就会变成了"死信" (Dea ...

  10. 『学了就忘』Linux软件包管理 — 42、对RPM软件包的查询操作

    目录 1.查询RPM软件包是否安装 2.查询系统中所有已安装的RPM软件包 3.查询RPM软件包的详细信息 4.查询RPM软件包中的文件列表 5.查询系统文件属于哪个RPM包 6.查询RPM软件包所依 ...