leetcode第十七周解题总结

来源:互联网 发布:java 递归删除子节点 编辑:程序博客网 时间:2024/05/16 10:48

121. Best Time to Buy and Sell Stock

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

题意解析: 给出每天股票的价格,只能做一次交易,问最大收益。
解题思路: 动态规划。设在第n天卖出股票的最大收益为f(n),

d=prices[n]prices[n1]

f(n)=max((f(n1)+d),d)

这样的话,一次交易买卖股票的最大收益就是n天之中最大的收益。

class Solution {public:    int maxProfit(vector<int>& prices) {        int maxprofit = 0;        int n = prices.size();        int profit;        profit = 0;        for(int i = 1; i < n; i++) {            int d = prices[i] - prices[i -1];            if(profit< 0) profit = 0;            profit = profit+ d;            maxprofit = max(profit, maxprofit);        }        return maxprofit;    }};

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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题意解析: 给出每天股票的价格,能做任意次交易,问最大收益。
解题思路: 一次交易可以拆分为多个小交易,当可以做任意次交易时,可以使用贪心算法,只要收益就进行交易,找到所有收益相加即可。

class Solution {public:    int maxProfit(vector<int>& prices) {        int maxPro = 0;        int n = prices.size();        for(int i = 0; i < n - 1; i ++) {           if(prices[i] < prices[i + 1]) {               maxPro += prices[i + 1] - prices[i];           }        }        return maxPro;    }};

123. 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, you must sell the stock before you buy again).

题意解析: 给出每天股票的价格,只能做最多两次交易,问最大收益。
解题思路: 这题比较难。思路依然是动态规划,设f(n)为第n天前做一次交易能获得的最大收益,g(n)为第n天后做一次交易能获得的最大收益。这样只需要遍历一遍中断点就能求出两次交易的最大收益。

而做第一题的时候对每个点取最大,再全部取最大,而这道题需要对各个区间取最大,所以要重新思考一种更好的方法。
仔细思考,其实一个区间内的一次交易最大收益等于其中的最大值减去最小值,但是最大值要在最小值后面。
所以当新加入一天,更新最小值,判断最大值是否改变。
假设minprice是之前区间内的最小值,

minprice=min(minprice,prices[n1])

f(n)=max(f(n1),prices[n]minprice)

而g(n)为第n天后做一次交易能获得的最大收益,需要反向遍历,即更新最大值,判断新加入是否为最小值,
假设maxprice是后面区间内的最大值,

maxpricc=max(maxprice,price[n+1])

g(n)=max(g(n1),maxpriceprices[n])

最后遍历找出f和g相加的最大值。

class Solution {public:    int maxProfit(vector<int>& prices) {        int maxprofit = 0;        int n = prices.size();        if(n <= 1) return 0;        vector<int> preprofit(n);        vector<int> postprofit(n);        preprofit[0] = 0;        int minp = prices[0];        for(int i = 1; i < n; i++) {            minp = min(minp,prices[i-1]);            preprofit[i] = max(preprofit[i-1],prices[i] - minp);        }        postprofit[n-1] = 0;        int maxp = prices[n-1];        for(int i = n-2; i >= 0; i--) {            maxp = max(maxp,prices[i+1]);            postprofit[i] = max(postprofit[i+1], maxp - prices[i]);        }        for(int i = 0; i < n; i++) {            int total = preprofit[i] + postprofit[i];            maxprofit = max(maxprofit, total);        }        return maxprofit;    }};
原创粉丝点击