Best Time to Buy and Sell Stock with Transaction Fee | Leetcode 714

preview_player
Показать описание

1) 0:00 Explaining the problem out loud
2) 1:10 Algorithm walkthrough
3) 10:15 Coding it up
4) 17:00 Complexity analysis

Рекомендации по теме
Комментарии
Автор

How well you explain!! I wish your reach increases...very soon

titanofchaos
Автор

Great Explanation! Was a bit confused in the start with the formulas but now its all clear !!

sahilshah
Автор

Just now searched for this question and you are almost live!

spetsnaz_
Автор

Since you only need prev days buy, sell values, you dont need an array O(n) --- it can be done with just 2 variables and so O(1) ....

dataman
Автор

nicely explained, it would had been more better if you would have talked about the intuition before starting the dry run.

mulayvaibhav
Автор

why are you substracting profit can you please explain

abhishekahlawat
Автор

Why are you subtracting profit for buying today?

dhruvdhiman
Автор

CAN BE SOLVED EASILY IN O(1) SPACE :

int n = prices.length;
if(prices == null || n == 0) return 0;

int buy = prices[0];
int profit = 0;
int sell = 0;
for(int i = 1; i < n; i++){
buy = Math.min(buy, prices[i] - profit);
sell = Math.max(profit, prices[i] - buy - fee);
profit = sell;
}
return profit;
}

adityabarodiya