<LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)
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.
分析:
思路首先:动态规划
遍历数组时记录当前价格曾经的最小价格curMin,记录昨天可以获得的最大利润maxProfit
对于今天,为了能获得此刻的最大利润,显然仅仅能卖,或者不做不论什么操作
假设不做不论什么操作。显然还是昨天maxProfit
假设卖掉今天天的股票。显然prices[i]-curMin
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<=1)
return 0;
int curMin=prices[0];
int maxProfit=0;
for(int i=1;i<prices.size();i++)
{
maxProfit=max(maxProfit,prices[i]-curMin);
curMin=min(curMin,prices[i]);//获得历史最小价格的股票
}
return maxProfit;
}
};
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like
(ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).
同一时间不能做多次交易(买一次再卖一次,或者卖一次再买一次算一次交易)。意思就是说在你买股票之前你必须卖掉股票
(即你手头最多同意保留一仅仅股票。同一时候隐含了每次仅仅能交易一次的意思)
分析:
题目理解错误,刚開始没有不论什么思路....这题通过率40%。我的内心是崩溃的!
!!
题目:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。设计一个算法找出最大利润
但一次仅仅能交易一支股票,也就是说手上最多仅仅能持有一支股票,求最大收益。
分析:贪心法。从前向后遍历数组,仅仅要当天的价格高于前一天的价格(即为了最大利润,仅仅要有利润存在就利用交易次数的无限制贪心的获取)。就累加到收益中。
代码:时间O(n),空间O(1)。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() < 2)
return 0;
int profit = 0;
for(auto ite = prices.begin()+1; ite != prices.end(); ite++)
profit += max(*ite - *(ite-1),0);
return profit;
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50524380
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)的更多相关文章
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- 【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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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 ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
- 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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [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 ...
- 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 ...
随机推荐
- Linux signal 那些事儿(4)信号的deliver顺序【转】
转自:http://blog.chinaunix.net/uid-24774106-id-4084864.html 上一篇博文提到了,如果同时有多个不同的信号处于挂起状态,kernel如何选择deli ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---12
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- java 修改字体大小
在Windows->Preferences->General->Appearance->Colors and Fonts->Java->Java Editor Te ...
- bytearray和file的后端上传方式
public static String readAndUpload(String serverpath,String imgid) { if(serverpath==null){ serverpat ...
- KMP算法之我见
预备谈谈下面这些,可能有补充 KMP算法的用途: KMP算法之前的暴力: KMP算法预备知识与概念: KMP算法模板: KMP算法的习题. 1.KMP算法的用途: 主要用于模式匹配(字符串匹配).给定 ...
- python安装在windows server2008,故障排除
python安装在windows server2008,故障排除 我也在虚拟机上的win2008安装python2.7.9多次回滚了.搜了一通 Windows Server 2003/2008无法 ...
- 2016北京集训测试赛(六)Problem B: 矩阵
Solution 最小割. 参考BZOJ 3144切糕 在那道题的基础上将建图方法稍作变形: 我们对格子进行黑白染色, 对于两个格子之和\(\le k\)的限制, 就可以确定其中一个是白色格子, 一个 ...
- shell date获取时间值
使用Shell获取上一个月.星期的时间范围 #!/bin/bash date_today=`date -d '1 day ago' +%Y%m%d` #最近7天 date_befor_7day=` ...
- mysql之创建数据库,创建数据表
写在前面 项目中用到mysql数据库,之前也没用过mysql,今天就学下mysql的常用的语法,发现跟sql server的语法极其相似.用起来还是蛮简单的. 一个例子 1.创建一个名为School的 ...
- 使用assembly将maven项目pom.xml中的jar包打包
参考官方网站:http://maven.apache.org/plugins/maven-assembly-plugin/usage.html 方法一:将pom.xml引入的jar包打到zip文件夹中 ...