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), design an algorithm to find the maximum profit.
思路:变相的求最大子数组,是算法导论上的例题,书上用分治法做的。
若prices=[1,4,2,8,1],每两数相减所得的利润,即头一天买入,第二天卖出。
profit=[0,3,-2,6,-7],可以看到第一天买入,最后一天卖出的profit为3+(-2)+6+(-7)=0
代码:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int len=prices.size();
int res=;
int temp=;
if(len==) return res;
vector<int> profit(len,);
vector<int> dp(len,);
for(int i=;i<len;++i){
if(i==) {profit[i]=;continue;}
profit[i]=prices[i]-prices[i-];
}
dp[]=profit[];
for(int i=;i<len;++i){
dp[i]=max(dp[i-]+profit[i],profit[i]);
if(dp[i]>res)
res=dp[i];
}
return res;
}
};
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 a given stock on day i. If you were ...
- Leetcode 121 Best Time to Buy and Sell Stock 动态规划
由于题意太长,请自己翻译,很容易懂的. 做法:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求.动态规 ...
- LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...
- 动态规划——Best Time to Buy and Sell Stock III
题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益. 示例 1:Input: [3,3,5,0,0,3 ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
随机推荐
- Qt获取本机IP地址
Qt获取本机IP地址: Qt版本:4.8.6 #include <QtNetwork/QNetworkInterface.h> QString ipAddr; QList<QHost ...
- js中的函数编程
之前在网上看到了一篇教你如何用js写出装逼的代码. 经过学些以及扩展很有收获在这里记录一下. 原文章找不到了.所以就不在这附上链接了. 大家看下下面两段js代码. 上面两端代码效果是一模一样的,都是在 ...
- docker 新手入门 (web项目的部署)
web项目的部署 1.首先我们下载centos镜像.docker pull centos 2.下载完成之后,我们首先要安装的是java环境 tomcat 和jdk 3.将下载好的软件放入到nmt目录 ...
- Wow64
翻译自Wikipedia: WoW64 运行在微软平台上,WoW64(Windows 32-bit on Windows 64-bit) 是一个Windows的子操作系统, 它能运行32位的应用,在所 ...
- OpenCV2:第二章 创建图像并显示
一.简介 相当于在PS中,新建一个画布 二.CvMat类/LPLImage和CvMat结构体 参考: OpenCV2:第一章 图像表示 三.create() Mat m(2,2,CV_8UC3); m ...
- 【2019-5-26】python:字典、常用字符串处理方法及文件操作
一.数据类型:字典 1.字典: 1.1定义字典:dict={'key':'value'} 1.2字典与列表相比,字典取值快,可直接找到key 1.3字典是无序的,不能根据顺序取值 1.4多个元素用逗号 ...
- excel数据比对,查找差异
1.选中需比对的数据 2.开始->条件格式->突出显示单元格规则->重复值 3.选择唯一值,点击确定 4.结果展示 5.颜色标识的即:不同值
- 数组合并--php
常用的合并数组方法有以下几种: 1 array_merge 2 '+' 3 array_merge_recursive 下面是一段对比的代码 $array1 = array(2,4," ...
- 深入Linux内核架构——锁与进程间通信
Linux作为多任务系统,当一个进程生成的数据传输到另一个进程时,或数据由多个进程共享时,或进程必须彼此等待时,或需要协调资源的使用时,应用程序必须彼此通信. 一.控制机制 1.竞态条件 几个进程在访 ...
- Openstack实验笔记
Openstack实验笔记 制作人:全心全意 Openstack:提供可靠的云部署方案及良好的扩展性 Openstack简单的说就是云操作系统,或者说是云管理平台,自身并不提供云服务,只是提供部署和管 ...