Skip to content

Commit 6b27756

Browse files
authored
Merge pull request #1858 from DaleSeo/main
[DaleSeo] WEEK 05 solutions
2 parents ca2d7b9 + c3163e0 commit 6b27756

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn max_profit(prices: Vec<i32>) -> i32 {
5+
let mut min_price = i32::MAX;
6+
let mut max_profit = 0;
7+
for price in prices {
8+
if price < min_price {
9+
min_price = price;
10+
}
11+
if price - min_price > max_profit {
12+
max_profit = price - min_price;
13+
}
14+
}
15+
max_profit
16+
}
17+
}

0 commit comments

Comments
 (0)