Skip to content

Commit fa454ab

Browse files
authored
Added tasks 3174-3181
1 parent da120bc commit fa454ab

File tree

24 files changed

+865
-0
lines changed

24 files changed

+865
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3101_3200.s3174_clear_digits
2+
3+
// #Easy #String #Hash_Table #Simulation #2024_06_15_Time_180_ms_(70.18%)_Space_35.1_MB_(94.74%)
4+
5+
class Solution {
6+
fun clearDigits(s: String): String {
7+
val result = StringBuilder()
8+
for (ch in s.toCharArray()) {
9+
if (ch in '0'..'9') {
10+
if (result.isNotEmpty()) {
11+
result.deleteCharAt(result.length - 1)
12+
}
13+
} else {
14+
result.append(ch)
15+
}
16+
}
17+
return result.toString()
18+
}
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3174\. Clear Digits
2+
3+
Easy
4+
5+
You are given a string `s`.
6+
7+
Your task is to remove **all** digits by doing this operation repeatedly:
8+
9+
* Delete the _first_ digit and the **closest** **non-digit** character to its _left_.
10+
11+
Return the resulting string after removing all digits.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "abc"
16+
17+
**Output:** "abc"
18+
19+
**Explanation:**
20+
21+
There is no digit in the string.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "cb34"
26+
27+
**Output:** ""
28+
29+
**Explanation:**
30+
31+
First, we apply the operation on `s[2]`, and `s` becomes `"c4"`.
32+
33+
Then we apply the operation on `s[1]`, and `s` becomes `""`.
34+
35+
**Constraints:**
36+
37+
* `1 <= s.length <= 100`
38+
* `s` consists only of lowercase English letters and digits.
39+
* The input is generated such that it is possible to delete all digits.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3101_3200.s3175_find_the_first_player_to_win_k_games_in_a_row
2+
3+
// #Medium #Array #Simulation #2024_06_15_Time_536_ms_(100.00%)_Space_63.9_MB_(81.82%)
4+
5+
class Solution {
6+
fun findWinningPlayer(skills: IntArray, k: Int): Int {
7+
val n = skills.size
8+
var max = skills[0]
9+
var cnt = 0
10+
var res = 0
11+
for (i in 1 until n) {
12+
if (skills[i] > max) {
13+
max = skills[i]
14+
cnt = 0
15+
res = i
16+
}
17+
cnt += 1
18+
if (cnt == k) {
19+
break
20+
}
21+
}
22+
return res
23+
}
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3175\. Find The First Player to win K Games in a Row
2+
3+
Medium
4+
5+
A competition consists of `n` players numbered from `0` to `n - 1`.
6+
7+
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**.
8+
9+
All players are standing in a queue in order from player `0` to player `n - 1`.
10+
11+
The competition process is as follows:
12+
13+
* The first two players in the queue play a game, and the player with the **higher** skill level wins.
14+
* After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.
15+
16+
The winner of the competition is the **first** player who wins `k` games **in a row**.
17+
18+
Return the initial index of the _winning_ player.
19+
20+
**Example 1:**
21+
22+
**Input:** skills = [4,2,6,3,9], k = 2
23+
24+
**Output:** 2
25+
26+
**Explanation:**
27+
28+
Initially, the queue of players is `[0,1,2,3,4]`. The following process happens:
29+
30+
* 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]`.
31+
* 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]`.
32+
* 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]`.
33+
34+
Player 2 won `k = 2` games in a row, so the winner is player 2.
35+
36+
**Example 2:**
37+
38+
**Input:** skills = [2,5,4], k = 3
39+
40+
**Output:** 1
41+
42+
**Explanation:**
43+
44+
Initially, the queue of players is `[0,1,2]`. The following process happens:
45+
46+
* 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]`.
47+
* 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]`.
48+
* 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]`.
49+
50+
Player 1 won `k = 3` games in a row, so the winner is player 1.
51+
52+
**Constraints:**
53+
54+
* `n == skills.length`
55+
* <code>2 <= n <= 10<sup>5</sup></code>
56+
* <code>1 <= k <= 10<sup>9</sup></code>
57+
* <code>1 <= skills[i] <= 10<sup>6</sup></code>
58+
* All integers in `skills` are unique.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3101_3200.s3176_find_the_maximum_length_of_a_good_subsequence_i
2+
3+
// #Medium #Array #Hash_Table #Dynamic_Programming
4+
// #2024_06_15_Time_183_ms_(100.00%)_Space_37.6_MB_(91.30%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maximumLength(nums: IntArray, k: Int): Int {
10+
val n = nums.size
11+
var count = 0
12+
for (i in 0 until nums.size - 1) {
13+
if (nums[i] != nums[i + 1]) {
14+
count++
15+
}
16+
}
17+
if (count <= k) {
18+
return n
19+
}
20+
val max = IntArray(k + 1)
21+
max.fill(1)
22+
val vis = IntArray(n)
23+
vis.fill(-1)
24+
val map = HashMap<Int, Int>()
25+
for (i in 0 until n) {
26+
if (!map.containsKey(nums[i])) {
27+
map[nums[i]] = i + 1
28+
} else {
29+
vis[i] = map[nums[i]]!! - 1
30+
map[nums[i]] = i + 1
31+
}
32+
}
33+
val dp = Array(n) { IntArray(k + 1) }
34+
for (i in 0 until n) {
35+
for (j in 0..k) {
36+
dp[i][j] = 1
37+
}
38+
}
39+
for (i in 1 until n) {
40+
for (j in k - 1 downTo 0) {
41+
dp[i][j + 1] = max(dp[i][j + 1], (1 + max[j]))
42+
max[j + 1] = max(max[j + 1], dp[i][j + 1])
43+
}
44+
if (vis[i] != -1) {
45+
val a = vis[i]
46+
for (j in 0..k) {
47+
dp[i][j] = max(dp[i][j], (1 + dp[a][j]))
48+
max[j] = max(dp[i][j], max[j])
49+
}
50+
}
51+
}
52+
var ans = 1
53+
for (i in 0..k) {
54+
ans = max(ans, max[i])
55+
}
56+
return ans
57+
}
58+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
3176\. Find the Maximum Length of a Good Subsequence I
2+
3+
Medium
4+
5+
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]`.
6+
7+
Return the **maximum** possible length of a **good** subsequence of `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,1,1,3], k = 2
12+
13+
**Output:** 4
14+
15+
**Explanation:**
16+
17+
The maximum length subsequence is <code>[<ins>1</ins>,<ins>2</ins>,<ins>1</ins>,<ins>1</ins>,3]</code>.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,2,3,4,5,1], k = 0
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
The maximum length subsequence is <code>[<ins>1</ins>,2,3,4,5,<ins>1</ins>]</code>.
28+
29+
**Constraints:**
30+
31+
* `1 <= nums.length <= 500`
32+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
33+
* `0 <= k <= min(nums.length, 25)`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3101_3200.s3177_find_the_maximum_length_of_a_good_subsequence_ii
2+
3+
// #Hard #Array #Hash_Table #Dynamic_Programming
4+
// #2024_06_15_Time_284_ms_(100.00%)_Space_40.3_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maximumLength(nums: IntArray, k: Int): Int {
10+
val hm = HashMap<Int, Int>()
11+
val n = nums.size
12+
val pre = IntArray(n)
13+
for (i in 0 until n) {
14+
pre[i] = hm.getOrDefault(nums[i], -1)
15+
hm[nums[i]] = i
16+
}
17+
val dp = Array(k + 1) { IntArray(n) }
18+
for (i in 0 until n) {
19+
dp[0][i] = 1
20+
if (pre[i] >= 0) {
21+
dp[0][i] = dp[0][pre[i]] + 1
22+
}
23+
}
24+
for (i in 1..k) {
25+
var max = 0
26+
for (j in 0 until n) {
27+
if (pre[j] >= 0) {
28+
dp[i][j] = dp[i][pre[j]] + 1
29+
}
30+
dp[i][j] = max(dp[i][j], (max + 1))
31+
max = max(max, dp[i - 1][j])
32+
}
33+
}
34+
var max = 0
35+
for (i in 0 until n) {
36+
max = max(max, dp[k][i])
37+
}
38+
return max
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
3177\. Find the Maximum Length of a Good Subsequence II
2+
3+
Hard
4+
5+
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]`.
6+
7+
Return the **maximum** possible length of a **good** subsequence of `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,1,1,3], k = 2
12+
13+
**Output:** 4
14+
15+
**Explanation:**
16+
17+
The maximum length subsequence is <code>[<ins>1</ins>,<ins>2</ins>,<ins>1</ins>,<ins>1</ins>,3]</code>.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,2,3,4,5,1], k = 0
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
The maximum length subsequence is <code>[<ins>1</ins>,2,3,4,5,<ins>1</ins>]</code>.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 5 * 10<sup>3</sup></code>
32+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
33+
* `0 <= k <= min(50, nums.length)`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g3101_3200.s3178_find_the_child_who_has_the_ball_after_k_seconds
2+
3+
// #Easy #Math #Simulation #2024_06_15_Time_136_ms_(82.35%)_Space_33.7_MB_(45.10%)
4+
5+
class Solution {
6+
fun numberOfChild(n: Int, k: Int): Int {
7+
val bigN = 2 * n - 2
8+
val x = k % bigN
9+
return if (x < n) x else bigN - x
10+
}
11+
}

0 commit comments

Comments
 (0)