BASIC CALCULATOR III | LEETCODE # 772 | PYTHON SOLUTION

แชร์
ฝัง

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

  • @landocodes
    @landocodes 9 หลายเดือนก่อน +1

    My Meta phone screen is tomorrow and I think I found a video for every Meta question I ran into (except for maybe 1 that I can't remember).. Just wanted to say thanks again for the hard work man. I'll be recommending this channel to everyone I see prepping for Meta and hopefully I'll pass to the onsite and get more utility out of it.

    • @crackfaang
      @crackfaang  9 หลายเดือนก่อน +1

      Thanks for the kind words and best of luck with the phone screen. I'm sure you'll smash it!

    • @landocodes
      @landocodes 9 หลายเดือนก่อน

      Thanks man I passed!@@crackfaang Now it's time to check out your System Design playlist :)

    • @aman4434
      @aman4434 9 หลายเดือนก่อน

      how was it? Mine was supposed to be tomorrow and its now gotten rescheduled lol. I'm watching this channel too! But still nervous...

    • @landocodes
      @landocodes 9 หลายเดือนก่อน

      @@aman4434it went well! I heard that I passed the next day.
      If you’ve done the top meta questions, and get a good interviewer, then it should be a breeze! Just make sure to communicate your ideas and have a good understanding of the solution you’ve chosen.

    • @landocodes
      @landocodes 9 หลายเดือนก่อน

      It went well! I was a bit nervous for a second but my interviewer was amazing. She calmed me down and told me to just let my thoughts out. Once I did that I got back on track and everything went smoothly! Heard back the next morning that I passed!
      As long as you have the majority of the medium/easy top 100 meta questions complete then I'm sure you'll do fine! Good luck :)@@aman4434

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

    Great solution! It's the most clean one I've seen. Implementing DFS using stack is definitely a pain lol

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

    Good job on your videos. Could you please do this one next? 1267. Count Servers that Communicate. I would really appreciate to hear your explanations

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

      Thanks for the kind words and your support. I’ll add that question to my work queue and make sure you subscribe so you don’t miss the upload

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

    in line 34, if op=s[i], will it become ")" ???

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

      yes but it's fine because if the string still has more after ")", the following MUST be a new OP, like 2*(...)/3 from the examples. And when we process the "/" after, which will first call helper with op being ")", which will do nothing. Then business as usual, num back to 0 and op set to "/"

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

    great explanation

  • @kn7419533
    @kn7419533 หลายเดือนก่อน

    Another solution to reuse Cracking FAANG's code for 227. Basic Calculator II without using stack, we can do
    class Solution {
    public:
    int calculate(string s) {
    int i = 0;
    return calculateHelper(s, i);
    }
    int calculateHelper(string& s, int& i) {
    int cur = 0, prev = 0, res = 0;
    char cur_operation = '+';
    while (i < s.length()) {
    char cur_char = s[i];
    if (isdigit(cur_char)) {
    cur = 0;
    // Extract the full number
    while (i < s.length() && isdigit(s[i])) {
    cur = cur * 10 + (s[i] - '0'); // Convert character to integer
    i++;
    }
    i--; // Adjust index to process the next character properly
    // Perform operation based on the previous operator
    if (cur_operation == '+') {
    res += cur;
    prev = cur;
    } else if (cur_operation == '-') {
    res -= cur;
    prev = -cur;
    } else if (cur_operation == '*') {
    res -= prev;
    prev = prev * cur;
    res += prev;
    } else if (cur_operation == '/') {
    res -= prev;
    prev = prev / cur;
    res += prev;
    }
    }
    else if (cur_char == '(') {
    i++; // Move past '('
    cur = calculateHelper(s, i); // Recursively evaluate parentheses***
    // Perform the operation (same as the previous block)
    if (cur_operation == '+') {
    res += cur;
    prev = cur;
    } else if (cur_operation == '-') {
    res -= cur;
    prev = -cur;
    } else if (cur_operation == '*') {
    res -= prev;
    prev = prev * cur;
    res += prev;
    } else if (cur_operation == '/') {
    res -= prev;
    prev = prev / cur;
    res += prev;
    }
    }
    else if (cur_char == ')') {
    // Return result if we encounter a closing parenthesis
    return res;
    }
    else if (cur_char != ' ') {
    // Store the operator for the next calculation
    cur_operation = cur_char;
    }
    // else --> ' ' (skip it)
    i++;
    }
    return res;
    }