Skip to content

Commit 3e30f3f

Browse files
committed
Added tasks 3467-3470
1 parent f2de1cc commit 3e30f3f

File tree

12 files changed

+517
-0
lines changed

12 files changed

+517
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3401_3500.s3467_transform_array_by_parity;
2+
3+
// #Easy #2025_03_02_Time_2_ms_(100.00%)_Space_45.06_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] transformArray(int[] nums) {
9+
for (int i = 0; i < nums.length; i++) {
10+
if (nums[i] % 2 == 0) {
11+
nums[i] = 0;
12+
} else {
13+
nums[i] = 1;
14+
}
15+
}
16+
Arrays.sort(nums);
17+
return nums;
18+
}
19+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3401_3500.s3468_find_the_number_of_copy_arrays;
2+
3+
// #Medium #2025_03_02_Time_2_ms_(100.00%)_Space_97.78_MB_(100.00%)
4+
5+
public class Solution {
6+
int countArrays(int[] original, int[][] bounds) {
7+
int low = bounds[0][0];
8+
int high = bounds[0][1];
9+
int ans = high - low + 1;
10+
for (int i = 1; i < original.length; ++i) {
11+
int diff = original[i] - original[i - 1];
12+
low = Math.max(low + diff, bounds[i][0]);
13+
high = Math.min(high + diff, bounds[i][1]);
14+
ans = Math.min(ans, high - low + 1);
15+
}
16+
return Math.max(ans, 0);
17+
}
18+
}
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements;
2+
3+
// #Medium #2025_03_02_Time_540_ms_(100.00%)_Space_57.03_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private int[][] dp;
9+
10+
public int minCost(int[] nums) {
11+
dp = new int[1001][1001];
12+
Arrays.stream(dp).forEach(row -> Arrays.fill(row, -1));
13+
return solve(nums, 1, 0);
14+
}
15+
16+
private int solve(int[] nums, int i, int last) {
17+
if (i + 1 >= nums.length) {
18+
return Math.max(nums[last], (i < nums.length ? nums[i] : 0));
19+
}
20+
if (dp[i][last] != -1) {
21+
return dp[i][last];
22+
}
23+
int res = Math.max(nums[i], nums[i + 1]) + solve(nums, i + 2, last);
24+
res = Math.min(res, Math.max(nums[i], nums[last]) + solve(nums, i + 2, i + 1));
25+
res = Math.min(res, Math.max(nums[i + 1], nums[last]) + solve(nums, i + 2, i));
26+
return dp[i][last] = res;
27+
}
28+
}
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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package g3401_3500.s3470_permutations_iv;
2+
3+
// #Hard #2025_03_02_Time_12_ms_(100.00%)_Space_45.94_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
// Define a large constant value to cap calculations and prevent overflow
11+
private static final long CAP = 1000000000000001L;
12+
// 3D DP array to store precomputed results for dynamic programming
13+
private static final long[][][] DP = new long[105][105][3];
14+
15+
// Initialize DP array with -1 (indicating uncomputed states)
16+
static {
17+
for (long[][] longs : DP) {
18+
for (long[] aLong : longs) {
19+
Arrays.fill(aLong, -1);
20+
}
21+
}
22+
}
23+
24+
// Recursive function to count alternating permutations
25+
private long rec(int o, int e, int req) {
26+
if (o == 0 && e == 0) {
27+
return 1;
28+
}
29+
if (DP[o][e][req] != -1) {
30+
return DP[o][e][req];
31+
}
32+
long count = 0;
33+
if (req == 2) {
34+
if (o > 0) {
35+
count = addCapped(count, multiplyCapped(o, rec(o - 1, e, 1)));
36+
}
37+
if (e > 0) {
38+
count = addCapped(count, multiplyCapped(e, rec(o, e - 1, 0)));
39+
}
40+
} else if (req == 0) {
41+
if (o > 0) {
42+
count = multiplyCapped(o, rec(o - 1, e, 1));
43+
}
44+
} else {
45+
if (e > 0) {
46+
count = multiplyCapped(e, rec(o, e - 1, 0));
47+
}
48+
}
49+
DP[o][e][req] = count;
50+
return count;
51+
}
52+
53+
// Helper function to prevent overflow when multiplying
54+
private long multiplyCapped(long a, long b) {
55+
if (b == 0) {
56+
return 0;
57+
}
58+
if (a >= CAP || b >= CAP || a > CAP / b) {
59+
return CAP;
60+
}
61+
return a * b;
62+
}
63+
64+
// Helper function to prevent overflow when adding
65+
private long addCapped(long a, long b) {
66+
long res = a + b;
67+
return Math.min(res, CAP);
68+
}
69+
70+
public int[] permute(int n, long k) {
71+
// Separate odd and even numbers from 1 to n
72+
List<Integer> odds = new ArrayList<>();
73+
List<Integer> evens = new ArrayList<>();
74+
for (int x = 1; x <= n; x++) {
75+
if ((x & 1) == 1) {
76+
odds.add(x);
77+
} else {
78+
evens.add(x);
79+
}
80+
}
81+
// Count the number of odd and even elements
82+
int oCount = odds.size();
83+
int eCount = evens.size();
84+
long ansTotal = rec(oCount, eCount, 2);
85+
if (k > ansTotal) {
86+
return new int[0];
87+
}
88+
List<Integer> result = new ArrayList<>();
89+
int req = 2;
90+
while (oCount + eCount > 0) {
91+
List<Integer> candidates = new ArrayList<>();
92+
if (req == 2) {
93+
int i = 0;
94+
int j = 0;
95+
while (i < odds.size() || j < evens.size()) {
96+
if (j >= evens.size() || (i < odds.size() && odds.get(i) < evens.get(j))) {
97+
candidates.add(odds.get(i++));
98+
} else {
99+
candidates.add(evens.get(j++));
100+
}
101+
}
102+
} else if (req == 0) {
103+
candidates.addAll(odds);
104+
} else {
105+
candidates.addAll(evens);
106+
}
107+
boolean found = false;
108+
for (int num : candidates) {
109+
int candidateParity = (num % 2 == 1) ? 0 : 1;
110+
if (req != 2 && candidateParity != req) {
111+
continue;
112+
}
113+
long ways;
114+
if (candidateParity == 0) {
115+
ways = rec(oCount - 1, eCount, 1);
116+
} else {
117+
ways = rec(oCount, eCount - 1, 0);
118+
}
119+
if (ways >= k) {
120+
result.add(num);
121+
if (candidateParity == 0) {
122+
odds.remove(Integer.valueOf(num));
123+
oCount--;
124+
req = 1;
125+
} else {
126+
evens.remove(Integer.valueOf(num));
127+
eCount--;
128+
req = 0;
129+
}
130+
found = true;
131+
break;
132+
} else {
133+
k -= ways;
134+
}
135+
}
136+
if (!found) {
137+
return new int[0];
138+
}
139+
}
140+
// Convert result list to array and return
141+
int[] ans = new int[result.size()];
142+
for (int i = 0; i < result.size(); i++) {
143+
ans[i] = result.get(i);
144+
}
145+
return ans;
146+
}
147+
}

0 commit comments

Comments
 (0)