best time to Buy and Sell Stock IV solution

Source: Internet
Author: User

Questionsay you had an array for which the ith element was the price of a given stock on day i.design an algorithm to find The maximum profit. Transactions at the most K. For example, given prices = [4,4,6,1,1,4,2,5], and k = 2, return 6.Answer with DP solution. LOCAL[I][J] Represents the number of 0~i, up to J transactions, and the last trade must contain PRICES[J] (that is, the last day must be sold), the maximum value of the proceeds. GLOBAL[I][J] Represents the number of 0~i, up to J transactions, the maximum value of earnings. diff = prices[i]-prices[i-1]local[i][j] = max (global[i-1][j-1] + max (diff,0), Local[i-1][j]+diff) global[i][j] = max (loc AL[I][J], global[i-1][j]) local[i-1][j] + diff means i-1 days, buy Prices[i-1], and sell for the first day. Because Local[i-1][j] indicates that the last trade must contain prices[i-1], i.e. prices[i-1] must be sold. So it is possible to combine the transaction of I-1 Day with the trading of day I, that is, the maximum of J transactions, the last day of trading must sell prices[i]. GLOBAL[I-1][J-1] indicates i-1 days, up to j-1 transactions. The last day compares diff, if diff>0, then i-1 Day buys, the first day sells, if diff<0, then I day buys, the first day sells. Class Solution {/** * @param k:an integer * @param prices:given an integer array * @return: Maximum PROFIT */public in T Maxprofit (int k, int[] prices) {//write your code here if (prices = NULL | | prices.length = 0 | | k = = 0) {RETurn 0; } int len = prices.length; if (k >= len/2) {int profit = 0; for (int i = 1; i < Len; i++) {if (Prices[i] > Prices[i-1]) {profit + = (p Rices[i]-prices[i-1]); }} return profit; } int[][] Local = new Int[len][k + 1]; int[][] Global = new Int[len][k + 1]; for (int i = 0; i < len; i++) {local[i][0] = 0; global[i][0] = 0;} Arrays.fill (Local[0], 0); Arrays.fill (Global[0], 0); for (int i = 1, i < Len; i++) {for (int j = 1; j <= K; j + +) {int diff = prices[i]-prices[i-1]; Local[i][j] = M Ath.max (Local[i-1][j] + diff, Global[i-1][j-1] + Math.max (diff, 0)); GLOBAL[I][J] = Math.max (Local[i][j], global[i-1][j]); }} return global[len-1][k]; }};

Best time to Buy and Sell Stock IV solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.