Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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_62_ms_(99.82%)_Space_78.09_MB_(20.19%)

import java.util.Arrays;

public class Solution {
public long maxSum(int[][] grid, int[] limits, int k) {
int l = 0;
for (int i = 0; i < limits.length; i++) {
l += limits[i];
for (int limit : limits) {
l += limit;
}
int[] dp = new int[l];
int a = 0;
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.11_MB_(42.10%)

public class Solution {
public int[] transformArray(int[] nums) {
int size = nums.length;
int[] ans = new int[size];
int countEven = 0;
for (int num : nums) {
if ((num & 1) == 0) {
countEven++;
}
}
for (int i = countEven; i < size; i++) {
ans[i] = 1;
}
return ans;
}
}
38 changes: 38 additions & 0 deletions src/main/java/g3401_3500/s3467_transform_array_by_parity/readme.md
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,18 @@
package g3401_3500.s3468_find_the_number_of_copy_arrays;

// #Medium #Array #Math #2025_03_02_Time_2_ms_(100.00%)_Space_97.78_MB_(100.00%)

public class Solution {
public int countArrays(int[] original, int[][] bounds) {
int low = bounds[0][0];
int high = bounds[0][1];
int ans = high - low + 1;
for (int i = 1; i < original.length; ++i) {
int diff = original[i] - original[i - 1];
low = Math.max(low + diff, bounds[i][0]);
high = Math.min(high + diff, bounds[i][1]);
ans = Math.min(ans, high - low + 1);
}
return Math.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,34 @@
package g3401_3500.s3469_find_minimum_cost_to_remove_array_elements;

// #Medium #Array #Dynamic_Programming #2025_03_06_Time_12_ms_(100.00%)_Space_45.73_MB_(95.77%)

import java.util.Arrays;

public class Solution {
private static final int INF = (int) 1e9;

public int minCost(int[] nums) {
int n = nums.length;
if (n % 2 == 0) {
nums = Arrays.copyOf(nums, ++n);
}
int[] dp = new int[n];
for (int j = 1; j < n - 1; j += 2) {
int cost1 = INF;
int cost2 = INF;
int max = Math.max(nums[j], nums[j + 1]);
for (int i = 0; i < j; ++i) {
cost1 = Math.min(cost1, dp[i] + Math.max(nums[i], nums[j + 1]));
cost2 = Math.min(cost2, dp[i] + Math.max(nums[i], nums[j]));
dp[i] += max;
}
dp[j] = cost1;
dp[j + 1] = cost2;
}
int result = INF;
for (int i = 0; i < n; ++i) {
result = Math.min(result, dp[i] + nums[i]);
}
return result;
}
}
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>
116 changes: 116 additions & 0 deletions src/main/java/g3401_3500/s3470_permutations_iv/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package g3401_3500.s3470_permutations_iv;

// #Hard #Array #Math #Enumeration #Combinatorics
// #2025_03_06_Time_11_ms_(59.56%)_Space_45.24_MB_(58.67%)

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("java:S6541")
public class Solution {
private static final long INF = 1_000_000_000_000_000_000L;

private long helper(int a, int b) {
long res = 1;
for (int i = 0; i < b; i++) {
res *= a - i;
if (res > INF) {
return INF;
}
}
return res;
}

private long solve(int odd, int even, int r, int req) {
if (r == 0) {
return 1;
}
int nOdd;
int nEven;
if (req == 1) {
nOdd = (r + 1) / 2;
nEven = r / 2;
} else {
nEven = (r + 1) / 2;
nOdd = r / 2;
}
if (odd < nOdd || even < nEven) {
return 0;
}
long oddWays = helper(odd, nOdd);
long evenWays = helper(even, nEven);
long total = oddWays;
if (evenWays == 0 || total > INF / evenWays) {
total = INF;
} else {
total *= evenWays;
}
return total;
}

public int[] permute(int n, long k) {
List<Integer> ans = new ArrayList<>();
boolean first = false;
boolean[] used = new boolean[n + 1];
int odd = (n + 1) / 2;
int even = n / 2;
int last = -1;
for (int i = 1; i <= n; i++) {
if (!used[i]) {
int odd2 = odd;
int even2 = even;
int cp = i & 1;
int next = (cp == 1 ? 0 : 1);
if (cp == 1) {
odd2--;
} else {
even2--;
}
int r = n - 1;
long cnt = solve(odd2, even2, r, next);
if (k > cnt) {
k -= cnt;
} else {
ans.add(i);
used[i] = true;
odd = odd2;
even = even2;
last = cp;
first = true;
break;
}
}
}
if (!first) {
return new int[0];
}
for (int z = 1; z < n; z++) {
for (int j = 1; j <= n; j++) {
if (!used[j] && ((j & 1) != last)) {
int odd2 = odd;
int even2 = even;
int cp = j & 1;
if (cp == 1) {
odd2--;
} else {
even2--;
}
int r = n - (z + 1);
int next = (cp == 1 ? 0 : 1);
long cnt2 = solve(odd2, even2, r, next);
if (k > cnt2) {
k -= cnt2;
} else {
ans.add(j);
used[j] = true;
odd = odd2;
even = even2;
last = cp;
break;
}
}
}
}
return ans.stream().mapToInt(i -> i).toArray();
}
}
Loading