Skip to content

Commit c3163e0

Browse files
committed
best-time-to-buy-and-sell-stock
1 parent 194fda6 commit c3163e0

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)