188. Best time to Buy and Sell Stock IV

Source: Internet
Author: User

    /** 188. best time to Buy and Sell Stock IV * 2016-6-6 by Mingyang * This topic my approach is to set up a number of transaction for the two-dimensional dp,t[i][j]i, J is the days * Also is to complete the maximum value of the I transaction within J days * T[i][j]=max 1. T[I][J-1]------> J Day does not trade at all * 2. (Prices[j]-prices[m]) +t[i-1][m]------all the maximum * for m=0,... j-1---that is, in M days to complete i-1 transactions, the last time by M buy, J Sell*/      Public intMaxprofit (intKint[] prices) {            intlen=prices.length; if(prices==NULL|| Len==0)              return0; int[] t=New int[K+1][prices.length];  for(inti=0;i<=k;i++) {t[i][0]=0; }            for(intj=0;j<prices.length;j++) {t[0][j]=0; }            for(inti=1;i<=k;i++){                for(intj=1;j<prices.length;j++){                   intMaxval=0;  for(intm=0;m<j;m++) {Maxval=math.max (maxval,prices[j]-prices[m]+t[i-1][m]); } T[i][j]=math.max (t[i][j-1],maxval); }           }           returnT[k][prices.length-1]; }

188. Best time to Buy and Sell Stock IV

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.