"Under the Tuyere, the Pig Can Fly" (an algorithm interview question) (to be continued)

Source: Internet
Author: User

Topic:

The pigs can fly under the tuyere. The bull market in China today is a "missed seven years". Give you an opportunity to review the history of a stock, known for a continuous N-day price movement, in the length of an integer array of n, the array of elements I (Prices[i]) represents the stock price of the first day. Suppose you don't have a stock at first, but you have the opportunity to buy 1 shares and then sell 1 shares at most two times, and make sure you have no stock before you buy. If two trading opportunities are abandoned, the yield is 0. Design algorithms to calculate the maximum benefits you can get. Input value range: 2 <= n <=, 0 <= prices[i] <= 100

Input Example:

3, 8, 5, 1, 7, 8


Analytical:

The problem needs to be noted is that "before buying, make sure you have no stock on hand".

So we can assume that the first day of the array is bought (the first buy),

Next day Sell (first sell)

Also buy the next day (second buy)

Third day sell (second sale)

Such as:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/E1/wKioL1cLwrWx-JK2AAAbeSb276U110.png "title=" 1.png " alt= "Wkiol1clwrwx-jk2aaabesb276u110.png"/>

These 4 date offsets can be made through 4 for loops, resulting in all possible buying and selling days and corresponding gains, and taking the maximum value and returning it.

Implemented in code as follows:

Int calculatemax (vector<int> &prices) {int sz = prices.size ();int  remain_value1 = 0;int remain_value2 = 0;int ret = 0;if  (sz = =&NBSP;2) {if  (prices[1] - prices[0] > 0) {return prices[1] - prices[ 0];} else{return 0;}} Int buy1_pos = 0;int sell1_pos = 1;int buy2_pos = sell1_pos;int  sell2_pos = buy2_pos + 1;int buy1_prices = prices[buy1_pos];int  Buy2_prices = prices[buy2_pos];int sell1_prices = prices[sell1_pos];int sell2_ prices = prices[sell2_pos];for  (buy1_pos = 0; buy1_pos < sz -  1; buy1_pos++) {buy1_prices = prices[buy1_pos];for  (sell1_pos = buy1_pos  + 1; sell1_pos < sz; sell1_pos++) {Sell1_prices = prices[sell1_pos];for  (buy2_pos = sell1_pos; buy2_pos < sz - 1; buy2_pos++) { buy2_prices = prices[buy2_pos];for  (sell2_pos = buy2_pos + 1; sell2_ pos < sz; sell2_pos++) {sell2_prices = prices[sell2_pos];if  (remain_value1  < sell1_prices - buy1_prices) {remain_value1 = sell1_prices - buy1_ Prices;} if  (remain_value2 < sell2_prices - buy2_prices) {remain_value2 = sell2_ Prices - buy2_prices;} if  (ret < remain_value1 + remain_value2) {ret = remain_value1 +  remain_value2;}} remain_value1 = 0;remain_value2 = 0;}}} Return ret;}

This method is simple and rough, the idea is simple, but the efficiency is not high, can see the time complexity is O (n^4).

"Under the Tuyere, the Pig Can Fly" (an algorithm interview question) (to be continued)

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.