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

&lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. C#TreeView读取Xml,TreeView导出到Xml

    实现功能有1.根据Xml生成TreeView2.双击修改节点3.右键添加子节点或添加要节点4.右键删除当前选择的节点5.将修改后的TreeView重新生成Xml文档 其实这个主要是实现 了Xml生成T ...

  2. LeetCode OJ-- Search a 2D Matrix

    https://oj.leetcode.com/problems/search-a-2d-matrix/ 具有数据递增性质的一个二维数组,对它进行二分搜索. 首先搜索target所在的行,再找列. c ...

  3. 牛客练习赛10 E题 数列查找 (分块思想 + 莫队算法)

    题目链接  数列查找 考虑分块然后跑莫队, 设$c[i]$为$i$在当前维护的区间内出现的次数, $g[i]$为在当前维护的区间内有多少个数出现次数为$i$, $bg[i]$把出现次数分块,$bg[i ...

  4. commons-lang3-StringUtils

    字符串工具类   abbreviate(String str, int maxWidth) 返回一个指定长度加省略号的字符串,maxWidth必须大于3 StringUtils.abbreviate( ...

  5. [BUG] CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)

    写在前面 在浏览view的时候,突然出现这个错误,不过还是很好解决的. bug 解决方案

  6. 如何判断自己外网IP是否为真实公网IP,以及解决方案

        如何判断本地(路由器)分配的IP是否公网IP? 公网IP:全球唯一IP地址.(公网IP又分为静态公网IP和动态公网IP,如何分辨的话,进入路由器查看连接方式pppoe连接都是动态公网IP.) ...

  7. 列表pagesize修改每页显示的数量失效

    ◇系统错误修复工具 >> 检测微表正确性 原因是删除一些数据导致记录与实际数据不符 转自:http://bbs.dedecms.com/269491.html

  8. CSS 属性选择器的深入挖掘

    CSS 属性选择器,可以通过已经存在的属性名或属性值匹配元素. 属性选择器是在 CSS2 中引入的并且在 CSS3 中得到了很好拓展.本文将会比较全面的介绍属性选择器,尽可能的去挖掘这个选择器在不同场 ...

  9. windows8开发-关于wp7应用迁移到win8 metro风格

    虽然微软说,wp7应用移植到win8上面是比较简单,只需要修改部分API和设计原则上的细节,同时它也提供了一份比较简洁的参考文档: 而实际上这种移植的工作量还是不小的,尤其当应用引用了较多底层的API ...

  10. 【DQ冰淇淋】—— Babylon 冰淇淋三维互动营销项目总结

    前言:在学习过Babylon.js基础之后,我上手的第一个网页端3D效果制作项目就是‘DQ冰淇淋’.这个小项目应用到了Babylon最基础的知识,既可以选味道,选点心,也可以旋转.倒置冰淇淋,互动起来 ...