不会跑

work for life

22 Jun 2017

LeetCode-Best_Time_to_Buy_and_Sell_Stock

动态规划的小题目,买卖股票, 原题链接: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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]

Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]

Output: 0

In this case, no transaction is done, i.e. max profit = 0.

# _*_ coding: utf-8 _*_


class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        用dp记录最大收益,以及之前的时段的最低价格
        """
        if not prices:
            return 0
        dp = [0, prices[0]]
        for i in xrange(1, len(prices)):
            if prices[i] - prices[i-1] > 0:
                dp[0] = max(prices[i] - dp[1], dp[0])
            dp[1] = min(prices[i], dp[1])
        return dp[0]
comments powered by Disqus