forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tasks 3174, 3175, 3176, 3177, 3178
- Loading branch information
Showing
15 changed files
with
525 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
24 changes: 24 additions & 0 deletions
24
src/main/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...n/java/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` | ||
* <code>2 <= n <= 10<sup>5</sup></code> | ||
* <code>1 <= k <= 10<sup>9</sup></code> | ||
* <code>1 <= skills[i] <= 10<sup>6</sup></code> | ||
* All integers in `skills` are unique. |
59 changes: 59 additions & 0 deletions
59
src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer, Integer> 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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <code>[<ins>1</ins>,<ins>2</ins>,<ins>1</ins>,<ins>1</ins>,3]</code>. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,2,3,4,5,1], k = 0 | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** | ||
|
||
The maximum length subsequence is <code>[<ins>1</ins>,2,3,4,5,<ins>1</ins>]</code>. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 500` | ||
* <code>1 <= nums[i] <= 10<sup>9</sup></code> | ||
* `0 <= k <= min(nums.length, 25)` |
40 changes: 40 additions & 0 deletions
40
...main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer, Integer> 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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ava/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <code>[<ins>1</ins>,<ins>2</ins>,<ins>1</ins>,<ins>1</ins>,3]</code>. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,2,3,4,5,1], k = 0 | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** | ||
|
||
The maximum length subsequence is <code>[<ins>1</ins>,2,3,4,5,<ins>1</ins>]</code>. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length <= 5 * 10<sup>3</sup></code> | ||
* <code>1 <= nums[i] <= 10<sup>9</sup></code> | ||
* `0 <= k <= min(50, nums.length)` |
11 changes: 11 additions & 0 deletions
11
src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.