String Compression II - Leetcode 1531 - Python

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link: leetcode.com/problems/string-...
    0:00 - Read the problem
    0:12 - Drawing Explanation
    10:19 - Coding Explanation
    leetcode 1531
    #neetcode #leetcode #python

Komentáře • 72

  • @MP-ny3ep
    @MP-ny3ep Před 7 měsíci +14

    They saved the best for the last😂. Thank you for explaining so well, it was a really tough problem.

  • @s20061002
    @s20061002 Před 7 měsíci +1

    Well explanation that saved my life!!!
    I am able to apply what you have taught in the video and solve today's leetcode question. This is encouraging for me !!!

  • @Sinders
    @Sinders Před 7 měsíci +10

    changing one of the base cases from i == len(s) to len(s) - i == k helped my runtime dramatically

    • @khushmeetchugh8669
      @khushmeetchugh8669 Před 7 měsíci

      it removed my TLE bro thanks

    • @gmasid
      @gmasid Před 7 měsíci

      Thanks bro, this help TLE too!

    • @raghavbhatia5548
      @raghavbhatia5548 Před 7 měsíci

      @@khushmeetchugh8669 C++ magic

    • @yang5843
      @yang5843 Před 7 měsíci

      This removed my TLE for Java, thank you

    • @karanshah2283
      @karanshah2283 Před 7 měsíci +1

      can you please explain the logic behind this base case change?

  • @chaunceyzhang6595
    @chaunceyzhang6595 Před 7 měsíci

    Thanks for your persistence do this video series!

  • @user-ec6jp7di7o
    @user-ec6jp7di7o Před 7 měsíci

    Thank you for explaining the solution. you are really brilliant and extraordinary.

  • @GuruPrasadShukla
    @GuruPrasadShukla Před 7 měsíci

    great explaination man!
    keep going bro!

  • @pradyumnasingh9555
    @pradyumnasingh9555 Před 7 měsíci

    thank you for such an easy explanation :)

  • @bhavikransubhe7244
    @bhavikransubhe7244 Před 7 měsíci +6

    Today's daily leetcode challenge was difficult.
    Need to look for video 😂

  • @thangavn8875
    @thangavn8875 Před 6 měsíci

    phenomenal work, you gained a sub and a follower 💯

  • @muhammadsodiqtoirov3204
    @muhammadsodiqtoirov3204 Před 7 měsíci

    Thanks bro ! Good explanation Even gpt couldn't solve this problem ...

  • @tranquoctoan1996
    @tranquoctoan1996 Před 7 měsíci

    Thank you, it saved me

  • @krateskim4169
    @krateskim4169 Před 7 měsíci

    Thank you so much

  • @il5083
    @il5083 Před 7 měsíci

    Is using func_tools cache to save sometime a bad idea in an interview? (for memoization)

  • @gary1614
    @gary1614 Před 5 měsíci

    This is a very difficult question. Thank you for making this video. It makes a little bit more sense now. lol

  • @ronniey4231
    @ronniey4231 Před 7 měsíci

    Can’t love your videos more😂

  • @Drdarker
    @Drdarker Před 7 měsíci +3

    questions like this is almost impossible to solve in a real interview

  • @w702897028970289
    @w702897028970289 Před 7 měsíci

    What an explanation...

  • @chien-yucode992
    @chien-yucode992 Před 7 měsíci

    So amazing

  • @SiddharthReddy16
    @SiddharthReddy16 Před 7 měsíci

    No way you actually make difficult problems with such clarity! I visit this channel every day, no matter how easy/hard the daily question is just to see your approach.

  • @sauravsingh4497
    @sauravsingh4497 Před 7 měsíci

    Off topic but the discord link in the description Is not working (i wanted to join the server)

  • @aryakadam7892
    @aryakadam7892 Před 7 měsíci

    thank you

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

    best explanation for this question. I was so traumatized until seeing this. Thanks for sharing

  • @gnaneswarganta9920
    @gnaneswarganta9920 Před 7 měsíci +1

    like what if we condensed the string first and later doing the computations

  • @RajGupta-cu9hi
    @RajGupta-cu9hi Před 7 měsíci

    How to convert it to 2-dp because some solve it in....?

  • @caothanhluan1813
    @caothanhluan1813 Před 7 měsíci

    God tier explanation but TypeScript compiler says it TLE when using Map()😂😂😂

  • @shashankjoshi8250
    @shashankjoshi8250 Před 7 měsíci +1

    Heyy Bro, your solution and way of explanation was amazing but still I am trying to understand this and how can I come up with this level of intuition to solve the problem. I was able to come up until greedy approach and got 74 passed.

  • @shreyashpandey9351
    @shreyashpandey9351 Před 7 měsíci

    How will I be able to develop intuition like that?

  • @ish90917
    @ish90917 Před 7 měsíci

    do we add cache evetime in question of DP or backtracking ? If not when do we add them ?

    • @suhaanbhandary4009
      @suhaanbhandary4009 Před 7 měsíci +1

      We add cache when we know that for a given set of input the output is same and their should also be overlapping sub-problems. In backtracking cache is of no use as mostly the sub problems are independent and doesn't occur more than once eg: Sudoku, but in DP same sub-problems occur eg: Fibonacci(try to draw tree of recursive call to better visualize).

    • @pritz9
      @pritz9 Před 7 měsíci

      To memoize the recursive solution, otherwise you are gonna get a TLE.

  • @user-ox2ik5jf4z
    @user-ox2ik5jf4z Před 6 měsíci +1

    How to do the opposite?

  • @susmitsingh1775
    @susmitsingh1775 Před 7 měsíci +1

    man fuck this problem, took me a bloody day to solve

  • @keremt8727
    @keremt8727 Před 6 měsíci

    Shouldn't we also consider the case of deleting s[i] when it is the same as the prev character?
    Like if we have the string "aaaaaaaaaaaaaaaaaaaaaaaaa..." (25 times a) and the k is 20, then we need to delete so that it is not a25, but a5 instead.

  • @binitrajshah9354
    @binitrajshah9354 Před 7 měsíci

    didn't understand why it fail in greedy approach here?
    even when we consider our string as s = ''aaaaaaaaabaaaaaaaaac' and k = 2
    s compressed = a9ba9c
    so deleting minimum window freq element here ''b' & 'c'
    implie "a18" with lenght 3.
    can someone explain this?

  • @BTCSEDarshan
    @BTCSEDarshan Před 7 měsíci

    Saviour !

  • @racoon-thespy7062
    @racoon-thespy7062 Před 7 měsíci

    bruhh, in the morning doing the problem, guy has already got the solution ready, hats off

  • @ngneerin
    @ngneerin Před 7 měsíci +5

    I see hard and I quit
    I see medium and I watch
    I see easy and I solve

  • @sidsudhir8231
    @sidsudhir8231 Před 7 měsíci +1

    clutchhh

  • @w702897028970289
    @w702897028970289 Před 7 měsíci

    I wrote it in Java version but 122/144 got TLE... Why

  • @shreehari2589
    @shreehari2589 Před 7 měsíci

    How the heck will I come up with this solution on spot in an interview, these problems are real tough

  • @tanishq8211
    @tanishq8211 Před 7 měsíci

    I dont understand why he did ... note updated the deleted character? can somebody explain?

    • @0ManPiano0
      @0ManPiano0 Před 7 měsíci

      Don’t understand your question

  • @tony7948
    @tony7948 Před 3 měsíci

    If I get this on an interview. I'm just going to walk out

  • @chiragsrivastava4217
    @chiragsrivastava4217 Před 7 měsíci

    class Solution:
    def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
    @cache
    def dfs(i, k, prev, prev_count):
    if k

  • @ajaybabua9735
    @ajaybabua9735 Před 7 měsíci

    Can someone please explain me about the increment part 14:04 to 16:00?

    • @w702897028970289
      @w702897028970289 Před 7 měsíci +3

      Because when we encounter
      1 -> 2 (a -> a2)
      9 -> 10(a9 -> a10)
      99 -> 100(a99 -> a100)
      all of these cases will result in answer to be plus by 1
      and the rest of the case don't need to do this because the length remain the same like
      3 -> 4(a3 ->a4)

    • @aswinnath8580
      @aswinnath8580 Před 7 měsíci

      your explanation helped me to understand that part.@@w702897028970289

  • @abhisheksuman4510
    @abhisheksuman4510 Před 7 měsíci

    This aproach is giving runtime error due to stack overflow.

  • @shawn_yg1394
    @shawn_yg1394 Před 7 měsíci

    The example2 is indicating that minheap does not work but I still waste time for that...

    • @shashankjoshi8250
      @shashankjoshi8250 Před 7 měsíci

      Nah. it will work for example 2. I tried it.. greedy passes 74 tc.

  • @ujjwalmaheshwari2327
    @ujjwalmaheshwari2327 Před 7 měsíci

    class Solution {
    mapdp;
    int solve(string &s, int k, int i, char prev, int prevCnt){
    if(k

    • @v-free
      @v-free Před 7 měsíci

      It may be because you are using map, which is ordered and slow. Use unordered_map instead. You need to define a hash function since you are using a vector as key.
      struct vector_hash {
      size_t operator()(const vector& v) const {
      hash hasher;
      size_t seed = 0;
      for (int i : v) {
      seed ^= hasher(i) + 0x9e3779b9 + (seed2);
      }
      return seed;
      }
      };
      unordered_map dp;

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

      @@v-free hey can u tell how i can learn to make custom comparator and custom hashfunction i have tried it from some of the internet tutorials (mostly written ones) please lmk know if u have some resource or tips for me to help me learn those
      thank u

  • @SantoshKumar-bu2qr
    @SantoshKumar-bu2qr Před 5 měsíci

    Why does he sounds like Bane (Batman) giving savage dialouges?

  • @shubhamraj25
    @shubhamraj25 Před 7 měsíci +3

    The same solution in Python beat 79% of users for me

  • @mindrust203
    @mindrust203 Před 7 měsíci +2

    Couldn't we just get the encoded string first and try to shrink it with a recursive function? Or will the addition of computing the encoded string give a TLE?
    EDIT: Nevermind this won't work, I thought run-length encoding would transform a string like "aabbaa" into "a4b2", but the encoding is actually "a2b2a2".
    If k = 2 in this case, the minimum length string would be "a4" with length 2. Indeed a hard problem.

    • @0ManPiano0
      @0ManPiano0 Před 7 měsíci

      Lol I was so happy in the beginning thought “oh! That’s not HRAD!”
      Codes up this exact solution in like 10 min then also ran into this test case.
      Banged my head for the rest 30 min then gave up, lol.
      But after watching this solution does make me believe the condensed list will still work if we keep track of the previous character, which I did not implement this resulting in the failed case you gave.

  • @anonymousperson4466
    @anonymousperson4466 Před 7 měsíci

    why is it so hard.....goddamn

    • @0ManPiano0
      @0ManPiano0 Před 7 měsíci

      This one is hard, isn’t it.

    • @anonymousperson4466
      @anonymousperson4466 Před 7 měsíci

      @@0ManPiano0 I'm having a hard problem everyday...at least one.....this one's painfully hard for me

  • @shrn
    @shrn Před 7 měsíci

    Same solution
    Runtime 1552 ms
    Beats 91.00% of users with Python3
    Memory 39.88 MB
    Beats 40.00% of users with Python3
    No !dea why leetcode does this sometimes

  • @stat_life
    @stat_life Před 7 měsíci

    This solution had beaten 100% of users in runtime and memory, LEETCODE is broken

  • @CheeseStickzZ
    @CheeseStickzZ Před 7 měsíci

    I mean this is cute and everything, but what the hell is the use of solving these. They have no practical application whatsoever 😂 AKA utter waste of time