2011. Final Value of Variable After Performing Operations
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Problem Overview
Simple accumulation: gain adds to score, lose subtracts.
Intuition
Simple accumulation: gain adds to score, lose subtracts. Return maximum score at any point.
Algorithm
- 1For each operation: if gain, score += point. If lose, score -= point. Track max score.
Common Pitfalls
- •Score can go negative but we track maximum at any point, not final score.
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;
}
}Was this solution helpful?
Related Problems
- 68. Text Justification(Hard)
- 592. Fraction Addition and Subtraction(Unknown)
- 657. Robot Return to Origin(Unknown)
- 838. Push Dominoes(Medium)
- 1545. Find Kth Bit in Nth Binary String(Easy)
- 1598. Crawler Log Folder(Easy)