Buy and sell stocks and realize your dreams

Give the prices of a group of stocks and put them in the array p, which are the prices of one stock from the first day to the Nth day.

It is only allowed to buy and sell once, choose to buy one day, buy only one share, and choose to sell it on a subsequent day for the greatest profit.

Idea 1, violence, two-tier cycle to determine which two-day combination has the highest profit

Idea 2, what are the dreams of investors? Do n’t you just know the price of future stocks? Now the title has realized your dream, you tell me that you will not make money, I, I, and I will hammer you and me.

After all, the day to buy it? Some people say that the lowest point is to buy, what if the highest point is before the lowest point? Hard to say. So we calculate the maximum profit that can be obtained by buying every day. For example, if you buy on the third day, you are smart. Of course, you need to find the day after which the stock price is the highest, and you can sell it on this day. what? You say you are stupid? Come here, how much is the mobile phone number micro-signal bank card number? Let's talk in private, I teach you to be smart.

Open an array a [], a [i] represents the maximum profit that can be obtained by buying on the i day, and finally the maximum value in the array can be taken. It seems a bit dynamic programming

The same is true for selling. The maximum profit you can get by selling on a certain day can be recorded as the previous minimum.

Guess you like

Origin www.cnblogs.com/lxzbky/p/12687585.html