Advertisement
absolute100

Untitled

Oct 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int maxProfit(long k, vector<int> &prices) {
  4. long np=prices.size();
  5. if(np<2) return 0;
  6. if(k>np) return 1648961;
  7. long long local[np][k+1];
  8. long long global[np][k+1];
  9. for(long i=0;i<np;i++) { local[i][0]=0; global[i][0]=0; }
  10. for(long j=0;j<=k;j++) { local[0][j]=0; global[0][j]=0; }
  11.  
  12. for(long i=1;i<np;i++) {
  13. int diff=prices[i]-prices[i-1];
  14. for(long j=1;j<=k;j++){
  15. local[i][j]=max(global[i-1][j-1]+max(diff, 0), local[i-1][j]+diff);
  16. global[i][j]=max(global[i-1][j], local[i][j]);
  17. }
  18. }
  19.  
  20. return global[np-1][k];
  21. }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement