Leetcode Asked a Dirty Question!! - Maximum 69 Number - Leetcode 1323

Поделиться
HTML-код
  • Опубликовано: 13 сен 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Комментарии • 48

  • @GregHogg
    @GregHogg  6 месяцев назад +11

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

    • @repeatbot
      @repeatbot 5 месяцев назад +2

      We can rewrite any integer as a sum of products by powers of 10:
      996969 = 9*10⁵ + 9*10⁴ + 6*10³ + 9*10² + 6*10¹ + 9*10⁰.
      As 9>6, we can write 999999 (9*10⁵+...+9*10⁰). The difference between the digit and the "first" 6 being non zero will tell us which to change:
      999999 - 996969 = 3030 which means that the 3rd (6-4 +1) digit must be changed.
      Thefore the code can be written to be O(1) instead of O(n):
      def maximum69Number (self, num: int) -> int
      s = str(num)
      max99 = int('9'*length(s)) #maximal int of the same length
      if max99 - num == 0: #then it's the max
      return num
      else:
      diff_length = length(str(max99 - num))
      i = length(s) - diff_length
      return int(s[:i] + '9' + s[i+1:])

  • @greensalad_1205
    @greensalad_1205 6 месяцев назад +24

    Missed opportunity to say "nice" at the end

  • @joshnathanielarcilla6127
    @joshnathanielarcilla6127 6 месяцев назад +11

    This one actually looks useful. Just replace the 69 with 1 and 0s

  • @vafasadrif12
    @vafasadrif12 6 месяцев назад +16

    I thought the task was to find the maximum number of '69's in the string

  • @CoolExcite
    @CoolExcite 6 месяцев назад +1

    one of my biggest problems when I started practicing questions like this is that I'd always try to solve them using exponents and logarithms instead of the simple string manipulation solution

  • @airlag
    @airlag 6 месяцев назад +5

    you should resize your screenshots so we can at least read the whole question

  • @chobes1827
    @chobes1827 6 месяцев назад +13

    It'd be more memory efficient to mod by increasing powers of 10 to get the digits instead of converting to a string.

    • @anonanon195
      @anonanon195 6 месяцев назад +8

      I've seen a few videos on this chanel, the author is far away from writing good performance code.

    • @marwan7614
      @marwan7614 6 месяцев назад

      It would be, Probably because this is already happening under the hood in the string conversion, Better than having to allocate more memory for the string then turning digits into their ASCII repesentation and storing them. All that can be avoided.

    • @usemayonaise
      @usemayonaise 6 месяцев назад +2

      Yeah this guy always shows up on my feed and I think he'd be lucky to pass an entry level coding interview

    • @nekomancer4821
      @nekomancer4821 5 месяцев назад

      ​@@darkopzthere is a difference between "pre mature optimization" and "doing it not stupidly"
      and holy shit it feels weird to watch these videos and know more than them now, when I grew up it was the other way around

  • @Hishambakrawala
    @Hishambakrawala 6 месяцев назад +14

    I want to reach the level of understanding and question solving ability like yours. I am right now learning C as my first language

    • @temirbek2004
      @temirbek2004 6 месяцев назад

      Go for c++

    • @TargunYssboern
      @TargunYssboern 6 месяцев назад +7

      it's just "start from the left and change first encountered 6 into 9"

    • @carissalarocque4484
      @carissalarocque4484 6 месяцев назад

      Dayummm, c as first language? I started on html and JavaScript. But i wish you luck!
      - CS student who's been coding since 15

    • @GregHogg
      @GregHogg  6 месяцев назад +2

      Sounds good, I'm sure you're well on your way! I have probably just solved more problems haha it just takes time and commitment

  • @aprasath1
    @aprasath1 6 месяцев назад +2

    Plain code discussion is good enough 😂

  • @MajdFreiji
    @MajdFreiji 6 месяцев назад +2

    I am getting these a whole lot more recently, but I think it would be nice if the solution came with print statements at each iteration to understand what's going on visually. Just seeing lines of codes and the returned value is really unhelpful.

    • @a.yashwanth
      @a.yashwanth 6 месяцев назад

      Not to discourage but this is just a basic for loop with a if condition in it. If you have problems understanding this then you need to revisit how for loop works.

  • @lucsol2255
    @lucsol2255 6 месяцев назад +1

    Pretty sure this code would not be correct. It does work for a number with only 9's, but will not give the correct result for a number of only sixes.

    • @darknight3613
      @darknight3613 6 месяцев назад

      Given an array with only 6's, the digit in the first index will be replaced with 9, we call the "break", then return the modified number

    • @lucsol2255
      @lucsol2255 6 месяцев назад

      ​@@darknight3613right, but there would be no 9 to replace the 6 with in the original number?

    • @darknight3613
      @darknight3613 6 месяцев назад

      @@lucsol2255 the question is not explained very well but based on the algorithm you can understand that just turning the first 6 to a 9 is valid

  • @code2compass
    @code2compass 6 месяцев назад +1

    What software are you using for illustration

  • @ssh3v
    @ssh3v 6 месяцев назад

    A stack can be used hear to solve , pop when peek is 6 and we are about to push 9 at last we have to check size of stack

  • @marekslazak1003
    @marekslazak1003 6 месяцев назад

    so
    number.toString().replace('6','9').toInt()
    assuming replace only replaces once (like js)

  • @vascoguerreiro341
    @vascoguerreiro341 6 месяцев назад

    Thanks

  • @alrjandrorivasespa9264
    @alrjandrorivasespa9264 6 месяцев назад

    It would be nice to see a transformer inplementation tutorial there are very few on The internet.

  • @rabiddoughnuts
    @rabiddoughnuts 2 месяца назад

    Does python not have indexOf? Just index of 6, if there isnt one, you are done

  • @ChrisLocke1969
    @ChrisLocke1969 6 месяцев назад +1

    Awwww, he lives at home and is telling you all how to pass job exams.... OKAAAAY

    • @GregHogg
      @GregHogg  6 месяцев назад +3

      I'm sorry but who doesn't live at home, would it be more convincing if I lived on the street?

    • @arsheyajain7055
      @arsheyajain7055 6 месяцев назад +1

      The funnier thing is that you believe that voice is a woman’s voice lol.

    • @ChrisLocke1969
      @ChrisLocke1969 6 месяцев назад

      even funnier that three hours later you're still thinking about this... time to leave mom's basement, I think@@arsheyajain7055

    • @Bacinator334
      @Bacinator334 6 месяцев назад

      ​@@GregHogg yup. Way more. Terry Davis my boi. And me.

  • @-aexc-
    @-aexc- 6 месяцев назад

    i hate that im here, but i have an interview in a week

  • @jerinjohnkachirackal
    @jerinjohnkachirackal 6 месяцев назад

    There should be an alternative 🤔💭

  • @Nexultiverse
    @Nexultiverse 6 месяцев назад

    Bro is fine 🥰

  • @TondalOfficial
    @TondalOfficial 5 месяцев назад

    Hi ! What is that website ?
    Thank you in advance 😊

  • @Joshjson
    @Joshjson 6 месяцев назад +1

    Now it seems only right that another with 420 is added

  • @ts47gaming28
    @ts47gaming28 6 месяцев назад +2

    I feel like you should teach more of Algorithms rather than just using some libraries or functions sir. You should be telling about how to_string works etc. Because I am an Intermediate programmer and A lot of my beginner Friends suffer through the sense of logic and computer science because of this

  • @andrewbarr4843
    @andrewbarr4843 6 месяцев назад

    Bro never done 69. Me neither

  • @khoilucas5480
    @khoilucas5480 6 месяцев назад

    I bet this interview question is from only fan or p*rnhub companies 😂😂

  • @jayraval2846
    @jayraval2846 6 месяцев назад +3

    Nice 😏

  • @theFifthMountain123
    @theFifthMountain123 6 месяцев назад +1

    Lol

  • @repeatbot
    @repeatbot 5 месяцев назад +1

    We can rewrite any integer as a sum of products by powers of 10:
    996969 = 9*10⁵ + 9*10⁴ + 6*10³ + 9*10² + 6*10¹ + 9*10⁰.
    As 9>6, we can write 999999 (9*10⁵+...+9*10⁰). The difference between the digit and the "first" 6 being non zero will tell us which to change:
    999999 - 996969 = 3030 which means that the 3rd (6-4 +1) digit must be changed.
    Therore the code can be written to be O(1) instead of O(n):
    def maximum69Number (self, num: int) -> int
    s = str(num)
    max99 = int('9'*length(s)) #maximal int of the same length
    if max99 - num == 0: #then it's the max
    return num
    else:
    diff_length = length(str(max99 - num))
    i = length(s) - diff_length
    return int(s[:i] + '9' + s[i+1:])