Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created October 20, 2025 22:07
Show Gist options
  • Select an option

  • Save Ifihan/e2e94806da888e87798101d1c6396c53 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/e2e94806da888e87798101d1c6396c53 to your computer and use it in GitHub Desktop.
Final Value of Variable After Performing Operations

Question

Approach

I start with X = 0. Each operation either increments (++X or X++) or decrements (--X or X--) the value of X by 1. So, I just iterate through the operations, adding 1 for "++" and subtracting 1 for "--".

Implementation

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

Complexities

  • Time: O(n)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment