Skip to content

Commit e8f9176

Browse files
committed
Added tasks 3467-3470
1 parent 67fecce commit e8f9176

File tree

12 files changed

+525
-0
lines changed

12 files changed

+525
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3401_3500.s3467_transform_array_by_parity
2+
3+
// #Easy #2025_03_02_Time_11_ms_(100.00%)_Space_42.68_MB_(100.00%)
4+
5+
class Solution {
6+
fun transformArray(nums: IntArray): IntArray {
7+
for (i in nums.indices) {
8+
if (nums[i] % 2 == 0) {
9+
nums[i] = 0
10+
} else {
11+
nums[i] = 1
12+
}
13+
}
14+
nums.sort()
15+
return nums
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
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`
Lines changed: 21 additions & 0 deletions
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 #2025_03_02_Time_4_ms_(100.00%)_Space_112.45_MB_(100.00%)
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+
}
Lines changed: 58 additions & 0 deletions
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>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements
2+
3+
// #Medium #2025_03_02_Time_320_ms_(100.00%)_Space_71.87_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
private lateinit var dp: Array<IntArray>
10+
11+
fun minCost(nums: IntArray): Int {
12+
dp = Array<IntArray>(1001) { IntArray(1001) }
13+
dp.forEach { row -> row.fill(-1) }
14+
return solve(nums, 1, 0)
15+
}
16+
17+
private fun solve(nums: IntArray, i: Int, last: Int): Int {
18+
if (i + 1 >= nums.size) {
19+
return max(nums[last], if (i < nums.size) nums[i] else 0)
20+
}
21+
if (dp[i][last] != -1) {
22+
return dp[i][last]
23+
}
24+
var res: Int = max(nums[i], nums[i + 1]) + solve(nums, i + 2, last)
25+
res = min(
26+
res,
27+
max(nums[i], nums[last]) + solve(nums, i + 2, i + 1)
28+
)
29+
res = min(
30+
res,
31+
max(nums[i + 1], nums[last]) + solve(nums, i + 2, i)
32+
)
33+
dp[i][last] = res
34+
return res
35+
}
36+
}
Lines changed: 45 additions & 0 deletions
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>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package g3401_3500.s3470_permutations_iv
2+
3+
// #Hard #2025_03_02_Time_63_ms_(100.00%)_Space_38.70_MB_(100.00%)
4+
5+
class Solution {
6+
private fun helper(a: Int, b: Int): Long {
7+
var res: Long = 1
8+
for (i in 0..<b) {
9+
res *= (a - i).toLong()
10+
if (res > INF) {
11+
return INF
12+
}
13+
}
14+
return res
15+
}
16+
17+
private fun solve(odd: Int, even: Int, r: Int, req: Int): Long {
18+
if (r == 0) {
19+
return 1
20+
}
21+
val nOdd: Int
22+
val nEven: Int
23+
if (req == 1) {
24+
nOdd = (r + 1) / 2
25+
nEven = r / 2
26+
} else {
27+
nEven = (r + 1) / 2
28+
nOdd = r / 2
29+
}
30+
if (odd < nOdd || even < nEven) {
31+
return 0
32+
}
33+
val oddWays = helper(odd, nOdd)
34+
val evenWays = helper(even, nEven)
35+
var total = oddWays
36+
if (evenWays == 0L || total > INF / evenWays) {
37+
total = INF
38+
} else {
39+
total *= evenWays
40+
}
41+
return total
42+
}
43+
44+
fun permute(n: Int, k: Long): IntArray {
45+
var k = k
46+
val ans: MutableList<Int> = ArrayList<Int>()
47+
var first = false
48+
val used = BooleanArray(n + 1)
49+
var odd = (n + 1) / 2
50+
var even = n / 2
51+
var last = -1
52+
for (i in 1..n) {
53+
if (!used[i]) {
54+
var odd2 = odd
55+
var even2 = even
56+
val cp = i and 1
57+
val next = (if (cp == 1) 0 else 1)
58+
if (cp == 1) {
59+
odd2--
60+
} else {
61+
even2--
62+
}
63+
val r = n - 1
64+
val cnt = solve(odd2, even2, r, next)
65+
if (k > cnt) {
66+
k -= cnt
67+
} else {
68+
ans.add(i)
69+
used[i] = true
70+
odd = odd2
71+
even = even2
72+
last = cp
73+
first = true
74+
break
75+
}
76+
}
77+
}
78+
if (!first) {
79+
return IntArray(0)
80+
}
81+
for (z in 1..<n) {
82+
for (j in 1..n) {
83+
if (!used[j] && ((j and 1) != last)) {
84+
var odd2 = odd
85+
var even2 = even
86+
val cp = j and 1
87+
if (cp == 1) {
88+
odd2--
89+
} else {
90+
even2--
91+
}
92+
val r = n - (z + 1)
93+
val next = (if (cp == 1) 0 else 1)
94+
val cnt2 = solve(odd2, even2, r, next)
95+
if (k > cnt2) {
96+
k -= cnt2
97+
} else {
98+
ans.add(j)
99+
used[j] = true
100+
odd = odd2
101+
even = even2
102+
last = cp
103+
break
104+
}
105+
}
106+
}
107+
}
108+
return ans.stream().mapToInt { i: Int? -> i!! }.toArray()
109+
}
110+
111+
companion object {
112+
private const val INF = 1000000000000000000L
113+
}
114+
}

0 commit comments

Comments
 (0)