2726. Calculator with Method Chaining LeetCode Solution

In this guide, you will get 2726. Calculator with Method Chaining LeetCode Solution with the best time and space complexity. The solution to Calculator with Method Chaining problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Calculator with Method Chaining solution in C++
  4. Calculator with Method Chaining solution in Java
  5. Calculator with Method Chaining solution in Python
  6. Additional Resources
2726. Calculator with Method Chaining LeetCode Solution image

Problem Statement of Calculator with Method Chaining

Design a Calculator class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining. The Calculator class constructor should accept a number which serves as the initial value of result.
Your Calculator class should have the following methods:

add – This method adds the given number value to the result and returns the updated Calculator.
subtract – This method subtracts the given number value from the result and returns the updated Calculator.
multiply – This method multiplies the result by the given number value and returns the updated Calculator.
divide – This method divides the result by the given number value and returns the updated Calculator. If the passed value is 0, an error “Division by zero is not allowed” should be thrown.
power – This method raises the result to the power of the given number value and returns the updated Calculator.
getResult – This method returns the result.

See also  2079. Watering Plants LeetCode Solution

Solutions within 10-5 of the actual result are considered correct.

Example 1:

Input:
actions = [“Calculator”, “add”, “subtract”, “getResult”],
values = [10, 5, 7]
Output: 8
Explanation:
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 – 7 = 8

Example 2:

Input:
actions = [“Calculator”, “multiply”, “power”, “getResult”],
values = [2, 5, 2]
Output: 100
Explanation:
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100

Example 3:

Input:
actions = [“Calculator”, “divide”, “getResult”],
values = [20, 0]
Output: “Division by zero is not allowed”
Explanation:
new Calculator(20).divide(0).getResult() // 20 / 0

The error should be thrown because we cannot divide by zero.

Constraints:

actions is a valid JSON array of strings
values is a valid JSON array of numbers
2 <= actions.length <= 2 * 104
1 <= values.length <= 2 * 104 – 1
actions[i] is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"
First action is always "Calculator"
Last action is always "getResult"

Complexity Analysis

  • Time Complexity: Google AdSense
  • Space Complexity: Google Analytics

2726. Calculator with Method Chaining LeetCode Solution in C++

class Calculator {
  private cur: number;

  constructor(value: number) {
    this.cur = value;
  }

  add(value: number): Calculator {
    this.cur += value;
    return this;
  }

  subtract(value: number): Calculator {
    this.cur -= value;
    return this;
  }

  multiply(value: number): Calculator {
    this.cur *= value;
    return this;
  }

  divide(value: number): Calculator {
    if (value === 0) throw new Error('Division by zero is not allowed');
    this.cur /= value;
    return this;
  }

  power(value: number): Calculator {
    this.cur **= value;
    return this;
  }

  getResult(): number {
    return this.cur;
  }
}
/* code provided by PROGIEZ */

2726. Calculator with Method Chaining LeetCode Solution in Java

N/A
// code provided by PROGIEZ

2726. Calculator with Method Chaining LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  3427. Sum of Variable Length Subarrays LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.