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. Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤

    http://blog.csdn.net/vagrxie/article/details/4602961 Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤 wri ...

  2. 使用jquery完成定时弹出广告图片

    <script src="../js/jquery-1.8.3.js"></script> <script type="text/javas ...

  3. springBoot 定时器

    程序入口类中加入注解 @EnableScheduling 配置定时任务为并行 @Slf4j @Configuration public class ScheduledConfig implements ...

  4. ConstraintLayout 约束布局

    约束布局ConstraintLayout 这种布局方式出现已经有一段时间了,刚出现的时候一直以为这种布局只是针对拖拽使用的布局,最近在新项目里看到了这种布局,又重新学习了这种布局,才发现以前真的是图样 ...

  5. java值传递和引用传递的理解

    java的基础数据类型有:(byte.short.int.long.float.double.char.boolean)八种 基础数据都是值传递,其他都是引用传递.但是引用传递要特别注意:String ...

  6. mysql二进制安装,升级,多实例部署

    目标 理解线上部署考虑的因素 学会编译安装以及二进制安装mysql 学会升级mysql 学会多实例部署mysql数据库 学会合理部署mysql线上库   考虑因素: 版本选择,5.1,5.5还是5.6 ...

  7. UIView之userInteractionEnabled属性介绍-特殊子类覆盖多见于UIImageView和UILabel

    属性作用 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互. 当值设置为NO后,UIView会忽略那些原本应该发生在其自身的诸如touch和keyboard等用 ...

  8. Android中的多线程断点续传

    Android多线程断点下载的代码流程解析: 运行效果图: 实现流程全解析: Step 1:创建一个用来记录线程下载信息的表 创建数据库表,于是乎我们创建一个数据库的管理器类,继承SQLiteOpen ...

  9. Android开发初期之后怎么提升?怎么才能叫精通?方向在哪?

    hi大头鬼hi Android开发专家     先mark一下,好多人我发现始终停留在两三年的水平上没有突破. 另外还有一个误区就是越底层越牛逼 第三个就是,我认识的大部分所谓的做过rom开发的对fr ...

  10. 防止sql注入和跨站脚本攻击,跨站请求伪造以及一句话木马的学习记录

    以下是来自精通脚本黑客的学习记录 防止以上漏洞的最好的方式 一对用户的输入进行编码,对用户输入进行编码,然后存入数据库,取出时解码成utf-8 二对用户的输入进行过滤,过滤jscript,javasc ...