Skip to content

Latest commit

 

History

History
95 lines (70 loc) · 3.3 KB

019.md

File metadata and controls

95 lines (70 loc) · 3.3 KB

Difficulty: 🟢 Easy

There is a programming language with only four operations and one variable X:

  • ++X and X++ increments the value of the variable X by 1.
  • --X and X-- decrements the value of the variable X by 1.

Initially, the value of X is 0.

Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.

Examples:

Example 1:

Input: operations = ["--X","X++","X++"]
Output: 1
Explanation: The operations are performed as follows:
Initially, X = 0.
--X: X is decremented by 1, X =  0 - 1 = -1.
X++: X is incremented by 1, X = -1 + 1 =  0.
X++: X is incremented by 1, X =  0 + 1 =  1.

Example 2:

Input: operations = ["++X","++X","X++"]
Output: 3
Explanation: The operations are performed as follows:
Initially, X = 0.
++X: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
X++: X is incremented by 1, X = 2 + 1 = 3.

Example 3:

Input: operations = ["X++","++X","--X","X--"]
Output: 0
Explanation: The operations are performed as follows:
Initially, X = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
--X: X is decremented by 1, X = 2 - 1 = 1.
X--: X is decremented by 1, X = 1 - 1 = 0.

Constraints:

  • 1 <= operations.length <= 100
  • operations[i] will be either "++X", "X++", "--X", or "X--".

Solutions

O(n) solution in Python3

class Solution:
    def finalValueAfterOperations(self, operations: List[str]) -> int:
        total = 0
        for operation in operations:
            if operation == "--X" or operation == "X--":
                total -= 1
            else:
                total += 1
        return total

The given solution solves the problem by performing the following steps:

  1. Initialize a variable total to store the current value of X.
  2. Iterate through each operation in the operations array.
  3. For each operation, check its value:
    • If the operation is "--X" or "X--", decrement the value of total by 1.
    • Otherwise, increment the value of total by 1.
  4. After performing all the operations, the final value of X is stored in the total variable.
  5. Return the value of total as the final answer.

Complexity Analysis

The time complexity of this algorithm is O(n), where n is the length of the operations array. This is because the algorithm performs a single pass through the array to evaluate each operation.

The space complexity of the algorithm is O(1) since it only uses a constant amount of memory to store the total variable. The memory usage does not depend on the input size.

Summary

The given solution effectively evaluates the final value of X after performing a series of operations. It iterates through the operations array, incrementing or decrementing the total variable based on the operation type. The algorithm has a time complexity of O(n) and a space complexity of O(1), making it an efficient solution for the problem.

NB: If you want to get community points please suggest solutions in other languages as merge requests.