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.

思路:变相的求最大子数组,是算法导论上的例题,书上用分治法做的。

若prices=[1,4,2,8,1],每两数相减所得的利润,即头一天买入,第二天卖出。

profit=[0,3,-2,6,-7],可以看到第一天买入,最后一天卖出的profit为3+(-2)+6+(-7)=0

代码:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int len=prices.size();
        int res=0;
        int temp=0;
        if(len==0) return res;
        vector<int> profit(len,0);
        vector<int> dp(len,0);
        for(int i=0;i<len;++i){
            if(i==0) {profit[i]=0;continue;}
            profit[i]=prices[i]-prices[i-1];
        }

        dp[0]=profit[0];
        for(int i=1;i<len;++i){
            dp[i]=max(dp[i-1]+profit[i],profit[i]);
            if(dp[i]>res)
                res=dp[i];
        }
        return res;
    }
};
时间: 2024-10-12 09:20:22

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 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. Exam

LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)

Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions 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, b

[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可以求得. 这里求

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: 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), desi

leetcode 之 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] Best time to buy and sell stock iii 买卖股票的最佳时机

Say you have an array for which the i th 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

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][Java] 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 (i

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