#Leetcode
Vložit
- čas přidán 15. 03. 2021
- You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.
Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [1,3,2,8,4,9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
- Buying at prices[0] = 1
- Selling at prices[3] = 8
- Buying at prices[4] = 4
- Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
Example 2:
Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6
Link to challenge: leetcode.com/problems/best-ti...
Whenever I want to find a solution for a problem on youtube, I first make sure whether you've published the solution or not. Your logic & code explanation are simply too good and super easy to understand. Thank you so much !
Very clean explanation!!! Amazing.. after listening I was able to do the problem by myself :)
NIce one !
very nice explanation!!
awesome explanation. This is the second video i watched after the one to find the max sum in a tree with the restriction not to pick adjacent nodes. Space optimized
private int maxProfitDPSpaceOptimized(int[] prices, int fee){
int n = prices.length;
int ans = 0; // sell state
int hold = -(prices[0] + fee); //buy state
for (int i = 1; i < n; i++){
int temp = Math.max(hold, ans - prices[i] - fee);
ans = Math.max(ans, hold + prices[i]);
hold = temp;
}
return ans;
}
Thak You Soooooooo much for solution
Perfectly understood
thanks
Thank you for going through this in detail.
Thanks for making me understand this !
The explanation was superb!
Great one! u can just neglect using space I guess, as we are bothered about previous transactions only.
Can u please explain in top to down approach?
Great explanation! This question was confusing because the problem description stated that the transaction fee should be applied for "every" transaction, but it seems its only applied when buying the stock.
great explanation ma'am ❤
Awesome. Splendid explanation. You deserve more than just a subs, a share, a like and a comment. Keep up the work. Keep uploading such nice content. I'm good at other DS and algos but DP :(
good work
you have move your palette to the right hand side. its hiding the entire view