From 70984609e9df26d492878fd90babf9bd85417b1f Mon Sep 17 00:00:00 2001 From: "Gumparthy Pavan Kumar[pk]" Date: Sun, 27 Jul 2025 10:49:03 +0530 Subject: [PATCH 1/2] fix: operator precedence for leetcode 227 [basic calculator ii] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. What is now different from before? > The implementation now reverses the numbers and operators stacks before applying + and - operators.This ensures that + and - are evaluated in left-to-right order, which matches the expected behavior of the expression (i.e., left-associative evaluation). Previously, operators were applied in reverse order, leading to incorrect results like 1-1+1 → -1 instead of 1. 2. What is the reason for the change? > + and - are left-associative operators with the same precedence, and must be processed in the order they appear. 3. Anything to watch out for? > Space complexity increases slightly due to the temporary numRev and opRev stacks, but remains O(n). --- c++/basic-calculator-ii.md | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/c++/basic-calculator-ii.md b/c++/basic-calculator-ii.md index 0c76c1d..e6b978f 100644 --- a/c++/basic-calculator-ii.md +++ b/c++/basic-calculator-ii.md @@ -60,18 +60,31 @@ int calculate(std::string s) { } } - // Now process remaining operators for addition or subtraction - int result = numbers.top(); - numbers.pop(); - while (!numbers.empty()) { - char op = operators.top(); - operators.pop(); - int num = numbers.top(); + // Reverse the stacks to maintain left-to-right order + std::stack numbersRev; + std::stack operatorsRev; + while(!numbers.empty()) { + numbersRev.push(numbers.top()); numbers.pop(); + } + + while(!operators.empty()) { + operatorsRev.push(operators.top()); + operators.pop(); + } + + // Now process remaining operators for addition or subtraction + int result = numbersRev.top(); + numbersRev.pop(); + while (!numbersRev.empty()) { + char op = operatorsRev.top(); + operatorsRev.pop(); + int num = numbersRev.top(); + numbersRev.pop(); if (op == '+') { result += num; } else if (op == '-') { - result = num - result; + result -= num; } } @@ -87,7 +100,7 @@ int main() { #### Complexity: - **Time Complexity:** O(n), where n is the length of the string, since we are iterating once through the input. -- **Space Complexity:** O(n), due to the supplementary space used by two stacks. +- **Space Complexity:** O(n), due to the supplementary space used by four stacks. --- From fbc94f1f714eecc40b50993c93a234c43bc2a858 Mon Sep 17 00:00:00 2001 From: "Gumparthy Pavan Kumar[pk]" Date: Tue, 23 Dec 2025 19:21:47 +0530 Subject: [PATCH 2/2] refactor: operator precedence for leetcode 227 [basic calculator ii] 1. What is now different than before? A. The implementation now tracks the precedence of - symbols by checking during execution of operation type 2. What is the reason for the change?+ and - are left-associative operators with the same precedence, and must be processed in the order they appear. 3. Anything to watch out for? > Space complexity increases slightly due to the temporary numRev and opRev stacks, but remains O(n). --- c++/basic-calculator-ii.md | 53 +++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/c++/basic-calculator-ii.md b/c++/basic-calculator-ii.md index e6b978f..d8fc5a0 100644 --- a/c++/basic-calculator-ii.md +++ b/c++/basic-calculator-ii.md @@ -31,25 +31,30 @@ int calculate(std::string s) { std::stack operators; int n = s.length(); int currentNumber = 0; - + for (int i = 0; i < n; i++) { char c = s[i]; - // If the current character is a digit, form the whole number if (isdigit(c)) { currentNumber = currentNumber * 10 + (c - '0'); } - - // If the current character is an operator, or it's the last character + + // If the current character is an operator, or it's the last + // character if (!isdigit(c) && !isspace(c) || i == n - 1) { if (!operators.empty()) { - // If the previous operator was '*' or '/', perform that operation char prevOp = operators.top(); if (prevOp == '*' || prevOp == '/') { operators.pop(); int prevNumber = numbers.top(); numbers.pop(); - currentNumber = (prevOp == '*') ? prevNumber * currentNumber : prevNumber / currentNumber; + currentNumber = (prevOp == '*') + ? prevNumber * currentNumber + : prevNumber / currentNumber; + } else if (prevOp == '-') { + operators.pop(); + operators.push('+'); + currentNumber = -currentNumber; } } numbers.push(currentNumber); @@ -59,35 +64,25 @@ int calculate(std::string s) { } } } - - // Reverse the stacks to maintain left-to-right order - std::stack numbersRev; - std::stack operatorsRev; - while(!numbers.empty()) { - numbersRev.push(numbers.top()); + + if (operators.size() == 0) + return numbers.top(); + int result = 0; + if (currentNumber != 0) + result = currentNumber; + else { + result = numbers.top(); numbers.pop(); } - while(!operators.empty()) { - operatorsRev.push(operators.top()); + while (!numbers.empty()) { + int first = numbers.top(); + numbers.pop(); + char op = operators.top(); operators.pop(); + result = (result + first); } - // Now process remaining operators for addition or subtraction - int result = numbersRev.top(); - numbersRev.pop(); - while (!numbersRev.empty()) { - char op = operatorsRev.top(); - operatorsRev.pop(); - int num = numbersRev.top(); - numbersRev.pop(); - if (op == '+') { - result += num; - } else if (op == '-') { - result -= num; - } - } - return result; }