-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2011-Final-Value-of-Variable-After-Performing-Operations.cpp
More file actions
65 lines (63 loc) · 1.67 KB
/
2011-Final-Value-of-Variable-After-Performing-Operations.cpp
File metadata and controls
65 lines (63 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Author : Vicen-te
// Date : 10/21/2025 (MM-DD-YYYY)
/**
*
* Description:
* You are given an array of strings operations, each representing an operation on a variable X.
* The operations can be:
* "++X" or "X++" → increment X by 1
* "--X" or "X--" → decrement X by 1
* Initially, X = 0.
* Return the final value of X after performing all operations.
*
* Example 1:
* Input: operations = ["--X","X++","X++"]
* Process:
* Step 1: X = 0
* Step 2: "--X" → X = -1
* Step 3: "X++" → X = 0
* Step 4: "X++" → X = 1
* Output: 1
*
* Example 2:
* Input: operations = ["++X","++X","X++"]
* Output: 3
*
* Example 3:
* Input: operations = ["X++","++X","--X","X--"]
* Output: 0
*
* Constraints:
* 1 <= operations.length <= 100
* operations[i] is "++X", "X++", "--X", or "X--"
*
*
* Algorithm:
* - Iterate through each operation string.
* - Check the second character (op[1]):
* '+' → increment accumulator by 1
* '-' → decrement accumulator by 1
* - Return the final accumulated value.
*
* Complexity:
* Time: O(N) (traverse operations once)
* Space: O(1) (only an integer accumulator)
*
*/
class Solution
{
public:
int finalValueAfterOperations(const std::vector<std::string>& operations)
{
return std::accumulate
(
operations.begin(),
operations.end(),
0,
[](int acc, const std::string& op)
{
return acc + (op[1] == '+' ? 1 : -1);
}
);
}
};