<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 ...
 
随机推荐
- C语言中的基础知识变量探讨
			
C语言中的变量是编程的基础,主要有四个要素:存储类型.存储大小.存储名称和存储地址. 一.变量的要素: 1.存储类型:主要表明名变量存储的特征,主要有auto.extern.static和regist ...
 - sublime text3 无法安装插件
			
下载 Package Control.sublime-package 点击这里下载: 打开sublime3 -> 首选项 -> 浏览插件 (程序自动打开插件目录) 删除 Package C ...
 - poj 2100(尺取法)
			
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 6107 Accepted: 1444 ...
 - js中click重复执行
			
问题背景:在写一个非常简单添加方法的时候,用onclick事件调用添加方法,点击第一次没问题,第二次会重复执行,经过多次查找资料得知这个状况的解决方案,特意总结一下: 代码如下:点击#spec_for ...
 - java的架构流行阶段
			
第一阶段:SSM 第二阶段:分布式系统改造,平台化初具规模,各项垂直业务系统搭建上线.产品端极大丰富用户投资.大数据平台研究并使用 第三阶段:SOA治理,使用zookeeper作为注册中心,dubbo ...
 - Codeforces 732F. Tourist Reform (Tarjan缩点)
			
题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...
 - 从int 3探索Windows应用程序调试原理
			
http://www.cnblogs.com/xuanyuan/p/3998408.html
 - 【python】redis基本命令和基本用法详解
			
[python]redis基本命令和基本用法详解 来自http://www.cnblogs.com/wangtp/p/5636872.html 1.redis连接 redis-py提供两个类Redis ...
 - Hadoop一些问题总结
			
1.运行mr程序出错 connecting to resoucemanager retrying .... retrying ..... 原因是没有启动yarn或者启动失败 2.初始化工作目录结构 h ...
 - 在windows搭建jenkins測试环境
			
jenkins 搭建好开发环境必备之中的一个,简单易用,搭建測试平台非常有帮助,不知道的都能够了解一下 官网下载地址 http://jenkins-ci.org/ 我是下载window版本号的 安装有 ...