Leetcode

Sdílet
Vložit
  • čas přidán 27. 07. 2024

Komentáře • 19

  • @jlecampana
    @jlecampana Před 11 měsíci

    Awesome, for a very long time I struggled to find a better solution than brute force for this problem! This is it!

  • @Aut0KAD
    @Aut0KAD Před 3 lety

    nice video!
    An approach I went with was keeping track of the max value right of any index and the index that max value set at.
    This way we can then loop left to right and if we need to swap, do it.
    temp = list(str(num))
    right_max = temp[len(temp)-1]
    digit_map = {right_max: len(temp)-1}
    num_max = len(temp) *[right_max]
    ## Build array
    for i in range(len(temp)-2,-1,-1):
    if temp[i] > right_max:
    num_max[i], right_max = temp[i], temp[i]
    digit_map[right_max] = i
    if num_max[i] < right_max:
    num_max[i] = right_max
    for i in range(len(temp)-1):
    if temp[i] < num_max[i]:
    index = digit_map[ num_max[i] ]
    temp[i], temp[index] = temp[index], temp[i]
    return int( ''.join(temp))
    return num

  • @seyidaniels1350
    @seyidaniels1350 Před 2 lety +1

    if a digit in intmap is not enough, we have to be sure the digit is greater than the current item and also we need to be sure that the index of the digit is greater than the index of the current item also

  • @lakshmih.m9115
    @lakshmih.m9115 Před 3 lety

    Very nice explanation! Please do more videos

  • @lala-jy4kz
    @lala-jy4kz Před 6 lety

    Patient explanation! Love your video!

  • @vijayalakshmi8337
    @vijayalakshmi8337 Před 4 lety +1

    I did not understand the swapping part. Why do we say numstr[digit]? From the example, would it not be numstr[7] when we check if 7 is in the intMap?

  • @khadijakhaldi6468
    @khadijakhaldi6468 Před 3 lety

    Great explanation!

  • @jasir5848
    @jasir5848 Před 3 lety

    For the input case A=7599 and B=2, it will return 9957 but the answer should be 9975

  • @jingjing6813
    @jingjing6813 Před 5 lety +1

    very clear and detailed explain!!! Thanks! Could you please also talk about time complexity?

    • @algorithmsillustrator3313
      @algorithmsillustrator3313  Před 5 lety

      Thank you for your kind words Jingjing

    • @shen-yusun7683
      @shen-yusun7683 Před 4 lety

      Thank you for the video as well. Also wondering what the time complexity is for the second approach? Are time and space complexity both O(N), where N is number of elements in the num?

  • @amandatao9622
    @amandatao9622 Před 2 lety

    thank you!!!

  • @lilzjay9732
    @lilzjay9732 Před 5 lety

    excellent

  • @seyidaniels1350
    @seyidaniels1350 Před 2 lety

    if a digit in intmap is not enough, we have to be sure the digit is greater than the current item and also we need to be sure that the index of the digit is greater than the index of the current item also