Skip to content

Commit

Permalink
65: Re-submit
Browse files Browse the repository at this point in the history
  • Loading branch information
sankichi92 committed Dec 31, 2023
1 parent 6656117 commit 1f15094
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions 65-best-time-to-buy-and-sell-stock-with-transaction-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@
// 2 [_, _, _, 5, 5, 6]
// 8 [_, _, _, _, 5, 6]
// 4 [_, _, _, _, _, 8]
use std::cmp;

pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
let days = prices.len();

let mut profits = vec![0; days];
let mut last_buy = -prices[0];
let mut profit = 0;

for (buy_day, buy_price) in prices.iter().enumerate().take(days - 1) {
let current_profit = profits[buy_day];

for (sell_day, sell_price) in prices.iter().enumerate().skip(buy_day + 1) {
profits[sell_day] = cmp::max(
current_profit + sell_price - buy_price - fee,
cmp::max(profits[sell_day], profits[sell_day - 1]),
)
}
for price in prices.into_iter().skip(1) {
let current_profit = profit;
profit = profit.max(last_buy + price - fee);
last_buy = last_buy.max(current_profit - price);
}

profits[days - 1]
profit
}

#[cfg(test)]
Expand Down

0 comments on commit 1f15094

Please sign in to comment.