Skip to content

Added tasks 3467-3475 #768

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 14 commits into from
Mar 6, 2025
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements

// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue)
// #Medium #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue
// #2025_02_25_Time_139_ms_(100.00%)_Space_88.84_MB_(79.31%)

class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3401_3500.s3467_transform_array_by_parity

// #Easy #Array #Sorting #Counting #2025_03_06_Time_1_ms_(100.00%)_Space_45.41_MB_(5.41%)

class Solution {
fun transformArray(nums: IntArray): IntArray {
val size = nums.size
val ans = IntArray(size)
var countEven = 0
for (i in nums.indices) {
if (nums[i] and 1 == 0) {
countEven++
}
}
for (i in countEven until size) {
ans[i] = 1
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3467\. Transform Array by Parity

Easy

You are given an integer array `nums`. Transform `nums` by performing the following operations in the **exact** order specified:

1. Replace each even number with 0.
2. Replace each odd numbers with 1.
3. Sort the modified array in **non-decreasing** order.

Return the resulting array after performing these operations.

**Example 1:**

**Input:** nums = [4,3,2,1]

**Output:** [0,0,1,1]

**Explanation:**

* Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, `nums = [0, 1, 0, 1]`.
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1]`.

**Example 2:**

**Input:** nums = [1,5,1,4,2]

**Output:** [0,0,1,1,1]

**Explanation:**

* Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, `nums = [1, 1, 1, 0, 0]`.
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1, 1]`.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g3401_3500.s3468_find_the_number_of_copy_arrays

// #Medium #Array #Math #2025_03_06_Time_3_ms_(100.00%)_Space_111.85_MB_(22.73%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun countArrays(original: IntArray, bounds: Array<IntArray>): Int {
var low = bounds[0][0]
var high = bounds[0][1]
var ans = high - low + 1
for (i in 1..<original.size) {
val diff = original[i] - original[i - 1]
low = max((low + diff), bounds[i][0])
high = min((high + diff), bounds[i][1])
ans = min(ans, (high - low + 1)).toInt()
}
return max(ans, 0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
3468\. Find the Number of Copy Arrays

Medium

You are given an array `original` of length `n` and a 2D array `bounds` of length `n x 2`, where <code>bounds[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>.

You need to find the number of **possible** arrays `copy` of length `n` such that:

1. `(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])` for `1 <= i <= n - 1`.
2. <code>u<sub>i</sub> <= copy[i] <= v<sub>i</sub></code> for `0 <= i <= n - 1`.

Return the number of such arrays.

**Example 1:**

**Input:** original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]

**Output:** 2

**Explanation:**

The possible arrays are:

* `[1, 2, 3, 4]`
* `[2, 3, 4, 5]`

**Example 2:**

**Input:** original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]

**Output:** 4

**Explanation:**

The possible arrays are:

* `[1, 2, 3, 4]`
* `[2, 3, 4, 5]`
* `[3, 4, 5, 6]`
* `[4, 5, 6, 7]`

**Example 3:**

**Input:** original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]

**Output:** 0

**Explanation:**

No array is possible.

**Constraints:**

* <code>2 <= n == original.length <= 10<sup>5</sup></code>
* <code>1 <= original[i] <= 10<sup>9</sup></code>
* `bounds.length == n`
* `bounds[i].length == 2`
* <code>1 <= bounds[i][0] <= bounds[i][1] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements

// #Medium #Array #Dynamic_Programming #2025_03_06_Time_27_ms_(100.00%)_Space_49.13_MB_(93.33%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun minCost(nums: IntArray): Int {
var nums = nums
var n = nums.size
if (n % 2 == 0) {
nums = nums.copyOf(++n)
}
val dp = IntArray(n)
var j = 1
while (j < n - 1) {
var cost1: Int = INF
var cost2: Int = INF
val max = max(nums[j], nums[j + 1])
for (i in 0..<j) {
cost1 =
min(cost1, dp[i] + max(nums[i], nums[j + 1]))
cost2 = min(cost2, dp[i] + max(nums[i], nums[j]))
dp[i] += max
}
dp[j] = cost1
dp[j + 1] = cost2
j += 2
}
var result: Int = INF
for (i in 0..<n) {
result = min(result, dp[i] + nums[i])
}
return result
}

