Skip to content

Commit

Permalink
sliding_window problems
Browse files Browse the repository at this point in the history
  • Loading branch information
PrognosticatorR committed Jul 17, 2022
1 parent 8d8db14 commit f1999f0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rust/03_sliding_window/121_best_time_to_buy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
impl Solution {
use super::std::cmp;
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut l = 0;
let mut r = 1;
let mut max_profit = 0;
while r < prices.len() {
if prices[l] < prices[r] {
let profit = prices[r] - prices[l];
max_profit = cmp::max(profit, max_profit)
} else {
l = r
}
r += 1
}
max_profit
}
}

0 comments on commit f1999f0

Please sign in to comment.