#Leetcode

Sdílet
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...

Komentáře • 18

  • @MP-ny3ep
    @MP-ny3ep Před rokem +6

    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 !

  • @replymadhug
    @replymadhug Před měsícem

    Very clean explanation!!! Amazing.. after listening I was able to do the problem by myself :)

  • @satadhi
    @satadhi Před 8 měsíci

    NIce one !

  • @satakshipal5008
    @satakshipal5008 Před rokem

    very nice explanation!!

  • @dvelagap
    @dvelagap Před rokem

    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;
    }

  • @pandeyshashidhar
    @pandeyshashidhar Před rokem

    Thak You Soooooooo much for solution

  • @nishchayatejasri6322
    @nishchayatejasri6322 Před 3 lety +3

    Perfectly understood

  • @prajwalmanojkumargawai6298

    thanks

  • @harikuduva
    @harikuduva Před 3 lety +1

    Thank you for going through this in detail.

  • @sarthakarora1042
    @sarthakarora1042 Před rokem

    Thanks for making me understand this !

  • @andreyvalverde4780
    @andreyvalverde4780 Před 2 lety

    The explanation was superb!

  • @venkatasriharsha4227
    @venkatasriharsha4227 Před 2 lety

    Great one! u can just neglect using space I guess, as we are bothered about previous transactions only.

  • @chaitanyagujarathi4318
    @chaitanyagujarathi4318 Před 3 lety +1

    Can u please explain in top to down approach?

  • @WdnUlik2no
    @WdnUlik2no Před 2 lety

    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.

  • @navneetkushwaha1830
    @navneetkushwaha1830 Před 2 lety

    great explanation ma'am ❤

  • @chetansahu1505
    @chetansahu1505 Před 3 lety

    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 :(

  • @2121sourabh
    @2121sourabh Před 3 lety

    good work

  • @replymadhug
    @replymadhug Před měsícem

    you have move your palette to the right hand side. its hiding the entire view