LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限。
思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大。但是时间是不能冲突的,比如说在明天买入,今天卖出。因此,对于今天的价格,应该要找到今天之前的该股的最低价,买入,今天卖出。
其实就是要为序列中的一个元素A[k],找到另一个元素A[e],位置满足e<k,结果使得A[k]-A[e]最大。
用动态规划解决,从左扫起,遇到一个元素就更新当前最小值,再用当前元素去减这个最小值。扫完就知道结果了。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int small=, ans=;
for(int i=; i<prices.size(); i++)
{
small=min(small, prices[i]); //找到i之前的最小值
ans=max( prices[i]-small, ans );
}
return ans;
}
};
AC代码
LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)的更多相关文章
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. If you were ...
- [LeetCode] 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 ...
- 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机
网址:https://leetcode.com/problems/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 ...
- [LintCode] 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 ...
- 121. Best Time to Buy and Sell Stock买卖股票12
一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
随机推荐
- Oracle数据库表的备份和数据表的删除操作
--Oracle数据库中的表备份: --备份语句:在备份之后就可以将这张表的所有数据源删除了,但是之后有人对这张表的数据进行操作,但是在操作完成之后要记得将数据表恢复 CREATE TABLE DZH ...
- javamail邮件发送报错解决方案
如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:Exception in thread "main" java.lang.NoClassDefFoundErro ...
- 如何实现Windows Phone代码与Unity相互通信(事件方式)
源地址:http://www.cnblogs.com/petto/p/3909063.html 一些废话 昨天写一篇今天写一篇.不是我闲的蛋疼,是今天一天碰到了好几个恼人的问题,浪费一天时间搞定.本文 ...
- JSP图片上传 公共工具类
需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...
- POJ 2568/ZOJ 1965 Decode the Tree
题意:在树中,每次删去节点值最小的叶子结点. 每删去一个点,就给出与这相连的点的值,直到最后只剩下一个根结点,给这N-1个数,重新建立这个树. 思路: 给出的节点号按次序存入到数组a中,将未给出的数存 ...
- jQuery经典面试题及答案精选[转载]
问题:jQuery的美元符号$有什么作用? 回答:其实美元符号$只是”jQuery”的别名,它是jQuery的选择器,如下代码: $(document).ready(function(){ }); 当 ...
- windows cmd控制台打开和关闭SqlServer 以及 显示发生系统错误5 拒绝访问的解决方案
打开:net start mssqlserver 关闭:net stop mssqlserver 在dos下输入 net start mssqlserver 显示发生系统错误5 拒绝访问 解决办法,以 ...
- Thread的第一天学习
1.实现线程的方法: 1)extend Thread 2)implements Runnable 2.下面代码执行哪个run方法: new Thread( new Runnable(){ public ...
- css元素z-index设置为什么不起作用?
元素位置重叠的背景常识 (x)html文档中的元素默认处于普通流(normal flow)中,也就是说其顺序由元素在文档中的先后位置决定,此时一般不会产生重叠(但指定负边距可能产生重叠). 当我们用c ...
- PowerDesigner概念模型的Notation设置
原文:PowerDesigner概念模型的Notation设置 在进行数据库设计模型时,分为概念模型设计和物理模型设计两种,概念模型主要是反映真是 世界中的业务关系,也就是我们常用的实体关系图.物理模 ...