companion object {
private const val INF = 1e9.toInt()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3469\. Find Minimum Cost to Remove Array Elements

Medium

You are given an integer array `nums`. Your task is to remove **all elements** from the array by performing one of the following operations at each step until `nums` is empty:

* Choose any two elements from the first three elements of `nums` and remove them. The cost of this operation is the **maximum** of the two elements removed.
* If fewer than three elements remain in `nums`, remove all the remaining elements in a single operation. The cost of this operation is the **maximum** of the remaining elements.

Return the **minimum** cost required to remove all the elements.

**Example 1:**

**Input:** nums = [6,2,8,4]

**Output:** 12

**Explanation:**

Initially, `nums = [6, 2, 8, 4]`.

* In the first operation, remove `nums[0] = 6` and `nums[2] = 8` with a cost of `max(6, 8) = 8`. Now, `nums = [2, 4]`.
* In the second operation, remove the remaining elements with a cost of `max(2, 4) = 4`.

The cost to remove all elements is `8 + 4 = 12`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 12.

**Example 2:**

**Input:** nums = [2,1,3,3]

**Output:** 5

**Explanation:**

Initially, `nums = [2, 1, 3, 3]`.

* In the first operation, remove `nums[0] = 2` and `nums[1] = 1` with a cost of `max(2, 1) = 2`. Now, `nums = [3, 3]`.
* In the second operation remove the remaining elements with a cost of `max(3, 3) = 3`.

The cost to remove all elements is `2 + 3 = 5`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 5.

**Constraints:**

* `1 <= nums.length <= 1000`
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
75 changes: 75 additions & 0 deletions src/main/kotlin/g3401_3500/s3470_permutations_iv/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package g3401_3500.s3470_permutations_iv

// #Hard #Array #Math #Enumeration #Combinatorics
// #2025_03_06_Time_4_ms_(96.77%)_Space_45.40_MB_(9.68%)

class Solution {
private val maxFac = 100_000_000L

fun permute(n: Int, k: Long): IntArray {
var res = IntArray(n)
var k = k - 1
val fac = LongArray(n / 2 + 1)
fac[0] = 1
for (i in 1..n / 2) {
fac[i] = fac[i - 1] * i
if (fac[i] >= maxFac) {
fac[i] = maxFac
}
}
var evenNum = n / 2
var oddNum = n - evenNum
var evens = mutableListOf<Int>()
var odds = mutableListOf<Int>()
for (i in 1..n) {
if (i % 2 == 0) {
evens.add(i)
} else {
odds.add(i)
}
}
for (i in 0..<n) {
if (i == 0) {
if (n % 2 == 0) {
val trailCombs = fac[evenNum] * fac[evenNum - 1]
val leadIdx = (k / trailCombs).toInt()
if (leadIdx + 1 > n) return IntArray(0)
res[i] = leadIdx + 1
if ((leadIdx + 1) % 2 == 0) {
evens.remove(leadIdx + 1)
} else {
odds.remove(leadIdx + 1)
}
k = k % trailCombs
} else {
val trailCombs = fac[oddNum - 1] * fac[evenNum]
val leadIdx = (k / trailCombs).toInt()
if (leadIdx >= odds.size) return IntArray(0)
val num = odds.removeAt(leadIdx)
res[i] = num
k = k % trailCombs
}
} else {
if (res[i - 1] % 2 == 0) {
val trailCombs = fac[evenNum] * fac[oddNum - 1]
val leadIdx = (k / trailCombs).toInt()
val num = odds.removeAt(leadIdx)
res[i] = num
k = k % trailCombs
} else {
val trailCombs = fac[evenNum - 1] * fac[oddNum ]
val leadIdx = (k / trailCombs).toInt()
val num = evens.removeAt(leadIdx)
res[i] = num
k = k % trailCombs
}
}
if (res[i] % 2 == 0) {
evenNum--
} else {
oddNum--
}
}
return res
}
}
63 changes: 63 additions & 0 deletions src/main/kotlin/g3401_3500/s3470_permutations_iv/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
3470\. Permutations IV

Hard

Given two integers, `n` and `k`, an **alternating permutation** is a permutation of the first `n` positive integers such that no **two** adjacent elements are both odd or both even.

Return the **k-th** **alternating permutation** sorted in _lexicographical order_. If there are fewer than `k` valid **alternating permutations**, return an empty list.

**Example 1:**

**Input:** n = 4, k = 6

**Output:** [3,4,1,2]

**Explanation:**

The lexicographically-sorted alternating permutations of `[1, 2, 3, 4]` are:

1. `[1, 2, 3, 4]`
2. `[1, 4, 3, 2]`
3. `[2, 1, 4, 3]`
4. `[2, 3, 4, 1]`
5. `[3, 2, 1, 4]`
6. `[3, 4, 1, 2]` ← 6th permutation
7. `[4, 1, 2, 3]`
8. `[4, 3, 2, 1]`

Since `k = 6`, we return `[3, 4, 1, 2]`.

**Example 2:**

**Input:** n = 3, k = 2

**Output:** [3,2,1]

**Explanation:**

The lexicographically-sorted alternating permutations of `[1, 2, 3]` are:

1. `[1, 2, 3]`
2. `[3, 2, 1]` ← 2nd permutation

Since `k = 2`, we return `[3, 2, 1]`.

**Example 3:**

**Input:** n = 2, k = 3

**Output:** []

**Explanation:**

The lexicographically-sorted alternating permutations of `[1, 2]` are:

1. `[1, 2]`
2. `[2, 1]`

There are only 2 alternating permutations, but `k = 3`, which is out of range. Thus, we return an empty list `[]`.

**Constraints:**

* `1 <= n <= 100`
* <code>1 <= k <= 10<sup>15</sup></code>
Loading