Skip to content

Commit 57e77f4

Browse files
authored
Added tasks 3467-3475
1 parent 0803691 commit 57e77f4

File tree

28 files changed

+1191
-1
lines changed

28 files changed

+1191
-1
lines changed

src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements
22

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

66
class Solution {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3401_3500.s3467_transform_array_by_parity
2+
3+
// #Easy #Array #Sorting #Counting #2025_03_06_Time_1_ms_(100.00%)_Space_45.41_MB_(5.41%)
4+
5+
class Solution {
6+
fun transformArray(nums: IntArray): IntArray {
7+
val size = nums.size
8+
val ans = IntArray(size)
9+
var countEven = 0
10+
for (i in nums.indices) {
11+
if (nums[i] and 1 == 0) {
12+
countEven++
13+
}
14+
}
15+
for (i in countEven until size) {
16+
ans[i] = 1
17+
}
18+
return ans
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3467\. Transform Array by Parity
2+
3+
Easy
4+
5+
You are given an integer array `nums`. Transform `nums` by performing the following operations in the **exact** order specified:
6+
7+
1. Replace each even number with 0.
8+
2. Replace each odd numbers with 1.
9+
3. Sort the modified array in **non-decreasing** order.
10+
11+
Return the resulting array after performing these operations.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [4,3,2,1]
16+
17+
**Output:** [0,0,1,1]
18+
19+
**Explanation:**
20+
21+
* Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, `nums = [0, 1, 0, 1]`.
22+
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1]`.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,5,1,4,2]
27+
28+
**Output:** [0,0,1,1,1]
29+
30+
**Explanation:**
31+
32+
* 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]`.
33+
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1, 1]`.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 100`
38+
* `1 <= nums[i] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3401_3500.s3468_find_the_number_of_copy_arrays
2+
3+
// #Medium #Array #Math #2025_03_06_Time_3_ms_(100.00%)_Space_111.85_MB_(22.73%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun countArrays(original: IntArray, bounds: Array<IntArray>): Int {
10+
var low = bounds[0][0]
11+
var high = bounds[0][1]
12+
var ans = high - low + 1
13+
for (i in 1..<original.size) {
14+
val diff = original[i] - original[i - 1]
15+
low = max((low + diff), bounds[i][0])
16+
high = min((high + diff), bounds[i][1])
17+
ans = min(ans, (high - low + 1)).toInt()
18+
}
19+
return max(ans, 0)
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3468\. Find the Number of Copy Arrays
2+
3+
Medium
4+
5+
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>.
6+
7+
You need to find the number of **possible** arrays `copy` of length `n` such that:
8+
9+
1. `(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])` for `1 <= i <= n - 1`.
10+
2. <code>u<sub>i</sub> <= copy[i] <= v<sub>i</sub></code> for `0 <= i <= n - 1`.
11+
12+
Return the number of such arrays.
13+
14+
**Example 1:**
15+
16+
**Input:** original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
The possible arrays are:
23+
24+
* `[1, 2, 3, 4]`
25+
* `[2, 3, 4, 5]`
26+
27+
**Example 2:**
28+
29+
**Input:** original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]
30+
31+
**Output:** 4
32+
33+
**Explanation:**
34+
35+
The possible arrays are:
36+
37+
* `[1, 2, 3, 4]`
38+
* `[2, 3, 4, 5]`
39+
* `[3, 4, 5, 6]`
40+
* `[4, 5, 6, 7]`
41+
42+
**Example 3:**
43+
44+
**Input:** original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]
45+
46+
**Output:** 0
47+
48+
**Explanation:**
49+
50+
No array is possible.
51+
52+
**Constraints:**
53+
54+
* <code>2 <= n == original.length <= 10<sup>5</sup></code>
55+
* <code>1 <= original[i] <= 10<sup>9</sup></code>
56+
* `bounds.length == n`
57+
* `bounds[i].length == 2`
58+
* <code>1 <= bounds[i][0] <= bounds[i][1] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements
2+
3+
// #Medium #Array #Dynamic_Programming #2025_03_06_Time_27_ms_(100.00%)_Space_49.13_MB_(93.33%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minCost(nums: IntArray): Int {
10+
var nums = nums
11+
var n = nums.size
12+
if (n % 2 == 0) {
13+
nums = nums.copyOf(++n)
14+
}
15+
val dp = IntArray(n)
16+
var j = 1
17+
while (j < n - 1) {
18+
var cost1: Int = INF
19+
var cost2: Int = INF
20+
val max = max(nums[j], nums[j + 1])
21+
for (i in 0..<j) {
22+
cost1 =
23+
min(cost1, dp[i] + max(nums[i], nums[j + 1]))
24+
cost2 = min(cost2, dp[i] + max(nums[i], nums[j]))
25+
dp[i] += max
26+
}
27+
dp[j] = cost1
28+
dp[j + 1] = cost2
29+
j += 2
30+
}
31+
var result: Int = INF
32+
for (i in 0..<n) {
33+
result = min(result, dp[i] + nums[i])
34+
}
35+
return result
36+
}
37+
38+
companion object {
39+
private const val INF = 1e9.toInt()
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3469\. Find Minimum Cost to Remove Array Elements
2+
3+
Medium
4+
5+
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:
6+
7+
* 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.
8+
* 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.
9+
10+
Return the **minimum** cost required to remove all the elements.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [6,2,8,4]
15+
16+
**Output:** 12
17+
18+
**Explanation:**
19+
20+
Initially, `nums = [6, 2, 8, 4]`.
21+
22+
* In the first operation, remove `nums[0] = 6` and `nums[2] = 8` with a cost of `max(6, 8) = 8`. Now, `nums = [2, 4]`.
23+
* In the second operation, remove the remaining elements with a cost of `max(2, 4) = 4`.
24+
25+
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.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [2,1,3,3]
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
Initially, `nums = [2, 1, 3, 3]`.
36+
37+
* In the first operation, remove `nums[0] = 2` and `nums[1] = 1` with a cost of `max(2, 1) = 2`. Now, `nums = [3, 3]`.
38+
* In the second operation remove the remaining elements with a cost of `max(3, 3) = 3`.
39+
40+
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.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 1000`
45+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package g3401_3500.s3470_permutations_iv
2+
3+
// #Hard #Array #Math #Enumeration #Combinatorics
4+
// #2025_03_06_Time_4_ms_(96.77%)_Space_45.40_MB_(9.68%)
5+
6+
class Solution {
7+
private val maxFac = 100_000_000L
8+
9+
fun permute(n: Int, k: Long): IntArray {
10+
var res = IntArray(n)
11+
var k = k - 1
12+
val fac = LongArray(n / 2 + 1)
13+
fac[0] = 1
14+
for (i in 1..n / 2) {
15+
fac[i] = fac[i - 1] * i
16+
if (fac[i] >= maxFac) {
17+
fac[i] = maxFac
18+
}
19+
}
20+
var evenNum = n / 2
21+
var oddNum = n - evenNum
22+
var evens = mutableListOf<Int>()
23+
var odds = mutableListOf<Int>()
24+
for (i in 1..n) {
25+
if (i % 2 == 0) {
26+
evens.add(i)
27+
} else {
28+
odds.add(i)
29+
}
30+
}
31+
for (i in 0..<n) {
32+
if (i == 0) {
33+
if (n % 2 == 0) {
34+
val trailCombs = fac[evenNum] * fac[evenNum - 1]
35+
val leadIdx = (k / trailCombs).toInt()
36+
if (leadIdx + 1 > n) return IntArray(0)
37+
res[i] = leadIdx + 1
38+
if ((leadIdx + 1) % 2 == 0) {
39+
evens.remove(leadIdx + 1)
40+
} else {
41+
odds.remove(leadIdx + 1)
42+
}
43+
k = k % trailCombs
44+
} else {
45+
val trailCombs = fac[oddNum - 1] * fac[evenNum]
46+
val leadIdx = (k / trailCombs).toInt()
47+
if (leadIdx >= odds.size) return IntArray(0)
48+
val num = odds.removeAt(leadIdx)
49+
res[i] = num
50+
k = k % trailCombs
51+
}
52+
} else {
53+
if (res[i - 1] % 2 == 0) {
54+
val trailCombs = fac[evenNum] * fac[oddNum - 1]
55+
val leadIdx = (k / trailCombs).toInt()
56+
val num = odds.removeAt(leadIdx)
57+
res[i] = num
58+
k = k % trailCombs
59+
} else {
60+
val trailCombs = fac[evenNum - 1] * fac[oddNum ]
61+
val leadIdx = (k / trailCombs).toInt()
62+
val num = evens.removeAt(leadIdx)
63+
res[i] = num
64+
k = k % trailCombs
65+
}
66+
}
67+
if (res[i] % 2 == 0) {
68+
evenNum--
69+
} else {
70+
oddNum--
71+
}
72+
}
73+
return res
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
3470\. Permutations IV
2+
3+
Hard
4+
5+
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.
6+
7+
Return the **k-th** **alternating permutation** sorted in _lexicographical order_. If there are fewer than `k` valid **alternating permutations**, return an empty list.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 4, k = 6
12+
13+
**Output:** [3,4,1,2]
14+
15+
**Explanation:**
16+
17+
The lexicographically-sorted alternating permutations of `[1, 2, 3, 4]` are:
18+
19+
1. `[1, 2, 3, 4]`
20+
2. `[1, 4, 3, 2]`
21+
3. `[2, 1, 4, 3]`
22+
4. `[2, 3, 4, 1]`
23+
5. `[3, 2, 1, 4]`
24+
6. `[3, 4, 1, 2]` ← 6th permutation
25+
7. `[4, 1, 2, 3]`
26+
8. `[4, 3, 2, 1]`
27+
28+
Since `k = 6`, we return `[3, 4, 1, 2]`.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 3, k = 2
33+
34+
**Output:** [3,2,1]
35+
36+
**Explanation:**
37+
38+
The lexicographically-sorted alternating permutations of `[1, 2, 3]` are:
39+
40+
1. `[1, 2, 3]`
41+
2. `[3, 2, 1]` ← 2nd permutation
42+
43+
Since `k = 2`, we return `[3, 2, 1]`.
44+
45+
**Example 3:**
46+
47+
**Input:** n = 2, k = 3
48+
49+
**Output:** []
50+
51+
**Explanation:**
52+
53+
The lexicographically-sorted alternating permutations of `[1, 2]` are:
54+
55+
1. `[1, 2]`
56+
2. `[2, 1]`
57+
58+
There are only 2 alternating permutations, but `k = 3`, which is out of range. Thus, we return an empty list `[]`.
59+
60+
**Constraints:**
61+
62+
* `1 <= n <= 100`
63+
* <code>1 <= k <= 10<sup>15</sup></code>

0 commit comments

Comments
 (0)