日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

木板最优切割利润最大_最多进行K笔交易的股票最大买卖利润

發(fā)布時(shí)間:2025/3/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 木板最优切割利润最大_最多进行K笔交易的股票最大买卖利润 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

木板最優(yōu)切割利潤(rùn)最大

This is a very popular interview problem to find maximum profit in stock buying and selling with at most K transactions. This problem has been featured in the interview rounds of Amazon.

這是一個(gè)非常受歡迎的面試問(wèn)題,目的是在最多進(jìn)行K筆交易時(shí),在股票買(mǎi)賣(mài)中獲得最大利潤(rùn) 。 亞馬遜的采訪(fǎng)回合中已經(jīng)提到了這個(gè)問(wèn)題。

Problem statement:

問(wèn)題陳述:

In stock market, a person buys a stock and sells it on some future date. Given the stock prices of N days in form of an array Amount and a positive integer K, find out the maximum profit a person can make in at most K transactions. A transaction is buying the stock on some day and selling the stock at some other future day and new transaction can start only when the previous transaction has been completed.

在股票市場(chǎng)中,一個(gè)人購(gòu)買(mǎi)股票并在將來(lái)的某個(gè)日期將其出售。 給定N天的股票價(jià)格( 數(shù)量為數(shù)組)和正整數(shù)K ,找出一個(gè)人最多可以進(jìn)行K筆交易的最大利潤(rùn)。 某筆交易在某天買(mǎi)入股票,而在將來(lái)的另一天賣(mài)出股票,只有在上一筆交易完成后才能開(kāi)始新交易。

Input:K=3N=7Stock prices on N days:10 25 38 40 45 5 58Output:88

Example:

例:

Number of maximum transactions: 3Total number of days, N: 7To achieve maximum profit:Stock bought at day1 (-10)Stock sold at day5 (+45)Stock bought at day6 (-5)Stock sold at day7 (+58)Total profit = 88Total transactions made = 2

Explanation:

說(shuō)明:

Let there are N number of days for transactions, say x1, x2, ..., xn

設(shè)交易的天數(shù)為N ,例如x 1 ,x 2 ,...,x n

Now for any day xi

現(xiàn)在任何一天x

  • Don't do any transaction on the day xi

    x 當(dāng)天不做任何交易

  • Transact on day xi, i.e., buy stock on some day xj and sell on day xi where j<i and i,j ? N

    一天的Transact X I,即,在某些天×j和賣(mài)出日股購(gòu)買(mǎi)X I其中j <i和I,J??

  • Total number of transactions can be made at most = K

    最多可以進(jìn)行的交易總數(shù)= K

    Now we can formulate the maximum profit case using above two condition.

    現(xiàn)在,我們可以使用以上兩個(gè)條件來(lái)制定最大獲利情況。

    Let,

    讓,

    f(t,i)=maximum profit upto ith day and t transactions

    Considering the above two facts about xi

    考慮關(guān)于x i的上述兩個(gè)事實(shí)

    f(t,i)=f(t,i-1) if there is no transaction made on day xi ...(1)Max(f(t-1,j)+amount[i]-amount[j]) where j<i and i,j ? N if there is transaction on day xi ...(2)Obviously, to maximize the profit we would take the maximum of (1) and (2)Number of maximum transactions: 3Total number of days, N: 7Stock prices on the days are:10 25 38 40 45 5 58

    Below is a part of recursion tree which can show that how many overlapping sub problems there will be.

    下面是遞歸樹(shù)的一部分,它可以顯示將有多少個(gè)重疊的子問(wèn)題。



    Figure 1: Partial recursion tree to show overlapping sub-problems

    圖1:部分遞歸樹(shù)顯示重疊的子問(wèn)題

    So we need dynamic programming...

    所以我們需要?jiǎng)討B(tài)編程...

    Problem Solution

    問(wèn)題方案

    Recursive Algorithm:

    遞歸算法:

    Function(t, i): //f(t, i)If i=0f(t, i)=0If t=0f(t, i)=0f(t, i)= f(t, i-1); //no transaction on day xiFor j =0: iFind max(f(t-1,j) + amount(i)- amount(j)End ForIf the maximum found > f(t, i)update f(t, i)End IFReturn f(t, i)

    Conversion to DP

    轉(zhuǎn)換為DP

    For tabulation we need a 2D array, DP[k+1][n] to store f(t,i)Base case,for i 0 to k, DP[i][0]=0 //no profit on 0th dayfor i 0 to n-1, DP[0][j]=0 //no profit on 0 transactionTo fill the higher values,for t=1 to kfor i =1 to n-1DP[t][i]=DP[t][i-1]for j= 0 to i-1 //buying on jth day and selling on ith day DP[t][i]=max(DP[t][i],DP[t-1][j]+ amount[i] –amount[j])End forEnd forEnd forResult would be f(k,n) that is value of DP[k][n-1]

    Initial DP table

    初始DP表

    DP[4][7] //K=3, N=7

    Try yourself to compute the DP table manually following the above algorithm and find out the result. Take some small example if necessary.

    嘗試按照上述算法手動(dòng)計(jì)算DP表并找出結(jié)果。 如有必要,舉一些小例子。

    C++ implementation:

    C ++實(shí)現(xiàn):

    #include <bits/stdc++.h> using namespace std;int Max_profit(vector<int> amount, int n, int k) {int DP[k + 1][n];memset(DP, 0, sizeof(DP));//on 0th dayfor (int i = 0; i <= k; i++)DP[i][0] = 0;//on 0 transaction madefor (int i = 0; i <= n; i++)DP[0][i] = 0;for (int t = 1; t <= k; t++) {for (int i = 1; i < n; i++) {DP[t][i] = DP[t][i - 1];int maxV = INT_MIN;//buying on jth day and selling on ith dayfor (int j = 0; j < i; j++) {if (maxV < DP[t - 1][j] + amount[i] - amount[j])maxV = DP[t - 1][j] + amount[i] - amount[j];}if (DP[t][i] < maxV)DP[t][i] = maxV;}}return DP[k][n - 1]; }int main() {int n, item, k;cout << "Enter maximum no of transactions:\n";cin >> k;cout << "Enter number of days\n";cin >> n;vector<int> amount;cout << "Enter stock values on corresponding days\n";for (int j = 0; j < n; j++) {scanf("%d", &item);amount.push_back(item);}cout << "Maximum profit that can be achieved: " << Max_profit(amount, n, k) << endl;return 0; }

    Output

    輸出量

    Enter maximum no of transactions: 3 Enter number of days 7 Enter stock values on corresponding days 10 25 38 40 45 5 58 Maximum profit that can be achieved: 88

    翻譯自: https://www.includehelp.com/icp/maximum-profit-in-stock-buy-and-sell-with-at-most-k-transaction.aspx

    木板最優(yōu)切割利潤(rùn)最大

    總結(jié)

    以上是生活随笔為你收集整理的木板最优切割利润最大_最多进行K笔交易的股票最大买卖利润的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。