【题解】【数组】【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 to find the maximum profit.

思路

Best Time to Buy and Sell Stock这题似乎就是codility中的MaxProfit,codility把这题归到Maximum slice problem一章,不过这题有更直观的解法

代码

int solution(const vector<int> &A) {
    if(A.size() < 2) return 0;
    int mina = A[0];//i之前的最小值
    int maxP = 0;//整体最大收益
    for(int i : A){
        int P = i - mina;//卖出i的收益
        if(P > maxP) maxP = P;
        if(i < mina) mina = i;//更新最小值
    }
    return maxP;
}

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

思路

II这道题反而比较像一个Maximum slice problem,有一个重要的观察是prices[i] - prices[i-2] = (prices[i] - prices[i-1]) + (prices[i-1] - prices[i-2]),因此我们可以抽象成相邻元素差序列的最大字段和问题。

代码

int maxProfit(vector<int> &prices) {
    int n = prices.size();
    if(n <= 1) return 0;
    int profit = 0;
    for(int i = 1; i < n; i++){
        int gap = prices[i] - prices[i-1];
        if(gap > 0) profit += gap;
    }
    return profit;
}

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

思路

这题很经典,跟MaxDoubleSliceSum的第二种思路有相似之处,枚举中间点,正反扫一遍

代码

int maxProfit(vector<int> &prices) {
    int n = prices.size();
    if(n <= 1) return 0;
    //求[0,i]这个子序列的交易最大值
    int min = prices[0];
    vector<int> maxprofitA(n,0);
    for(int i = 1; i < n; i++) {
        int price = prices[i];
        int diff = price - min;
        if(maxprofitA[i-1] < diff) maxprofitA[i] = diff;
        else maxprofitA[i] = maxprofitA[i-1];
        if(price < min) min = price;
    }
    //求[i,n-1]这个子序列的交易最大值
    int max = prices[n-1];
    vector<int> maxprofitB(n,0);
    for(int i = n-2; i >= 0; i--) {
        int price = prices[i];
        int diff = max - prices[i];
        if(maxprofitB[i+1] < diff) maxprofitB[i] = diff;
        else maxprofitB[i] = maxprofitB[i+1];
        if(price > max) max = price;
    }
    //把两次交易的分割点遍历一遍
    int maxprofit = maxprofitA[n-1];//对应着在[0,n-1]进行一次交易
    for(int i = 0; i < n-1; i++) {
        maxprofitA[i] += maxprofitB[i+1]; //注意下标的含义
        if(maxprofit < maxprofitA[i]) maxprofit = maxprofitA[i];
    }
    return maxprofit;
}

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

时间: 2024-10-15 10:33:11

【题解】【数组】【DP】【Codility】Best Time to Buy and Sell Stock的相关文章

【LeetCode】【Python题解】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). Ho

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

Leetcode problem-122 Best Time to Buy and Sell Stock II 题解

Leetcode Problem-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, bu

Lintcode393 Best Time to Buy and Sell Stock IV solution 题解

[题目描述] 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 k transactions. Notice:You may not engage in multiple transactions at the same ti

【数组】Best Time to Buy and Sell Stock I/II

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), design an algo

【leetcode刷题笔记】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,

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

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

[leetcode]_Best Time to Buy and Sell Stock I &amp;&amp; II

一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换.要求只买卖一次,获得最大收益. 思路:一开始我认为是寻找最大.最小值,但由于最大值不一定总是出现在最小值的后面,因此WA. 参考思路:DP.对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比. 代码: 1 public int max