Hardest Coding Problem Explained (LeetCode)

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ธ.ค. 2024

ความคิดเห็น • 42

  • @homieinthesky8919
    @homieinthesky8919 2 ปีที่แล้ว +1

    You explain this so well that a beginner can inplement this into his/her starter language

  • @maxwellmaragia2272
    @maxwellmaragia2272 2 ปีที่แล้ว +2

    Difficult problem easily explained, great job and kudos for this.

  • @sampleshawn5380
    @sampleshawn5380 3 ปีที่แล้ว +2

    I would definitely say that you have understood the problem thoroughly and thanks for letting us know the way you understood. Thanks mate.

  • @oli6839
    @oli6839 4 ปีที่แล้ว +6

    you explain really well, good job! Thanks for the vidéo :)

  • @theghostwhowalk
    @theghostwhowalk 4 ปีที่แล้ว +3

    Another hard problem made easy!
    great explanation!

  • @mamyname
    @mamyname 2 ปีที่แล้ว +1

    I went and cheeked the same issue in leet code, and was able to fix it without a for-loop
    just validate if the string was null or ends with e, or + or .
    then try to cast it to decimal, if casting failed, exit with false (it worked)
    if (s == "Infinity" || s == "-Infinity" || s == "+Infinity") return false;
    ... more validations
    try:
    double num = double.Parse(s);
    catch:
    return false;

  • @sanjayizardar2263
    @sanjayizardar2263 4 ปีที่แล้ว +4

    Awesome explanation.. keep up the good work.

  • @harelavv8806
    @harelavv8806 3 ปีที่แล้ว +5

    You can solve it with a regex and e counter (I'm new to CS and from mobile so it's pretty bad):
    Bool isValidDecimal(string str) {
    Regex reg = /[-+]?\d+(\.\d+?(e -? \d+))?/;
    Return reg.test(str) ;
    }
    [-+]? A sign in the start and its optional
    \d+ must be at least one digit before the decimal point
    The parentheses and question sign mean that all the part of decimal point is option
    \. We're escaping the dot because Normally dot in regex means every character and now it means just dot
    \d+? There might be numbers before the exponent
    (e -? \d+)? There might be not exponent and if there is there must be just one and after the decimal point and afterwards might be a minus sign and there must be at least one digit
    I really didn't think about it that much and there might be some cases I missed and rook me forever ro type cuz of mobile so correct me if I'm wrong, I'm also only 5 months into coding so yeah

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +3

      Regex is a great way to solve it, nice!

    • @DanielVazquez
      @DanielVazquez 3 ปีที่แล้ว +1

      Regex would be cheating. Is like the interviewer asking you to implement any sorting algorithm and you just use a library.

  • @gioasencio4946
    @gioasencio4946 4 ปีที่แล้ว +4

    why not just trim then regex?

  • @nishadtardalkar8737
    @nishadtardalkar8737 3 ปีที่แล้ว +3

    Accepted.
    class Solution:
    def isNumber(self, s: str) -> bool:
    s = s.lower()
    try:
    if 'inf' in s:
    return False
    x = float(s)
    return True
    except:
    return False
    Runtime: 32 ms, faster than 79.16% of Python3 online submissions for Valid Number.
    Memory Usage: 14.1 MB, less than 84.88% of Python3 online submissions for Valid Number.

  • @pshivaramakrishna
    @pshivaramakrishna 4 ปีที่แล้ว +2

    Hi Guys! I may not be professional in writing code. But I solved it within 2 minutes. I used Python's Exception Handling concept here, by float(input string) if it gives value error just return false. Trust me It really worked 😂😂😂

  • @makubexmeg
    @makubexmeg 2 ปีที่แล้ว

    is it better if you use regex?

  • @shmuelmoskowicz328
    @shmuelmoskowicz328 4 ปีที่แล้ว +5

    this problem is not so hard, it is just very poorly written and you don't know what is considered valid and what not until you have attempted it many times.
    I had to attempt it nine times, and each time I found out a new edge case what is considered valid or not valid, until the tenth time I finally got it.

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว +4

      Yep, exactly. That's why the problem has so many downvotes haha

  • @FitnessChaos
    @FitnessChaos 3 ปีที่แล้ว

    Really good breakdown

  • @MyVip3r
    @MyVip3r ปีที่แล้ว

    there is already a function that checks this problem but here an implementation on C
    #include
    #include
    #include
    bool is_decimal_number(const char *str) {
    if (str == NULL) {
    return false;
    }
    // Check if the string is empty
    if (*str == '\0') {
    return false;
    }
    // Check if the string starts with a negative sign
    if (*str == '-') {
    str++;
    }
    // Check if the string contains only digits
    while (*str != '\0') {
    if (!isdigit(*str)) {
    return false;
    }
    str++;
    }
    return true;
    }
    int main(void) {
    const char *str1 = "123";
    const char *str2 = "-123";
    const char *str3 = "123.45";
    const char *str4 = "abc";
    printf("%d
    ", is_decimal_number(str1)); // prints 1
    printf("%d
    ", is_decimal_number(str2)); // prints 1
    printf("%d
    ", is_decimal_number(str3)); // prints 0
    printf("%d
    ", is_decimal_number(str4)); // prints 0
    return 0;
    }

  • @MuhammadWaseem-mj3ge
    @MuhammadWaseem-mj3ge 4 ปีที่แล้ว +1

    Which website was that?

  • @paoloranit8834
    @paoloranit8834 3 ปีที่แล้ว

    why not just convert it to parseInt or parseDouble and handle exceptions to check instead?

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว

      You can do that, but I imagine in an interview they might restrict you to not be able to use those functions.

  • @okiseeyou
    @okiseeyou 3 ปีที่แล้ว +1

    (constant space complexity)
    “Memory Usage: less than 6.25% of submissions”
    da fuq everybody doin over there

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +3

      lol right, sometimes I feel like people are hard coding the test cases haha

  • @DreaMagnifier
    @DreaMagnifier 2 ปีที่แล้ว

    regular expression is good way

  • @satoshinakamoto7593
    @satoshinakamoto7593 3 ปีที่แล้ว

    If anyone wants to study together or make a new friend, feel free to dm me! It would be awesome to go over any programming concepts or Leetcode together!

  • @yukkiamatsu8297
    @yukkiamatsu8297 3 ปีที่แล้ว

    Nice explain, but i still not understand 😅 iwant to learn coding but, i hope i can 😅

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +1

      It takes alot of time, just make sure to be consistent!

  • @DanielVazquez
    @DanielVazquez 3 ปีที่แล้ว

    Regardless of difficulty let us agree it is just a bad coding question.