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. Method, apparatus, and system for speculative abort control mechanisms

    An apparatus and method is described herein for providing robust speculative code section abort cont ...

  2. netbeans8.2下struts2的Java Web开发Demo1

    struts2框架主要是封装了servlet,简化了jsp跳转的复杂操作,并且提供了易于编写的标签,可以快速开发view层的代码. 过去,我们用jsp和servlet搭配,实现展现时,大体的过程是: ...

  3. php通过$_SERVER['HTTP_USER_AGENT']获取浏览器相关参数

    最近不忙,同事在忙一个app项目.当听到领导安排让他做一个判断苹果还是安卓手机,如果是安卓手机下载安卓app.如果是苹果手机下载苹果app;然后我就上网搜了一下学习学习: php通过$_SERVER[ ...

  4. 误加all_load引起的程序报错

    一.为什么要加-all_load 在64位的mac系统和iOS系统下,链接器有一个bug,会导致只包含有类别的静态库无法使用-ObjC标志来加载文件.解决方法是使用-all_load或者-force_ ...

  5. 已知一个序列A1.A2….An,给你一个整数K,找到满足所有Ai+Aj>=k的数对(i,j)的个数

    #include<bits/stdc++.h> using namespace std; #define ll long long #define maxn 100010 /* 已知一个序 ...

  6. CentOS7安装部署jumpserver0.5

    组件说明 Jumpserver为管理后台,管理员可以通过Web页面进行资产管理.用户管理.资产授权等操作; Coco为SSH Server和Web Terminal Server.用户可以通过使用自己 ...

  7. HNOI 2006 BZOJ 1195 最短母串

    题面 问题描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字 ...

  8. weight initilzation

    1. pytorch提供接口 method 1  torch.nn.init里面有很多初始化分布 import torch.nn.init as init self.conv1 = nn.Conv2d ...

  9. Docker镜像原理和最佳实践

    https://yq.aliyun.com/articles/68477 https://yq.aliyun.com/articles/57126  DockerCon 2016 深度解读: Dock ...

  10. C# 导出 数据 到Excel

    /// <summary> /// 实现将数据导出至Excel, /// 在上面的代码中,我们首先将gridview绑定到指定的数据源中,然后在button1的按钮(用来做导出到EXCEL ...