diff --git a/src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql b/src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql index 7d845c450..17b7930c5 100644 --- a/src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql +++ b/src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql @@ -1,6 +1,6 @@ # Write your MySQL query statement below -# #Medium #2024_07_18_Time_272_ms_(100.00%)_Space_0B_(100.00%) +# #Medium #Database #2024_07_23_Time_248_ms_(85.85%)_Space_0B_(100.00%) select transaction_date, sum(case when amount%2<>0 then amount else 0 end) as odd_sum, sum(case when amount%2=0 then amount else 0 end) as even_sum from transactions -group by transaction_date order by transaction_date; +group by transaction_date order by transaction_date asc; diff --git a/src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/Solution.java b/src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/Solution.java new file mode 100644 index 000000000..64e7d695d --- /dev/null +++ b/src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/Solution.java @@ -0,0 +1,15 @@ +package g3201_3300.s3222_find_the_winning_player_in_coin_game; + +// #Easy #Math #Simulation #Game_Theory #2024_07_23_Time_0_ms_(100.00%)_Space_41.6_MB_(67.81%) + +public class Solution { + public String losingPlayer(int x, int y) { + boolean w = false; + while (x > 0 && y >= 4) { + x--; + y -= 4; + w = !w; + } + return w ? "Alice" : "Bob"; + } +} diff --git a/src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/readme.md b/src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/readme.md new file mode 100644 index 000000000..649d5f0d0 --- /dev/null +++ b/src/main/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/readme.md @@ -0,0 +1,38 @@ +3222\. Find the Winning Player in Coin Game + +Easy + +You are given two **positive** integers `x` and `y`, denoting the number of coins with values 75 and 10 _respectively_. + +Alice and Bob are playing a game. Each turn, starting with **Alice**, the player must pick up coins with a **total** value 115. If the player is unable to do so, they **lose** the game. + +Return the _name_ of the player who wins the game if both players play **optimally**. + +**Example 1:** + +**Input:** x = 2, y = 7 + +**Output:** "Alice" + +**Explanation:** + +The game ends in a single turn: + +* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10. + +**Example 2:** + +**Input:** x = 4, y = 11 + +**Output:** "Bob" + +**Explanation:** + +The game ends in 2 turns: + +* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10. +* Bob picks 1 coin with a value of 75 and 4 coins with a value of 10. + +**Constraints:** + +* `1 <= x, y <= 100` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations/Solution.java b/src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations/Solution.java new file mode 100644 index 000000000..8491f5496 --- /dev/null +++ b/src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations/Solution.java @@ -0,0 +1,26 @@ +package g3201_3300.s3223_minimum_length_of_string_after_operations; + +// #Medium #String #Hash_Table #Counting #2024_07_23_Time_9_ms_(94.23%)_Space_46.5_MB_(38.50%) + +public class Solution { + public int minimumLength(String s) { + int[] freq = new int[26]; + for (int i = 0; i < 26; i++) { + freq[i] = 0; + } + for (int i = 0; i < s.length(); i++) { + freq[s.charAt(i) - 'a']++; + } + int c = 0; + for (int i : freq) { + if (i != 0) { + if (i % 2 == 0) { + c += 2; + } else { + c += 1; + } + } + } + return c; + } +} diff --git a/src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations/readme.md b/src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations/readme.md new file mode 100644 index 000000000..a53d43938 --- /dev/null +++ b/src/main/java/g3201_3300/s3223_minimum_length_of_string_after_operations/readme.md @@ -0,0 +1,39 @@ +3223\. Minimum Length of String After Operations + +Medium + +You are given a string `s`. + +You can perform the following process on `s` **any** number of times: + +* Choose an index `i` in the string such that there is **at least** one character to the left of index `i` that is equal to `s[i]`, and **at least** one character to the right that is also equal to `s[i]`. +* Delete the **closest** character to the **left** of index `i` that is equal to `s[i]`. +* Delete the **closest** character to the **right** of index `i` that is equal to `s[i]`. + +Return the **minimum** length of the final string `s` that you can achieve. + +**Example 1:** + +**Input:** s = "abaacbcbb" + +**Output:** 5 + +**Explanation:** + We do the following operations: + +* Choose index 2, then remove the characters at indices 0 and 3. The resulting string is `s = "bacbcbb"`. +* Choose index 3, then remove the characters at indices 0 and 5. The resulting string is `s = "acbcb"`. + +**Example 2:** + +**Input:** s = "aa" + +**Output:** 2 + +**Explanation:** + We cannot perform any operations, so we return the length of the original string. + +**Constraints:** + +* 1 <= s.length <= 2 * 105 +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/Solution.java b/src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/Solution.java new file mode 100644 index 000000000..c7dea4e34 --- /dev/null +++ b/src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/Solution.java @@ -0,0 +1,32 @@ +package g3201_3300.s3224_minimum_array_changes_to_make_differences_equal; + +// #Medium #Array #Hash_Table #Prefix_Sum #2024_07_23_Time_4_ms_(100.00%)_Space_58.4_MB_(41.64%) + +public class Solution { + public int minChanges(int[] nums, int k) { + int[] cm = new int[k + 2]; + for (int i = 0; i < nums.length / 2; i++) { + int a = Math.min(nums[i], nums[nums.length - 1 - i]); + int b = Math.max(nums[i], nums[nums.length - 1 - i]); + int d = b - a; + if (d > 0) { + cm[0]++; + cm[d]--; + cm[d + 1]++; + int max = Math.max(a, k - b) + d; + cm[max + 1]++; + } else { + cm[1]++; + int max = Math.max(a, k - a); + cm[max + 1]++; + } + } + int sum = cm[0]; + int res = cm[0]; + for (int i = 1; i <= k; i++) { + sum += cm[i]; + res = Math.min(res, sum); + } + return res; + } +} diff --git a/src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/readme.md b/src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/readme.md new file mode 100644 index 000000000..b3b22283e --- /dev/null +++ b/src/main/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/readme.md @@ -0,0 +1,47 @@ +3224\. Minimum Array Changes to Make Differences Equal + +Medium + +You are given an integer array `nums` of size `n` where `n` is **even**, and an integer `k`. + +You can perform some changes on the array, where in one change you can replace **any** element in the array with **any** integer in the range from `0` to `k`. + +You need to perform some changes (possibly none) such that the final array satisfies the following condition: + +* There exists an integer `X` such that `abs(a[i] - a[n - i - 1]) = X` for all `(0 <= i < n)`. + +Return the **minimum** number of changes required to satisfy the above condition. + +**Example 1:** + +**Input:** nums = [1,0,1,2,4,3], k = 4 + +**Output:** 2 + +**Explanation:** + We can perform the following changes: + +* Replace `nums[1]` by 2. The resulting array is nums = [1,**2**,1,2,4,3]. +* Replace `nums[3]` by 3. The resulting array is nums = [1,2,1,**3**,4,3]. + +The integer `X` will be 2. + +**Example 2:** + +**Input:** nums = [0,1,2,3,3,6,5,4], k = 6 + +**Output:** 2 + +**Explanation:** + We can perform the following operations: + +* Replace `nums[3]` by 0. The resulting array is nums = [0,1,2,**0**,3,6,5,4]. +* Replace `nums[4]` by 4. The resulting array is nums = [0,1,2,0,**4**,6,5,4]. + +The integer `X` will be 4. + +**Constraints:** + +* 2 <= n == nums.length <= 105 +* `n` is even. +* 0 <= nums[i] <= k <= 105 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations/Solution.java b/src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations/Solution.java new file mode 100644 index 000000000..f17edff81 --- /dev/null +++ b/src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations/Solution.java @@ -0,0 +1,57 @@ +package g3201_3300.s3225_maximum_score_from_grid_operations; + +// #Hard #Array #Dynamic_Programming #Matrix #Prefix_Sum +// #2024_07_23_Time_21_ms_(100.00%)_Space_45.1_MB_(96.92%) + +public class Solution { + public long maximumScore(int[][] grid) { + final int n = grid.length; + long[] dp1 = new long[n]; + long[] dp2 = new long[n + 1]; + long[] dp3 = new long[n + 1]; + long[] dp12 = new long[n]; + long[] dp22 = new long[n + 1]; + long[] dp32 = new long[n + 1]; + long res = 0; + for (int i = 0; i < n; ++i) { + long sum = 0; + long pre = 0; + for (int[] ints : grid) { + sum += ints[i]; + } + for (int j = n - 1; j >= 0; --j) { + long s2 = sum; + dp12[j] = s2 + dp3[n]; + for (int k = 0; k <= j; ++k) { + s2 -= grid[k][i]; + long v = Math.max(dp1[k] + s2, dp3[j] + s2); + v = Math.max(v, pre + s2); + dp12[j] = Math.max(dp12[j], v); + if (k == j) { + dp22[j] = dp32[j] = v; + res = Math.max(res, v); + } + } + if (i > 0) { + pre = Math.max(pre + grid[j][i], dp2[j] + grid[j][i]); + } + sum -= grid[j][i]; + } + dp22[n] = dp32[n] = pre; + res = Math.max(res, pre); + for (int j = 1; j <= n; ++j) { + dp32[j] = Math.max(dp32[j], dp32[j - 1]); + } + long[] tem = dp1; + dp1 = dp12; + dp12 = tem; + tem = dp2; + dp2 = dp22; + dp22 = tem; + tem = dp3; + dp3 = dp32; + dp32 = tem; + } + return res; + } +} diff --git a/src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations/readme.md b/src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations/readme.md new file mode 100644 index 000000000..90081c6fc --- /dev/null +++ b/src/main/java/g3201_3300/s3225_maximum_score_from_grid_operations/readme.md @@ -0,0 +1,39 @@ +3225\. Maximum Score From Grid Operations + +Hard + +You are given a 2D matrix `grid` of size `n x n`. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices `(i, j)`, and color black all the cells of the jth column starting from the top row down to the ith row. + +The grid score is the sum of all `grid[i][j]` such that cell `(i, j)` is white and it has a horizontally adjacent black cell. + +Return the **maximum** score that can be achieved after some number of operations. + +**Example 1:** + +**Input:** grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]] + +**Output:** 11 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/11/one.png) + +In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is `grid[3][0] + grid[1][2] + grid[3][3]` which is equal to 11. + +**Example 2:** + +**Input:** grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]] + +**Output:** 94 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/11/two-1.png) + +We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is `grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4]` which is equal to 94. + +**Constraints:** + +* `1 <= n == grid.length <= 100` +* `n == grid[i].length` +* 0 <= grid[i][j] <= 109 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/Solution.java b/src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/Solution.java new file mode 100644 index 000000000..f8d66b1f9 --- /dev/null +++ b/src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/Solution.java @@ -0,0 +1,22 @@ +package g3201_3300.s3226_number_of_bit_changes_to_make_two_integers_equal; + +// #Easy #Bit_Manipulation #2024_07_23_Time_0_ms_(100.00%)_Space_40.5_MB_(97.19%) + +public class Solution { + public int minChanges(int n, int k) { + if ((n | k) != n) { + return -1; + } + int cnt = 0; + while (n > 0 || k > 0) { + int bitN = n & 1; + int bitK = k & 1; + if (bitN == 1 && bitK == 0) { + cnt++; + } + n >>= 1; + k >>= 1; + } + return cnt; + } +} diff --git a/src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/readme.md b/src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/readme.md new file mode 100644 index 000000000..e03ae0e81 --- /dev/null +++ b/src/main/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/readme.md @@ -0,0 +1,41 @@ +3226\. Number of Bit Changes to Make Two Integers Equal + +Easy + +You are given two positive integers `n` and `k`. + +You can choose **any** bit in the **binary representation** of `n` that is equal to 1 and change it to 0. + +Return the _number of changes_ needed to make `n` equal to `k`. If it is impossible, return -1. + +**Example 1:** + +**Input:** n = 13, k = 4 + +**Output:** 2 + +**Explanation:** + Initially, the binary representations of `n` and `k` are n = (1101)2 and k = (0100)2. + We can change the first and fourth bits of `n`. The resulting integer is n = (**0**10**0**)2 = k. + +**Example 2:** + +**Input:** n = 21, k = 21 + +**Output:** 0 + +**Explanation:** + `n` and `k` are already equal, so no changes are needed. + +**Example 3:** + +**Input:** n = 14, k = 13 + +**Output:** \-1 + +**Explanation:** + It is not possible to make `n` equal to `k`. + +**Constraints:** + +* 1 <= n, k <= 106 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3227_vowels_game_in_a_string/Solution.java b/src/main/java/g3201_3300/s3227_vowels_game_in_a_string/Solution.java new file mode 100644 index 000000000..5056d1896 --- /dev/null +++ b/src/main/java/g3201_3300/s3227_vowels_game_in_a_string/Solution.java @@ -0,0 +1,16 @@ +package g3201_3300.s3227_vowels_game_in_a_string; + +// #Medium #String #Math #Game_Theory #Brainteaser +// #2024_07_23_Time_4_ms_(96.15%)_Space_45.3_MB_(96.39%) + +public class Solution { + public boolean doesAliceWin(String s) { + for (int i = 0; i < s.length(); i++) { + char curr = s.charAt(i); + if (curr == 'a' || curr == 'e' || curr == 'i' || curr == 'o' || curr == 'u') { + return true; + } + } + return false; + } +} diff --git a/src/main/java/g3201_3300/s3227_vowels_game_in_a_string/readme.md b/src/main/java/g3201_3300/s3227_vowels_game_in_a_string/readme.md new file mode 100644 index 000000000..770a35741 --- /dev/null +++ b/src/main/java/g3201_3300/s3227_vowels_game_in_a_string/readme.md @@ -0,0 +1,44 @@ +3227\. Vowels Game in a String + +Medium + +Alice and Bob are playing a game on a string. + +You are given a string `s`, Alice and Bob will take turns playing the following game where Alice starts **first**: + +* On Alice's turn, she has to remove any **non-empty** substring from `s` that contains an **odd** number of vowels. +* On Bob's turn, he has to remove any **non-empty** substring from `s` that contains an **even** number of vowels. + +The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play **optimally**. + +Return `true` if Alice wins the game, and `false` otherwise. + +The English vowels are: `a`, `e`, `i`, `o`, and `u`. + +**Example 1:** + +**Input:** s = "leetcoder" + +**Output:** true + +**Explanation:** + Alice can win the game as follows: + +* Alice plays first, she can delete the underlined substring in s = "**leetco**der" which contains 3 vowels. The resulting string is `s = "der"`. +* Bob plays second, he can delete the underlined substring in s = "**d**er" which contains 0 vowels. The resulting string is `s = "er"`. +* Alice plays third, she can delete the whole string s = "**er**" which contains 1 vowel. +* Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game. + +**Example 2:** + +**Input:** s = "bbcd" + +**Output:** false + +**Explanation:** + There is no valid play for Alice in her first turn, so Alice loses the game. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/Solution.java b/src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/Solution.java new file mode 100644 index 000000000..ad996e081 --- /dev/null +++ b/src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/Solution.java @@ -0,0 +1,19 @@ +package g3201_3300.s3228_maximum_number_of_operations_to_move_ones_to_the_end; + +// #Medium #String #Greedy #Counting #2024_07_23_Time_5_ms_(100.00%)_Space_45.2_MB_(89.96%) + +public class Solution { + public int maxOperations(String s) { + char[] arr = s.toCharArray(); + int result = 0; + int ones = 0; + int n = arr.length; + for (int i = 0; i < n; ++i) { + ones += arr[i] - '0'; + if (i > 0 && arr[i] < arr[i - 1]) { + result += ones; + } + } + return result; + } +} diff --git a/src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/readme.md b/src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/readme.md new file mode 100644 index 000000000..edd953a6f --- /dev/null +++ b/src/main/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/readme.md @@ -0,0 +1,38 @@ +3228\. Maximum Number of Operations to Move Ones to the End + +Medium + +You are given a binary string `s`. + +You can perform the following operation on the string **any** number of times: + +* Choose **any** index `i` from the string where `i + 1 < s.length` such that `s[i] == '1'` and `s[i + 1] == '0'`. +* Move the character `s[i]` to the **right** until it reaches the end of the string or another `'1'`. For example, for `s = "010010"`, if we choose `i = 1`, the resulting string will be s = "0**001**10". + +Return the **maximum** number of operations that you can perform. + +**Example 1:** + +**Input:** s = "1001101" + +**Output:** 4 + +**Explanation:** + +We can perform the following operations: + +* Choose index `i = 0`. The resulting string is s = "**001**1101". +* Choose index `i = 4`. The resulting string is s = "0011**01**1". +* Choose index `i = 3`. The resulting string is s = "001**01**11". +* Choose index `i = 2`. The resulting string is s = "00**01**111". + +**Example 2:** + +**Input:** s = "00111" + +**Output:** 0 + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/Solution.java b/src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/Solution.java new file mode 100644 index 000000000..a09970f4c --- /dev/null +++ b/src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/Solution.java @@ -0,0 +1,32 @@ +package g3201_3300.s3229_minimum_operations_to_make_array_equal_to_target; + +// #Hard #Array #Dynamic_Programming #Greedy #Stack #Monotonic_Stack +// #2024_07_23_Time_2_ms_(100.00%)_Space_58.3_MB_(71.80%) + +public class Solution { + public long minimumOperations(int[] nums, int[] target) { + int n = nums.length; + long incr = 0; + long decr = 0; + long ops = 0; + for (int i = 0; i < n; i++) { + int diff = target[i] - nums[i]; + if (diff > 0) { + if (incr < diff) { + ops += diff - incr; + } + incr = diff; + decr = 0; + } else if (diff < 0) { + if (decr < -diff) { + ops += -diff - decr; + } + decr = -diff; + incr = 0; + } else { + incr = decr = 0; + } + } + return ops; + } +} diff --git a/src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/readme.md b/src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/readme.md new file mode 100644 index 000000000..12dc0737e --- /dev/null +++ b/src/main/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/readme.md @@ -0,0 +1,41 @@ +3229\. Minimum Operations to Make Array Equal to Target + +Hard + +You are given two positive integer arrays `nums` and `target`, of the same length. + +In a single operation, you can select any subarray of `nums` and increment or decrement each element within that subarray by 1. + +Return the **minimum** number of operations required to make `nums` equal to the array `target`. + +**Example 1:** + +**Input:** nums = [3,5,1,2], target = [4,6,2,4] + +**Output:** 2 + +**Explanation:** + +We will perform the following operations to make `nums` equal to `target`: + \- Increment `nums[0..3]` by 1, `nums = [4,6,2,3]`. + \- Increment `nums[3..3]` by 1, `nums = [4,6,2,4]`. + +**Example 2:** + +**Input:** nums = [1,3,2], target = [2,1,4] + +**Output:** 5 + +**Explanation:** + +We will perform the following operations to make `nums` equal to `target`: + \- Increment `nums[0..0]` by 1, `nums = [2,3,2]`. + \- Decrement `nums[1..1]` by 1, `nums = [2,2,2]`. + \- Decrement `nums[1..1]` by 1, `nums = [2,1,2]`. + \- Increment `nums[2..2]` by 1, `nums = [2,1,3]`. + \- Increment `nums[2..2]` by 1, `nums = [2,1,4]`. + +**Constraints:** + +* 1 <= nums.length == target.length <= 105 +* 1 <= nums[i], target[i] <= 108 \ No newline at end of file diff --git a/src/test/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/SolutionTest.java b/src/test/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/SolutionTest.java new file mode 100644 index 000000000..719296f9f --- /dev/null +++ b/src/test/java/g3201_3300/s3222_find_the_winning_player_in_coin_game/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3222_find_the_winning_player_in_coin_game; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void losingPlayer() { + assertThat(new Solution().losingPlayer(2, 7), equalTo("Alice")); + } + + @Test + void losingPlayer2() { + assertThat(new Solution().losingPlayer(4, 11), equalTo("Bob")); + } +} diff --git a/src/test/java/g3201_3300/s3223_minimum_length_of_string_after_operations/SolutionTest.java b/src/test/java/g3201_3300/s3223_minimum_length_of_string_after_operations/SolutionTest.java new file mode 100644 index 000000000..ccbc049ea --- /dev/null +++ b/src/test/java/g3201_3300/s3223_minimum_length_of_string_after_operations/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3223_minimum_length_of_string_after_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumLength() { + assertThat(new Solution().minimumLength("abaacbcbb"), equalTo(5)); + } + + @Test + void minimumLength2() { + assertThat(new Solution().minimumLength("aa"), equalTo(2)); + } +} diff --git a/src/test/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/SolutionTest.java b/src/test/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/SolutionTest.java new file mode 100644 index 000000000..653c214f8 --- /dev/null +++ b/src/test/java/g3201_3300/s3224_minimum_array_changes_to_make_differences_equal/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3224_minimum_array_changes_to_make_differences_equal; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minChanges() { + assertThat(new Solution().minChanges(new int[] {1, 0, 1, 2, 4, 3}, 4), equalTo(2)); + } + + @Test + void minChanges2() { + assertThat(new Solution().minChanges(new int[] {0, 1, 2, 3, 3, 6, 5, 4}, 6), equalTo(2)); + } +} diff --git a/src/test/java/g3201_3300/s3225_maximum_score_from_grid_operations/SolutionTest.java b/src/test/java/g3201_3300/s3225_maximum_score_from_grid_operations/SolutionTest.java new file mode 100644 index 000000000..00d7eec8d --- /dev/null +++ b/src/test/java/g3201_3300/s3225_maximum_score_from_grid_operations/SolutionTest.java @@ -0,0 +1,38 @@ +package g3201_3300.s3225_maximum_score_from_grid_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumScore() { + assertThat( + new Solution() + .maximumScore( + new int[][] { + {0, 0, 0, 0, 0}, + {0, 0, 3, 0, 0}, + {0, 1, 0, 0, 0}, + {5, 0, 0, 3, 0}, + {0, 0, 0, 0, 2} + }), + equalTo(11L)); + } + + @Test + void maximumScore2() { + assertThat( + new Solution() + .maximumScore( + new int[][] { + {10, 9, 0, 0, 15}, + {7, 1, 0, 8, 0}, + {5, 20, 0, 11, 0}, + {0, 0, 0, 1, 2}, + {8, 12, 1, 10, 3} + }), + equalTo(94L)); + } +} diff --git a/src/test/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/SolutionTest.java b/src/test/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/SolutionTest.java new file mode 100644 index 000000000..8801746b4 --- /dev/null +++ b/src/test/java/g3201_3300/s3226_number_of_bit_changes_to_make_two_integers_equal/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3226_number_of_bit_changes_to_make_two_integers_equal; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minChanges() { + assertThat(new Solution().minChanges(13, 4), equalTo(2)); + } + + @Test + void minChanges2() { + assertThat(new Solution().minChanges(21, 21), equalTo(0)); + } + + @Test + void minChanges3() { + assertThat(new Solution().minChanges(14, 13), equalTo(-1)); + } +} diff --git a/src/test/java/g3201_3300/s3227_vowels_game_in_a_string/SolutionTest.java b/src/test/java/g3201_3300/s3227_vowels_game_in_a_string/SolutionTest.java new file mode 100644 index 000000000..e0d40fc16 --- /dev/null +++ b/src/test/java/g3201_3300/s3227_vowels_game_in_a_string/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3227_vowels_game_in_a_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void doesAliceWin() { + assertThat(new Solution().doesAliceWin("leetcoder"), equalTo(true)); + } + + @Test + void doesAliceWin2() { + assertThat(new Solution().doesAliceWin("bbcd"), equalTo(false)); + } +} diff --git a/src/test/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/SolutionTest.java b/src/test/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/SolutionTest.java new file mode 100644 index 000000000..566bec53f --- /dev/null +++ b/src/test/java/g3201_3300/s3228_maximum_number_of_operations_to_move_ones_to_the_end/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3228_maximum_number_of_operations_to_move_ones_to_the_end; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxOperations() { + assertThat(new Solution().maxOperations("1001101"), equalTo(4)); + } + + @Test + void maxOperations2() { + assertThat(new Solution().maxOperations("00111"), equalTo(0)); + } +} diff --git a/src/test/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/SolutionTest.java b/src/test/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/SolutionTest.java new file mode 100644 index 000000000..0f67df83d --- /dev/null +++ b/src/test/java/g3201_3300/s3229_minimum_operations_to_make_array_equal_to_target/SolutionTest.java @@ -0,0 +1,22 @@ +package g3201_3300.s3229_minimum_operations_to_make_array_equal_to_target; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumOperations() { + assertThat( + new Solution().minimumOperations(new int[] {3, 5, 1, 2}, new int[] {4, 6, 2, 4}), + equalTo(2L)); + } + + @Test + void minimumOperations2() { + assertThat( + new Solution().minimumOperations(new int[] {1, 3, 2}, new int[] {2, 1, 4}), + equalTo(5L)); + } +}