Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec27ad3

Browse files
committed
Create 0122-best-time-to-buy-and-sell-stock-ii
1 parent9bbbc7b commitec27ad3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Approach:
3+
If stock price is going up tomorrow just buy it today and sell it tomorrow.
4+
5+
Time complexity : O(n)
6+
Space complexity: O(1)
7+
8+
n is number of days.
9+
*/
10+
11+
classSolution {
12+
public:
13+
intmaxProfit(vector<int>& prices) {
14+
int ans =0;
15+
16+
// look into the next day and if you are making profit just buy it today.
17+
for(int i =1;i<prices.size();i++){
18+
if(prices[i]>prices[i-1]) ans += (prices[i]-prices[i-1]);
19+
}
20+
return ans;
21+
}
22+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp