【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
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(股票问题)的更多相关文章
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
- 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 ...
随机推荐
- hdu 1080 Human Gene Functions(DP)
题意: 人类基因由A.C.G.T组成. 有一张5*5的基因表.每格有一个值,叫相似度.例:A-C:-3.意思是如果A和C配对, 则它俩的相似度是-3[P.S.:-和-没有相似度,即-和-不能配对] 现 ...
- vue2-admin-mpa vue2多页面应用系统【开源项目】
1. 项目介绍 一个基于Vue2.0的多页面应用系统. 技术栈包含:Vue.VueX.Vue Router.Element UI. 2. 功能介绍 2.1 多页面切换功能 说明:采用 tabs + ...
- Mysql教程:(三)运算符:数学运算符
运算符:数学运算符 mysql> select class,number,maths,maths+5 from score; mysql>select class,number,chine ...
- JMeter 5.4 打开测试计划 报错:Unexpected error
保存测试计划名为: 新增用户.jmx 关闭 JMeter 再次打开JMeter : 双击 jmeter.bat JMeter 启动 打开 新增用户.jmx Unexpected error. 看上图 ...
- django前后端分离403 csrf token missing or incorrect
根据这个链接https://stackoverflow.com/a/26639895 这是一个django的跨域访问问题. django,会对合法的跨域访问做这样的检验,cookies里面存储的'cs ...
- C# for Beginner Part 21 to 30
Part 21 Inheritance in c# Why Inheritance Pillars(支架) of Object Oriented Programming 1,Inheritance(继 ...
- java更开源-安全可靠国产系统背景下的应有.NET Core的一席之地
"安可"背景下的中国软件开发 在中美当前背景下的安全可靠国产系统(简称安可),安可产业要实现技术自主可控,需要在四个层面逐步实现:基础硬件设施,如芯片.服务器.存储.交换机.路由器 ...
- 通过t-sql定期自动备份SQL Server 上的所有数据库
项目背景 解决方案 方案一,是采用SQL的定时备份,建立作业来操作,这里有完整的使用手册: 方案二:基于t-sql方法进行查询备份 方案思路: 1.1 在 Master 数据库上创建一个备份所有数据库 ...
- 基于Mui与H5+开发webapp的Android原生工程打包步骤(使用新版本5+SDK与Android studio)(部分内容转自dcloud官网)
文章背景: dcloud官网给出的打包步骤对于有一定安卓打包基础的同学来说比较容易掌握,但是对于webapp小白来讲有的地方可能没有说的太具体.下面我给大家介绍的详细一点,保证大家按照步骤就能学会打包 ...
- 『学了就忘』Linux软件包管理 — 43、RPM包的校验和证书
目录 1.RPM包的校验 (1)RPM包校验基本命令 (2)校验某个系统文件是否被修改举例 (3)验证内容中8个信息的具体内容 (4)文件类型有哪些 2.RPM包的证书 (1)数字证书 (2)数字证书 ...