<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

时间: 2024-08-26 16:02:03

&lt;LeetCode OJ&gt; 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 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 on

LeetCode练题——122. Best Time to Buy and Sell Stock II

1.题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 .  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5

LeetCode121/122/123 Best Time to Buy and Sell Stock&lt;股票&gt; I/II/III----DP+Greedy**

一:LeetCode 121 Best Time to Buy and Sell Stock 题目: 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), d

【leetcode刷题笔记】Best Time to Buy and Sell Stock III

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 at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

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 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 algor

【leetcode】122.Best Time to Buy and Sell Stock II

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) /************************ * @description: dynamic programming. * 相邻元素做

【算法分析与设计】【第一周】121.Best Time to Buy and Sell Stock&amp;122. Best Time to Buy and Sell Stock II

原题来自:121:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 122:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 第一周,小试牛刀吧. 题目121不复杂,就是英文原题读起来怪怪的.意思是,在保证先买后卖且最多只能买卖一次的情况下,计算最大利润. 思路很简单:逐个求差,差最大且被减数

LeetCode OJ 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 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). Ho

LeetCode OJ:Best Time to Buy and Sell Stock(股票买卖的最佳时期)

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. 简单的动