Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益

原创 2015年07月08日 23:29:45

原题地址

https://leetcode.com/problems/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.
如果你至多只能完成一次交易(即,买一次股票、卖一次股票),设计一个算法来计算最大收益。

解题思路

极大值减去极小值即可获得临时最大收益,一次遍历过程中不断更新临时最大收益。遍历结束时即获得最大收益。

算法描述

  1. 极小值low设为第一天的价格。
  2. 遍历剩余的价格,设当前某一天的价格为cur,计算diff=cur-low,如果diff<0,则找到新的极小值;如果diff>max则更新最大收益max=diff,否则继续遍历下一天,重复第2步。
  3. 返回最大收益max。

代码 C

/**
 * 计算最大收益
 * @param prices : 每日价格
 * @param pricesDize : 数据规模
 * @return : 最大收益 
 * @note : 只进行一次买卖
 */
int maxProfit(int* prices, int pricesSize) {
    /* 如果价格数据为空或者只有一个数据,返回0  */
    if (pricesSize <= 1) return 0;

    int min = *prices, day, diff, max = 0;
    for (day = 1; day < pricesSize; ++day) {
        diff = *(prices + day) - min;
        if (diff < 0) min = *(prices + day);
        else if (diff > max) max = diff;
    }

    return max;
}

完整代码https://github.com/Orange1991/leetcode/blob/master/121/c/main.c

代码 CPP

class Solution {
public:
    /**
     * 计算最大收益
     * @param prices : 每日价格
     * @return : 最大收益 
     * @note : 只进行一次买卖
     */
    int maxProfit(vector<int>& prices) {
        int pricesSize = prices.size();
        /* 如果价格数据为空,或者只有一个数据,返回0  */
        if (pricesSize <= 1) return 0;

        int day, diff, min = prices[0], max = 0;
        for (day = 1; day < pricesSize; ++day) {
            diff = prices[day] - min;
            if (diff < 0) min = prices[day];
            else if (diff > max) max = diff;
        }

        return max;
    }
};

完整代码https://github.com/Orange1991/leetcode/blob/master/121/cpp/main.cpp

运行情况

Lauguage Status Time
c Accept 4ms
cpp Accept 8ms

2015/7/8

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。 设计一个算法来计算最大收益。你至多可以完成两次交易...

Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。设计一个算法来计算最大收益。你可以完成任意次数的交易(即,买买股票多次)。但是你不能同时进行多次交易(即,在你买入股票之前必须卖掉手里的股票)。...

[LeetCode]121. Best Time to Buy and Sell Stock(求近期股票能获得的最大利润)

121. Best Time to Buy and Sell Stock原题链接 Say you have an array for which the ith element is the pri...

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have a...

Best Time to Buy and Sell Stock 股票交易最大收益

主要是利用动态规划的思想解决问题。Best Time to Buy and Sell Stock I Say you have an array for which the ith element...

leetcode 121. Best Time to Buy and Sell Stock | 最大差值和最大子序列关系

leetcode 121Say you have an array for which the ith element is the price of a given stock on day i.I...

LeetCode OJ-121. Best Time to Buy and Sell Stock(股票问题)

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri...

LeetCode121/122/123/188 Best Time to Buy and Sell Stock<股票> I/II/III/IIII----DP+Greedy**

一:LeetCode 121 Best Time to Buy and Sell Stock 题目: Say you have an array for which the ith element...

Best Time to Buy and Sell Stock III 最佳时间买入卖出股票(最多两次买卖)@LeetCode

直到要用DP做,但是一开始思路是错的。后来参考了 http://blog.csdn.net/pickless/article/details/12034365 才意识到可以在整个区间的每一点切开,然后...

LeetCode:Best Time to Buy and Sell Stock(买卖股票)系列

121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of ...
  • BestZem
  • BestZem
  • 2016年07月25日 10:26
  • 247
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益
举报原因:
原因补充:

(最多只允许输入30个字)