Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "fix smells"
This reverts commit 2f48efa.
  • Loading branch information
ThanhNIT committed May 23, 2022
commit 624f222b755d56ef229a84fff13a5f3ecd246483
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public int minOperations(int[] nums) {
int n = nums.length;
int duplicates = 0;
int maxContinuous = 0;
int start = 0;
int end = 0;
while (end < n) {
for (int start = 0, end = 0; end < n; end++) {
if (end > 0 && nums[end] == nums[end - 1]) {
duplicates++;
}
Expand All @@ -23,7 +21,6 @@ public int minOperations(int[] nums) {
}
}
maxContinuous = Math.max(maxContinuous, end - start + 1 - duplicates);
end++;
}
return n - maxContinuous;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g2001_2100.s2011_final_value_of_variable_after_performing_operations;

// #Easy #Array #String #Simulation #2022_05_23_Time_1_ms_(94.40%)_Space_43.6_MB_(26.15%)

public class Solution {
public int finalValueAfterOperations(String[] operations) {
int xValue = 0;
for (String word : operations) {
if (word.contains("+")) {
xValue++;
} else {
xValue--;
}
}
return xValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
2011\. Final Value of Variable After Performing Operations

Easy

There is a programming language with only **four** operations and **one** variable `X`:

* `++X` and `X++` **increments** the value of the variable `X` by `1`.
* `--X` and `X--` **decrements** the value of the variable `X` by `1`.

Initially, the value of `X` is `0`.

Given an array of strings `operations` containing a list of operations, return _the **final** value of_ `X` _after performing all the operations_.

**Example 1:**

**Input:** operations = ["--X","X++","X++"]

**Output:** 1

**Explanation:** The operations are performed as follows:

Initially, X = 0.

--X: X is decremented by 1, X = 0 - 1 = -1.

X++: X is incremented by 1, X = -1 + 1 = 0.

X++: X is incremented by 1, X = 0 + 1 = 1.

**Example 2:**

**Input:** operations = ["++X","++X","X++"]

**Output:** 3

**Explanation:** The operations are performed as follows:

Initially, X = 0.

++X: X is incremented by 1, X = 0 + 1 = 1.

++X: X is incremented by 1, X = 1 + 1 = 2.

X++: X is incremented by 1, X = 2 + 1 = 3.

**Example 3:**

**Input:** operations = ["X++","++X","--X","X--"]

**Output:** 0

**Explanation:** The operations are performed as follows:

Initially, X = 0.

X++: X is incremented by 1, X = 0 + 1 = 1.

++X: X is incremented by 1, X = 1 + 1 = 2.

--X: X is decremented by 1, X = 2 - 1 = 1.

X--: X is decremented by 1, X = 1 - 1 = 0.

**Constraints:**

* `1 <= operations.length <= 100`
* `operations[i]` will be either `"++X"`, `"X++"`, `"--X"`, or `"X--"`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g2001_2100.s2011_final_value_of_variable_after_performing_operations;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void finalValueAfterOperations() {
assertThat(
new Solution().finalValueAfterOperations(new String[] {"--X", "X++", "X++"}),
equalTo(1));
}

@Test
void finalValueAfterOperations2() {
assertThat(
new Solution().finalValueAfterOperations(new String[] {"++X", "++X", "X++"}),
equalTo(3));
}

@Test
void finalValueAfterOperations3() {
assertThat(
new Solution().finalValueAfterOperations(new String[] {"X++", "++X", "--X", "X--"}),
equalTo(0));
}
}