diff --git a/src/FinalValueOfVariableAfterPerformingOperations2011.java b/src/FinalValueOfVariableAfterPerformingOperations2011.java deleted file mode 100644 index 9415ab8..0000000 --- a/src/FinalValueOfVariableAfterPerformingOperations2011.java +++ /dev/null @@ -1,13 +0,0 @@ -public class FinalValueOfVariableAfterPerformingOperations2011 { - public int finalValueAfterOperations(String[] operations) { - int res = 0; - for (String operation : operations) { - if (operation.equals("++X") || operation.equals("X++")) { - res++; - } else if (operation.equals("--X") || operation.equals("X--")) { - res--; - } - } - return res; - } -} diff --git a/src/main/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011.java b/src/main/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011.java new file mode 100644 index 0000000..28bab63 --- /dev/null +++ b/src/main/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011.java @@ -0,0 +1,23 @@ +// Tags: Array, String, Simulation +package com.leetcode.easy; + +/** + * Time Complexity: O(n) where n is the length of the operations array. + * We iterate through each operation exactly once. + *
+ * Space Complexity: O(1) as we only use a constant amount of extra space + * for the result variable, regardless of input size. + */ +public class FinalValueOfVariableAfterPerformingOperations2011 { + public int finalValueAfterOperations(String[] operations) { + int res = 0; + for (String operation : operations) { + if (operation.charAt(1) == '+') { + res++; + } else { + res--; + } + } + return res; + } +} diff --git a/src/test/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011Test.java b/src/test/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011Test.java new file mode 100644 index 0000000..0b43798 --- /dev/null +++ b/src/test/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011Test.java @@ -0,0 +1,39 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FinalValueOfVariableAfterPerformingOperations2011Test { + private FinalValueOfVariableAfterPerformingOperations2011 solution; + + @BeforeEach + void setUp() { + solution = new FinalValueOfVariableAfterPerformingOperations2011(); + } + + @Test + void testFinalValueAfterOperations_Example1() { + String[] operations = {"--X", "X++", "X++"}; + int expected = 1; + int result = solution.finalValueAfterOperations(operations); + assertEquals(expected, result); + } + + @Test + void testFinalValueAfterOperations_Example2() { + String[] operations = {"++X", "++X", "X++"}; + int expected = 3; + int result = solution.finalValueAfterOperations(operations); + assertEquals(expected, result); + } + + @Test + void testFinalValueAfterOperations_Example3() { + String[] operations = {"X++", "++X", "--X", "X--"}; + int expected = 0; + int result = solution.finalValueAfterOperations(operations); + assertEquals(expected, result); + } +}