DDSA
Advertisement

2011. Final Value of Variable After Performing Operations

Time: O(n)
Space: O(1)

Approach

Count '+' increments and '-' decrements; return difference.

2011.cs
C#
// Approach: Count '+' increments and '-' decrements; return difference.
// Time: O(n) Space: O(1)

public class Solution
{
    public int FinalValueAfterOperations(string[] operations)
    {
        int result = 0;
        foreach (string operation in operations)
        {
            if (operation[1] == '+')
                result += 1;
            else
                result -= 1;
        }
        return result;
    }
}
Advertisement
Was this solution helpful?