*************
C++
topic: 122. 买卖股票的最佳时机 II - 力扣(LeetCode)
*************
Inspect the topic:
|
How's your weekend? Overtime work or meet friends, are all kinds of spending time, which we have only once. At very first, I always complained about the unreasonable works and unhealthy relationships. But now, I donnot wanna do it because it reminds me the unhapppy things while I have my own time. And I changed a lot those years. Sometimes I really enjoy the smoking moment because in the smoking period, maybe 3 - 5 minites, that's yours. No matter the working things, or the peoples, it is just you.I never buy a stock but maybe in this topic I will figure out the principle how to warn money from a stock.
I have no idea about this topic. No familiar topics I've ever done.
Maybe the aesthetic of silance works?
The gift of mine tells me that buy in when the price is low and sale it out when the price is high.
|
if price[i + 1] > price[i], benifit = price[i + 1] - price[i]
class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0;
for(int i = 0; i < prices.size() - 1; i++) {
if(prices[i + 1] > prices[i]) {
profit += prices[i + 1] - prices[i];
}
}
return profit;
}
};
I then have a think about it, why this work?
For mathematic, it is ez to understand:
if i have the price:
In groups of two, each group is monotonically increasing. That's the method.
Happy UR monday.