diff --git a/src/main/java/g3101_3200/s3174_clear_digits/Solution.java b/src/main/java/g3101_3200/s3174_clear_digits/Solution.java new file mode 100644 index 000000000..9d87f7d34 --- /dev/null +++ b/src/main/java/g3101_3200/s3174_clear_digits/Solution.java @@ -0,0 +1,19 @@ +package g3101_3200.s3174_clear_digits; + +// #Easy #String #Hash_Table #Simulation #2024_06_12_Time_1_ms_(100.00%)_Space_42.1_MB_(96.47%) + +public class Solution { + public String clearDigits(String s) { + StringBuilder result = new StringBuilder(); + for (char ch : s.toCharArray()) { + if (ch >= '0' && ch <= '9') { + if (!result.isEmpty()) { + result.deleteCharAt(result.length() - 1); + } + } else { + result.append(ch); + } + } + return String.valueOf(result); + } +} diff --git a/src/main/java/g3101_3200/s3174_clear_digits/readme.md b/src/main/java/g3101_3200/s3174_clear_digits/readme.md new file mode 100644 index 000000000..9dab03487 --- /dev/null +++ b/src/main/java/g3101_3200/s3174_clear_digits/readme.md @@ -0,0 +1,39 @@ +3174\. Clear Digits + +Easy + +You are given a string `s`. + +Your task is to remove **all** digits by doing this operation repeatedly: + +* Delete the _first_ digit and the **closest** **non-digit** character to its _left_. + +Return the resulting string after removing all digits. + +**Example 1:** + +**Input:** s = "abc" + +**Output:** "abc" + +**Explanation:** + +There is no digit in the string. + +**Example 2:** + +**Input:** s = "cb34" + +**Output:** "" + +**Explanation:** + +First, we apply the operation on `s[2]`, and `s` becomes `"c4"`. + +Then we apply the operation on `s[1]`, and `s` becomes `""`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists only of lowercase English letters and digits. +* The input is generated such that it is possible to delete all digits. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/Solution.java b/src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/Solution.java new file mode 100644 index 000000000..90cc9d63c --- /dev/null +++ b/src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/Solution.java @@ -0,0 +1,24 @@ +package g3101_3200.s3175_find_the_first_player_to_win_k_games_in_a_row; + +// #Medium #Array #Simulation #2024_06_12_Time_1_ms_(100.00%)_Space_60.4_MB_(70.11%) + +public class Solution { + public int findWinningPlayer(int[] skills, int k) { + int n = skills.length; + int max = skills[0]; + int cnt = 0; + int res = 0; + for (int i = 1; i < n; i++) { + if (skills[i] > max) { + max = skills[i]; + cnt = 0; + res = i; + } + cnt += 1; + if (cnt == k) { + break; + } + } + return res; + } +} diff --git a/src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md b/src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md new file mode 100644 index 000000000..0c5e09fdf --- /dev/null +++ b/src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md @@ -0,0 +1,58 @@ +3175\. Find The First Player to win K Games in a Row + +Medium + +A competition consists of `n` players numbered from `0` to `n - 1`. + +You are given an integer array `skills` of size `n` and a **positive** integer `k`, where `skills[i]` is the skill level of player `i`. All integers in `skills` are **unique**. + +All players are standing in a queue in order from player `0` to player `n - 1`. + +The competition process is as follows: + +* The first two players in the queue play a game, and the player with the **higher** skill level wins. +* After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it. + +The winner of the competition is the **first** player who wins `k` games **in a row**. + +Return the initial index of the _winning_ player. + +**Example 1:** + +**Input:** skills = [4,2,6,3,9], k = 2 + +**Output:** 2 + +**Explanation:** + +Initially, the queue of players is `[0,1,2,3,4]`. The following process happens: + +* Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is `[0,2,3,4,1]`. +* Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is `[2,3,4,1,0]`. +* Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is `[2,4,1,0,3]`. + +Player 2 won `k = 2` games in a row, so the winner is player 2. + +**Example 2:** + +**Input:** skills = [2,5,4], k = 3 + +**Output:** 1 + +**Explanation:** + +Initially, the queue of players is `[0,1,2]`. The following process happens: + +* Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`. +* Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is `[1,0,2]`. +* Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`. + +Player 1 won `k = 3` games in a row, so the winner is player 1. + +**Constraints:** + +* `n == skills.length` +* 2 <= n <= 105 +* 1 <= k <= 109 +* 1 <= skills[i] <= 106 +* All integers in `skills` are unique. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.java b/src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.java new file mode 100644 index 000000000..652e6d4b5 --- /dev/null +++ b/src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.java @@ -0,0 +1,59 @@ +package g3101_3200.s3176_find_the_maximum_length_of_a_good_subsequence_i; + +// #Medium #Array #Hash_Table #Dynamic_Programming +// #2024_06_12_Time_4_ms_(99.70%)_Space_44_MB_(87.51%) + +import java.util.Arrays; +import java.util.HashMap; + +public class Solution { + public int maximumLength(int[] nums, int k) { + int n = nums.length; + int count = 0; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] != nums[i + 1]) { + count++; + } + } + if (count <= k) { + return n; + } + int[] max = new int[k + 1]; + Arrays.fill(max, 1); + int[] vis = new int[n]; + Arrays.fill(vis, -1); + HashMap map = new HashMap<>(); + for (int i = 0; i < n; i++) { + if (!map.containsKey(nums[i])) { + map.put(nums[i], i + 1); + } else { + vis[i] = map.get(nums[i]) - 1; + map.put(nums[i], i + 1); + } + } + int[][] dp = new int[n][k + 1]; + for (int i = 0; i < n; i++) { + for (int j = 0; j <= k; j++) { + dp[i][j] = 1; + } + } + for (int i = 1; i < n; i++) { + for (int j = k - 1; j >= 0; j--) { + dp[i][j + 1] = Math.max(dp[i][j + 1], 1 + max[j]); + max[j + 1] = Math.max(max[j + 1], dp[i][j + 1]); + } + if (vis[i] != -1) { + int a = vis[i]; + for (int j = 0; j <= k; j++) { + dp[i][j] = Math.max(dp[i][j], 1 + dp[a][j]); + max[j] = Math.max(dp[i][j], max[j]); + } + } + } + int ans = 1; + for (int i = 0; i <= k; i++) { + ans = Math.max(ans, max[i]); + } + return ans; + } +} diff --git a/src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md b/src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md new file mode 100644 index 000000000..2d7b340ef --- /dev/null +++ b/src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md @@ -0,0 +1,33 @@ +3176\. Find the Maximum Length of a Good Subsequence I + +Medium + +You are given an integer array `nums` and a **non-negative** integer `k`. A sequence of integers `seq` is called **good** if there are **at most** `k` indices `i` in the range `[0, seq.length - 2]` such that `seq[i] != seq[i + 1]`. + +Return the **maximum** possible length of a **good** subsequence of `nums`. + +**Example 1:** + +**Input:** nums = [1,2,1,1,3], k = 2 + +**Output:** 4 + +**Explanation:** + +The maximum length subsequence is [1,2,1,1,3]. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5,1], k = 0 + +**Output:** 2 + +**Explanation:** + +The maximum length subsequence is [1,2,3,4,5,1]. + +**Constraints:** + +* `1 <= nums.length <= 500` +* 1 <= nums[i] <= 109 +* `0 <= k <= min(nums.length, 25)` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.java b/src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.java new file mode 100644 index 000000000..e39971a2b --- /dev/null +++ b/src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.java @@ -0,0 +1,40 @@ +package g3101_3200.s3177_find_the_maximum_length_of_a_good_subsequence_ii; + +// #Hard #Array #Hash_Table #Dynamic_Programming +// #2024_06_12_Time_11_ms_(100.00%)_Space_45.8_MB_(90.55%) + +import java.util.HashMap; + +public class Solution { + public int maximumLength(int[] nums, int k) { + HashMap hm = new HashMap<>(); + int n = nums.length; + int[] pre = new int[n]; + for (int i = 0; i < n; i++) { + pre[i] = hm.getOrDefault(nums[i], -1); + hm.put(nums[i], i); + } + int[][] dp = new int[k + 1][n]; + for (int i = 0; i < n; i++) { + dp[0][i] = 1; + if (pre[i] >= 0) { + dp[0][i] = dp[0][pre[i]] + 1; + } + } + for (int i = 1; i <= k; i++) { + int max = 0; + for (int j = 0; j < n; j++) { + if (pre[j] >= 0) { + dp[i][j] = dp[i][pre[j]] + 1; + } + dp[i][j] = Math.max(dp[i][j], max + 1); + max = Math.max(max, dp[i - 1][j]); + } + } + int max = 0; + for (int i = 0; i < n; i++) { + max = Math.max(max, dp[k][i]); + } + return max; + } +} diff --git a/src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md b/src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md new file mode 100644 index 000000000..8c6b300b0 --- /dev/null +++ b/src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md @@ -0,0 +1,33 @@ +3177\. Find the Maximum Length of a Good Subsequence II + +Hard + +You are given an integer array `nums` and a **non-negative** integer `k`. A sequence of integers `seq` is called **good** if there are **at most** `k` indices `i` in the range `[0, seq.length - 2]` such that `seq[i] != seq[i + 1]`. + +Return the **maximum** possible length of a **good** subsequence of `nums`. + +**Example 1:** + +**Input:** nums = [1,2,1,1,3], k = 2 + +**Output:** 4 + +**Explanation:** + +The maximum length subsequence is [1,2,1,1,3]. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5,1], k = 0 + +**Output:** 2 + +**Explanation:** + +The maximum length subsequence is [1,2,3,4,5,1]. + +**Constraints:** + +* 1 <= nums.length <= 5 * 103 +* 1 <= nums[i] <= 109 +* `0 <= k <= min(50, nums.length)` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/Solution.java b/src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/Solution.java new file mode 100644 index 000000000..57dac331f --- /dev/null +++ b/src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/Solution.java @@ -0,0 +1,11 @@ +package g3101_3200.s3178_find_the_child_who_has_the_ball_after_k_seconds; + +// #Easy #Math #Simulation #2024_06_12_Time_0_ms_(100.00%)_Space_40.4_MB_(93.82%) + +public class Solution { + public int numberOfChild(int n, int k) { + int bigN = 2 * n - 2; + int x = k % bigN; + return (x < n) ? x : bigN - x; + } +} diff --git a/src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md b/src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md new file mode 100644 index 000000000..eafcd9098 --- /dev/null +++ b/src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md @@ -0,0 +1,114 @@ +3178\. Find the Child Who Has the Ball After K Seconds + +Easy + +You are given two **positive** integers `n` and `k`. There are `n` children numbered from `0` to `n - 1` standing in a queue _in order_ from left to right. + +Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches **either** end of the line, i.e. child 0 or child `n - 1`, the direction of passing is **reversed**. + +Return the number of the child who receives the ball after `k` seconds. + +**Example 1:** + +**Input:** n = 3, k = 5 + +**Output:** 1 + +**Explanation:** + +Time elapsed + +Children + +`0` + +[0, 1, 2] + +`1` + +[0, 1, 2] + +`2` + +[0, 1, 2] + +`3` + +[0, 1, 2] + +`4` + +[0, 1, 2] + +`5` + +[0, 1, 2] + +**Example 2:** + +**Input:** n = 5, k = 6 + +**Output:** 2 + +**Explanation:** + +Time elapsed + +Children + +`0` + +[0, 1, 2, 3, 4] + +`1` + +[0, 1, 2, 3, 4] + +`2` + +[0, 1, 2, 3, 4] + +`3` + +[0, 1, 2, 3, 4] + +`4` + +[0, 1, 2, 3, 4] + +`5` + +[0, 1, 2, 3, 4] + +`6` + +[0, 1, 2, 3, 4] + +**Example 3:** + +**Input:** n = 4, k = 2 + +**Output:** 2 + +**Explanation:** + +Time elapsed + +Children + +`0` + +[0, 1, 2, 3] + +`1` + +[0, 1, 2, 3] + +`2` + +[0, 1, 2, 3] + +**Constraints:** + +* `2 <= n <= 50` +* `1 <= k <= 50` \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3174_clear_digits/SolutionTest.java b/src/test/java/g3101_3200/s3174_clear_digits/SolutionTest.java new file mode 100644 index 000000000..edbe9064c --- /dev/null +++ b/src/test/java/g3101_3200/s3174_clear_digits/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3174_clear_digits; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void clearDigits() { + assertThat(new Solution().clearDigits("abc"), equalTo("abc")); + } + + @Test + void clearDigits2() { + assertThat(new Solution().clearDigits("cb34"), equalTo("")); + } +} diff --git a/src/test/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.java b/src/test/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.java new file mode 100644 index 000000000..612c190e5 --- /dev/null +++ b/src/test/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3175_find_the_first_player_to_win_k_games_in_a_row; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findWinningPlayer() { + assertThat(new Solution().findWinningPlayer(new int[] {4, 2, 6, 3, 9}, 2), equalTo(2)); + } + + @Test + void findWinningPlayer2() { + assertThat(new Solution().findWinningPlayer(new int[] {2, 5, 4}, 3), equalTo(1)); + } +} diff --git a/src/test/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.java b/src/test/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.java new file mode 100644 index 000000000..02688989b --- /dev/null +++ b/src/test/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3176_find_the_maximum_length_of_a_good_subsequence_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumLength() { + assertThat(new Solution().maximumLength(new int[] {1, 2, 1, 1, 3}, 2), equalTo(4)); + } + + @Test + void maximumLength2() { + assertThat(new Solution().maximumLength(new int[] {1, 2, 3, 4, 5, 1}, 0), equalTo(2)); + } +} diff --git a/src/test/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.java b/src/test/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.java new file mode 100644 index 000000000..28e1af488 --- /dev/null +++ b/src/test/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3177_find_the_maximum_length_of_a_good_subsequence_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumLength() { + assertThat(new Solution().maximumLength(new int[] {1, 2, 1, 1, 3}, 2), equalTo(4)); + } + + @Test + void maximumLength2() { + assertThat(new Solution().maximumLength(new int[] {1, 2, 3, 4, 5, 1}, 0), equalTo(2)); + } +} diff --git a/src/test/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.java b/src/test/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.java new file mode 100644 index 000000000..5955a6621 --- /dev/null +++ b/src/test/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3178_find_the_child_who_has_the_ball_after_k_seconds; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfChild() { + assertThat(new Solution().numberOfChild(3, 5), equalTo(1)); + } + + @Test + void numberOfChild2() { + assertThat(new Solution().numberOfChild(5, 6), equalTo(2)); + } + + @Test + void numberOfChild3() { + assertThat(new Solution().numberOfChild(4, 2), equalTo(2)); + } +}