Leetcode best time to buy and other stock II

Source: Internet
Author: User
/*** say you have an array for which the ith element is the price of a given stock on day I. * design an algorithm to find the maximum profit. you may complete as your transactions as you like * (ie, buy one and every one share ofthe stock multiple times ). * However, you may not engage in multiple transactions at the same time (ie, you must wait the stock before you buy again ). ** subject requirements can be bought and sold multiple times, At the same time, there can be only one stock in your hand. That is to say, the stock in the hand must be sold in the next purchase. Buying and selling can happen in the same * subscript of the vector. For example, the input vector is [6, 9, 12, 8, 4, 11, 2, 1, 9]. you can buy and sell a vector [I] at the same time. * Therefore, the profit is (-6 + 9) + (-9 + 12) + (-4 + 11) + (-1 + 9) * In this way, you can buy before each ascending subsequence and sell at the end of the ascending subsequence. It is equivalent to obtaining the benefits of all ascending subsequences. */Class solution {public: int maxprofit (vector & prices) {int I = 0; int size = prices. size (); If (size <2) {return 0;} int totalprofit = 0; for (I = 1; I Prices [I-1]) {totalprofit + = Prices [I]-Prices [I-1] ;}} return totalprofit ;}};

leetcode best time to buy and stock Stock II

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.