木板最优切割利润最大_最多进行K笔交易的股票最大买卖利润
木板最優(yōu)切割利潤最大
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è)非常受歡迎的面試問題,目的是在最多進(jìn)行K筆交易時(shí),在股票買賣中獲得最大利潤 。 亞馬遜的采訪回合中已經(jīng)提到了這個(gè)問題。
Problem statement:
問題陳述:
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è)日期將其出售。 給定N天的股票價(jià)格( 數(shù)量為數(shù)組)和正整數(shù)K ,找出一個(gè)人最多可以進(jìn)行K筆交易的最大利潤。 某筆交易在某天買入股票,而在將來的另一天賣出股票,只有在上一筆交易完成后才能開始新交易。
Input:K=3N=7Stock prices on N days:10 25 38 40 45 5 58Output:88Example:
例:
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 = 2Explanation:
說明:
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和賣出日股購買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è)條件來制定最大獲利情況。
Let,
讓,
f(t,i)=maximum profit upto ith day and t transactionsConsidering 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 58Below is a part of recursion tree which can show that how many overlapping sub problems there will be.
下面是遞歸樹的一部分,它可以顯示將有多少個(gè)重疊的子問題。
Figure 1: Partial recursion tree to show overlapping sub-problems
圖1:部分遞歸樹顯示重疊的子問題
So we need dynamic programming...
所以我們需要?jiǎng)討B(tài)編程...
Problem Solution
問題方案
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=7Try 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)切割利潤最大
總結(jié)
以上是生活随笔為你收集整理的木板最优切割利润最大_最多进行K笔交易的股票最大买卖利润的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 方法 示例_Java语言环境g
- 下一篇: Java ObjectInputStre