Skip to content

Added tasks 3174-3181 #659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/kotlin/g3101_3200/s3174_clear_digits/Solution.kt
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_15_Time_180_ms_(70.18%)_Space_35.1_MB_(94.74%)

class Solution {
fun clearDigits(s: String): String {
val result = StringBuilder()
for (ch in s.toCharArray()) {
if (ch in '0'..'9') {
if (result.isNotEmpty()) {
result.deleteCharAt(result.length - 1)
}
} else {
result.append(ch)
}
}
return result.toString()
}
}
39 changes: 39 additions & 0 deletions src/main/kotlin/g3101_3200/s3174_clear_digits/readme.md
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.
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_15_Time_536_ms_(100.00%)_Space_63.9_MB_(81.82%)

class Solution {
fun findWinningPlayer(skills: IntArray, k: Int): Int {
val n = skills.size
var max = skills[0]
var cnt = 0
var res = 0
for (i in 1 until n) {
if (skills[i] > max) {
max = skills[i]
cnt = 0
res = i
}
cnt += 1
if (cnt == k) {
break
}
}
return res
}
}
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package g3101_3200.s3176_find_the_maximum_length_of_a_good_subsequence_i

// #Medium #Array #Hash_Table #Dynamic_Programming
// #2024_06_15_Time_183_ms_(100.00%)_Space_37.6_MB_(91.30%)

import kotlin.math.max

class Solution {
fun maximumLength(nums: IntArray, k: Int): Int {
val n = nums.size
var count = 0
for (i in 0 until nums.size - 1) {
if (nums[i] != nums[i + 1]) {
count++
}
}
if (count <= k) {
return n
}
val max = IntArray(k + 1)
max.fill(1)
val vis = IntArray(n)
vis.fill(-1)
val map = HashMap<Int, Int>()
for (i in 0 until n) {
if (!map.containsKey(nums[i])) {
map[nums[i]] = i + 1
} else {
vis[i] = map[nums[i]]!! - 1
map[nums[i]] = i + 1
}
}
val dp = Array(n) { IntArray(k + 1) }
for (i in 0 until n) {
for (j in 0..k) {
dp[i][j] = 1
}
}
for (i in 1 until n) {
for (j in k - 1 downTo 0) {
dp[i][j + 1] = max(dp[i][j + 1], (1 + max[j]))
max[j + 1] = max(max[j + 1], dp[i][j + 1])
}
if (vis[i] != -1) {
val a = vis[i]
for (j in 0..k) {
dp[i][j] = max(dp[i][j], (1 + dp[a][j]))
max[j] = max(dp[i][j], max[j])
}
}
}
var ans = 1
for (i in 0..k) {
ans = max(ans, max[i])
}
return ans
}
}
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)`
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_15_Time_284_ms_(100.00%)_Space_40.3_MB_(100.00%)

import kotlin.math.max

class Solution {
fun maximumLength(nums: IntArray, k: Int): Int {
val hm = HashMap<Int, Int>()
val n = nums.size
val pre = IntArray(n)
for (i in 0 until n) {
pre[i] = hm.getOrDefault(nums[i], -1)
hm[nums[i]] = i
}
val dp = Array(k + 1) { IntArray(n) }
for (i in 0 until n) {
dp[0][i] = 1
if (pre[i] >= 0) {
dp[0][i] = dp[0][pre[i]] + 1
}
}
for (i in 1..k) {
var max = 0
for (j in 0 until n) {
if (pre[j] >= 0) {
dp[i][j] = dp[i][pre[j]] + 1
}
dp[i][j] = max(dp[i][j], (max + 1))
max = max(max, dp[i - 1][j])
}
}
var max = 0
for (i in 0 until n) {
max = max(max, dp[k][i])
}
return max
}
}
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)`
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_15_Time_136_ms_(82.35%)_Space_33.7_MB_(45.10%)

class Solution {
fun numberOfChild(n: Int, k: Int): Int {
val bigN = 2 * n - 2
val x = k % bigN
return if (x < n) x else bigN - x
}
}
Loading