(DP)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.

题目大意:

给定一个数组,里面的值表示一支股票每天的价格, 如第i天价格为prices[i],假设只能进行一次买进,一次卖出,请找出最大利润值。



解:

以prices[i]表示第i天股票价格,profit[i]表示到第i天为止所能获取的最大利润。

考虑在第n天卖出股票的最大利润 profit[n]=max(profit[n-1],  prices[n]-min), 其中min为前n-1天的股票最低价。

可见我们必须在遍历数组的过程中记录最小值。

而且profit只跟前一个状态有关,所以只需用一个值来记录,而不用数组。



Java代码:

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0)
            return 0;
        int min = prices[0];
        int profit = 0;
        for(int i = 1; i < prices.length; i++){
            min = prices[i] < min ? prices[i] : min;
            profit = prices[i] - min > profit ? prices[i] - min : profit;
        }
        return profit;
    }
}

Python代码:

class Solution:
    # @param {integer[]} prices
    # @return {integer}
    def maxProfit(self, prices):
        if not prices:
            return 0
        tmin = prices[0]
        profit = 0
        for each in prices[1:]:
            profit = max(profit, each-tmin)
            tmin = min(each, tmin)
        return profit
时间: 2024-10-29 19:08:35

(DP)Best Time to Buy and Sell Stock的相关文章

【题解】【数组】【DP】【Codility】Best Time to Buy and Sell Stock

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

[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

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

[LeetCode]Best Time to Buy and Sell Stock III 动态规划

本题是Best Time to Buy and Sell Stock/的改进版. 本题中,可以买最多买进卖出两次股票. 买两次股票可以看成是第0~i天买进卖出以及第i+1~n-1天买进卖出两部分.这要枚举i并求出0th~ith的最大利益与(i+1)th~(n-1)th的最大利益之和的最大值就是买进卖出两次可以得到的最大利益.即状态转移方程: dp[0,n-1]=max{dp[0,k]+dp[k+1,n-1]},k=1,...,n-2 而只买进卖出一次的最大利润通过0th~ith可以求得. 这里求

Best Time to Buy and Sell Stock I &amp;amp;&amp;amp; II &amp;amp;&amp;amp; III

题目1: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 algori

[email&#160;protected] [121]Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock Total Accepted: 69292 Total Submissions: 206193 Difficulty: Medium 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

Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III

题目1: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 algori

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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 k transactions. 解题思路: 本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下: public i

[LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

LeetCode123 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. (Hard) Note:You may not engage in multiple transactions at the same tim