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 ...
随机推荐
- css3技巧——产品列表之鼠标滑过效果(一)
查看效果: http://www.daqianduan.com/example?pid=6117 html代码: <div class="main"> <div ...
- C++ 面试题整理
我和朋友们面到的c++试题整理 虚表 static const sizeof 可构造不可继承的类 stl Iterator失效 map vector vector的removed_if 优化 ---- ...
- Android串口通信(基于Tiny6410平台)
友善之臂的Android系统有他们自己编写的一个串口通信程序,网上没有找到他的源代码,而且界面操作不在一个界面,不是很方便,这里我自己写了一个粗糙点的串口通信程序. 同样这里还是调用友善之臂的frie ...
- SGU 114
114. Telecasting station time limit per test: 0.25 sec. memory limit per test: 4096 KB Every city in ...
- 【好玩的应用】QQ连连看辅助工具
自己学了这么久的C语言,但没有写出过什么可以用的东西来,总觉得心里不爽.这几天实在是不想干正事,在网上瞎逛逛,结果发现有人写了连连看的外挂.顿时觉得这很有意思啊.于是把代码下载下来,捣鼓了捣鼓.发现还 ...
- 手把手VirtualBox虚拟机下安装rhel6.4 linux 64位系统详细文档
下面演示安装的是在VirtualBox里安装rhel 6.4 linux 64位系统. 一.VirtualBOX 版本. 二.虚拟机的配置. 1.现在开始演示安装,一起从零开始.点击“新建”,创建新的 ...
- 为Android Studio 项目手动下载gradle
在http://developer.android.com/samples/index.html上下载的例子,导入Android Studio的时候,第一件事就是下载项目对应版本的gradle.gra ...
- 【多媒体封装格式详解】---MKV
http://blog.csdn.net/tx3344/article/details/8162656# http://blog.csdn.net/tx3344/article/details/817 ...
- 只有innoDB才允许使用外键
1.只有InnoDB引擎才允许使用外键,所以,我们的数据表必须使用InnoDB引擎. 2.注意: 1.必须使用InnoDB引擎: 2.外键必须建立索引(INDEX): 3.外键绑定关系这里使用了“ O ...
- Zookeeper安装部署
Zookeeper安装 1. 安装 wget http://www.apache.org/dist//zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz ...