<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 ...
随机推荐
- SQL Server 2005 无法连接到 127.0.0.1
[问题描述]如果连接本机的数据库服务器,服务器名称可填: 127.0.0.1或localhost或本机机器全名(比如HOME),有时候用本机机器名可以登录SQL Server 2005,但用127.0 ...
- s 中日期 转换成时间戳 例如2013-08-30 转换为时间戳
以前遇到过一个关于时间戳的问题,为了不被大家鄙视,先说一下概念. 具体时间戳怎么定义的我也不清楚,但百度百科中有这么一句:“时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时 ...
- centos7下配置时间同步服务器
同网段20几台服务器: 其中有一组mysql 集群中 互为主从 选一台mysql master 作为时间同步的服务器,这样做的好处以便于这台down了 另一个与他互为主从的master 继续提供时间同 ...
- 二叉树遍历 Morris
二叉树的遍历,先根遍历,不适用递归,存储空间为 O(1) 转自:http://chuansongme.com/n/100461 MorrisInOrder(): while 没有结束 如果当前节点没有 ...
- HDU 1018 Big Number【斯特林公式/log10 / N!】
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- localStorage.getItem
WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...
- Product of Array Except Self - LeetCode
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- COFF - 中间文件格式解析
http://www.cnblogs.com/weikanzong/p/5296739.html
- Create Data Block Based On From Clause Query In Oracle Forms
Example is given below to create a data block based on From Clause query in Oracle Forms. The follow ...
- 由内省引出JavaBean的应用
IntroSpector-->javaBean-->特殊的java类 get和set方法 ReflectPoint pt1 = new ReflectPoint(3,5); String ...