Skip to content

Commit 1c2cc20

Browse files
committed
Added tasks 3477-3480
1 parent 103c79b commit 1c2cc20

File tree

12 files changed

+419
-0
lines changed

12 files changed

+419
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3401_3500.s3477_fruits_into_baskets_ii;
2+
3+
// #Easy #2025_03_09_Time_2_ms_(100.00%)_Space_45.04_MB_(100.00%)
4+
5+
public class Solution {
6+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
7+
int n = fruits.length;
8+
int ct = 0;
9+
boolean[] used = new boolean[n];
10+
for (int fruit : fruits) {
11+
boolean flag = true;
12+
for (int i = 0; i < n; i++) {
13+
if (fruit <= baskets[i] && !used[i]) {
14+
used[i] = true;
15+
flag = false;
16+
break;
17+
}
18+
}
19+
if (flag) {
20+
ct++;
21+
}
22+
}
23+
return ct;
24+
}
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3477\. Fruits Into Baskets II
2+
3+
Easy
4+
5+
You are given two arrays of integers, `fruits` and `baskets`, each of length `n`, where `fruits[i]` represents the **quantity** of the <code>i<sup>th</sup></code> type of fruit, and `baskets[j]` represents the **capacity** of the <code>j<sup>th</sup></code> basket.
6+
7+
From left to right, place the fruits according to these rules:
8+
9+
* Each fruit type must be placed in the **leftmost available basket** with a capacity **greater than or equal** to the quantity of that fruit type.
10+
* Each basket can hold **only one** type of fruit.
11+
* If a fruit type **cannot be placed** in any basket, it remains **unplaced**.
12+
13+
Return the number of fruit types that remain unplaced after all possible allocations are made.
14+
15+
**Example 1:**
16+
17+
**Input:** fruits = [4,2,5], baskets = [3,5,4]
18+
19+
**Output:** 1
20+
21+
**Explanation:**
22+
23+
* `fruits[0] = 4` is placed in `baskets[1] = 5`.
24+
* `fruits[1] = 2` is placed in `baskets[0] = 3`.
25+
* `fruits[2] = 5` cannot be placed in `baskets[2] = 4`.
26+
27+
Since one fruit type remains unplaced, we return 1.
28+
29+
**Example 2:**
30+
31+
**Input:** fruits = [3,6,1], baskets = [6,4,7]
32+
33+
**Output:** 0
34+
35+
**Explanation:**
36+
37+
* `fruits[0] = 3` is placed in `baskets[0] = 6`.
38+
* `fruits[1] = 6` cannot be placed in `baskets[1] = 4` (insufficient capacity) but can be placed in the next available basket, `baskets[2] = 7`.
39+
* `fruits[2] = 1` is placed in `baskets[1] = 4`.
40+
41+
Since all fruits are successfully placed, we return 0.
42+
43+
**Constraints:**
44+
45+
* `n == fruits.length == baskets.length`
46+
* `1 <= n <= 100`
47+
* `1 <= fruits[i], baskets[i] <= 1000`
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3401_3500.s3478_choose_k_elements_with_maximum_sum;
2+
3+
// #Medium #2025_03_09_Time_156_ms_(100.00%)_Space_63.92_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
public long[] findMaxSum(int[] nums1, int[] nums2, int k) {
10+
int n = nums1.length;
11+
int[][] arr = new int[n][3];
12+
for (int i = 0; i < n; i++) {
13+
arr[i][0] = nums1[i];
14+
arr[i][1] = nums2[i];
15+
arr[i][2] = i;
16+
}
17+
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
18+
long[] ans = new long[n];
19+
PriorityQueue<Integer> pq = new PriorityQueue<>();
20+
long sum = 0;
21+
for (int i = 0; i < n; i++) {
22+
if (i > 0 && arr[i - 1][0] == arr[i][0]) {
23+
ans[arr[i][2]] = ans[arr[i - 1][2]];
24+
} else {
25+
ans[arr[i][2]] = sum;
26+
}
27+
pq.add(arr[i][1]);
28+
sum += arr[i][1];
29+
if (pq.size() > k) {
30+
sum -= pq.remove();
31+
}
32+
}
33+
return ans;
34+
}
35+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3478\. Choose K Elements With Maximum Sum
2+
3+
Medium
4+
5+
You are given two integer arrays, `nums1` and `nums2`, both of length `n`, along with a positive integer `k`.
6+
7+
For each index `i` from `0` to `n - 1`, perform the following:
8+
9+
* Find **all** indices `j` where `nums1[j]` is less than `nums1[i]`.
10+
* Choose **at most** `k` values of `nums2[j]` at these indices to **maximize** the total sum.
11+
12+
Return an array `answer` of size `n`, where `answer[i]` represents the result for the corresponding index `i`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2
17+
18+
**Output:** [80,30,0,80,50]
19+
20+
**Explanation:**
21+
22+
* For `i = 0`: Select the 2 largest values from `nums2` at indices `[1, 2, 4]` where `nums1[j] < nums1[0]`, resulting in `50 + 30 = 80`.
23+
* For `i = 1`: Select the 2 largest values from `nums2` at index `[2]` where `nums1[j] < nums1[1]`, resulting in 30.
24+
* For `i = 2`: No indices satisfy `nums1[j] < nums1[2]`, resulting in 0.
25+
* For `i = 3`: Select the 2 largest values from `nums2` at indices `[0, 1, 2, 4]` where `nums1[j] < nums1[3]`, resulting in `50 + 30 = 80`.
26+
* For `i = 4`: Select the 2 largest values from `nums2` at indices `[1, 2]` where `nums1[j] < nums1[4]`, resulting in `30 + 20 = 50`.
27+
28+
**Example 2:**
29+
30+
**Input:** nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1
31+
32+
**Output:** [0,0,0,0]
33+
34+
**Explanation:**
35+
36+
Since all elements in `nums1` are equal, no indices satisfy the condition `nums1[j] < nums1[i]` for any `i`, resulting in 0 for all positions.
37+
38+
**Constraints:**
39+
40+
* `n == nums1.length == nums2.length`
41+
* <code>1 <= n <= 10<sup>5</sup></code>
42+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
43+
* `1 <= k <= n`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3401_3500.s3479_fruits_into_baskets_iii;
2+
3+
// #Medium #2025_03_09_Time_629_ms_(100.00%)_Space_67.19_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
10+
int unp = 0;
11+
int max = 0;
12+
List<Integer> list = new ArrayList<>();
13+
for (int basket : baskets) {
14+
list.add(basket);
15+
max = Math.max(max, basket);
16+
}
17+
for (int fruit : fruits) {
18+
if (fruit > max) {
19+
unp++;
20+
continue;
21+
}
22+
boolean placed = false;
23+
for (int j = 0; j < list.size(); j++) {
24+
if (list.get(j) >= fruit) {
25+
list.remove(j);
26+
placed = true;
27+
break;
28+
}
29+
}
30+
if (!placed) {
31+
unp++;
32+
}
33+
}
34+
return unp;
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3479\. Fruits Into Baskets III
2+
3+
Medium
4+
5+
You are given two arrays of integers, `fruits` and `baskets`, each of length `n`, where `fruits[i]` represents the **quantity** of the <code>i<sup>th</sup></code> type of fruit, and `baskets[j]` represents the **capacity** of the <code>j<sup>th</sup></code> basket.
6+
7+
From left to right, place the fruits according to these rules:
8+
9+
* Each fruit type must be placed in the **leftmost available basket** with a capacity **greater than or equal** to the quantity of that fruit type.
10+
* Each basket can hold **only one** type of fruit.
11+
* If a fruit type **cannot be placed** in any basket, it remains **unplaced**.
12+
13+
Return the number of fruit types that remain unplaced after all possible allocations are made.
14+
15+
**Example 1:**
16+
17+
**Input:** fruits = [4,2,5], baskets = [3,5,4]
18+
19+
**Output:** 1
20+
21+
**Explanation:**
22+
23+
* `fruits[0] = 4` is placed in `baskets[1] = 5`.
24+
* `fruits[1] = 2` is placed in `baskets[0] = 3`.
25+
* `fruits[2] = 5` cannot be placed in `baskets[2] = 4`.
26+
27+
Since one fruit type remains unplaced, we return 1.
28+
29+
**Example 2:**
30+
31+
**Input:** fruits = [3,6,1], baskets = [6,4,7]
32+
33+
**Output:** 0
34+
35+
**Explanation:**
36+
37+
* `fruits[0] = 3` is placed in `baskets[0] = 6`.
38+
* `fruits[1] = 6` cannot be placed in `baskets[1] = 4` (insufficient capacity) but can be placed in the next available basket, `baskets[2] = 7`.
39+
* `fruits[2] = 1` is placed in `baskets[1] = 4`.
40+
41+
Since all fruits are successfully placed, we return 0.
42+
43+
**Constraints:**
44+
45+
* `n == fruits.length == baskets.length`
46+
* <code>1 <= n <= 10<sup>5</sup></code>
47+
* <code>1 <= fruits[i], baskets[i] <= 10<sup>9</sup></code>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g3401_3500.s3480_maximize_subarrays_after_removing_one_conflicting_pair;
2+
3+
// #Hard #2025_03_09_Time_581_ms_(_%)_Space_163.11_MB_(_%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.TreeMap;
11+
12+
public class Solution {
13+
public long maxSubarrays(int n, int[][] cp) {
14+
for (int[] pair : cp) {
15+
if (pair[0] > pair[1]) {
16+
int temp = pair[0];
17+
pair[0] = pair[1];
18+
pair[1] = temp;
19+
}
20+
}
21+
Arrays.sort(cp, Comparator.comparingInt(a -> a[0]));
22+
long[] contributions = new long[cp.length];
23+
long totalPossible = (long) n * (n + 1) / 2;
24+
TreeMap<Integer, List<Integer>> endPointMap = new TreeMap<>();
25+
int currentIndex = cp.length - 1;
26+
for (int start = n; start >= 1; start--) {
27+
while (currentIndex >= 0 && cp[currentIndex][0] >= start) {
28+
int end = cp[currentIndex][1];
29+
endPointMap.computeIfAbsent(end, k -> new ArrayList<>()).add(currentIndex);
30+
currentIndex--;
31+
}
32+
if (endPointMap.isEmpty()) {
33+
continue;
34+
}
35+
Map.Entry<Integer, List<Integer>> firstEntry = endPointMap.firstEntry();
36+
int smallestEnd = firstEntry.getKey();
37+
int pairIndex = firstEntry.getValue().get(0);
38+
if (firstEntry.getValue().size() == 1) {
39+
endPointMap.remove(smallestEnd);
40+
} else {
41+
firstEntry.getValue().remove(0);
42+
}
43+
long covered = n - smallestEnd + 1;
44+
totalPossible -= covered;
45+
if (endPointMap.isEmpty()) {
46+
contributions[pairIndex] += covered;
47+
} else {
48+
int nextEnd = endPointMap.firstKey();
49+
contributions[pairIndex] += nextEnd - smallestEnd;
50+
}
51+
endPointMap.computeIfAbsent(smallestEnd, k -> new ArrayList<>()).add(pairIndex);
52+
}
53+
long result = totalPossible;
54+
for (long contribution : contributions) {
55+
result = Math.max(result, totalPossible + contribution);
56+
}
57+
return result;
58+
}
59+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3480\. Maximize Subarrays After Removing One Conflicting Pair
2+
3+
Hard
4+
5+
You are given an integer `n` which represents an array `nums` containing the numbers from 1 to `n` in order. Additionally, you are given a 2D array `conflictingPairs`, where `conflictingPairs[i] = [a, b]` indicates that `a` and `b` form a conflicting pair.
6+
7+
Remove **exactly** one element from `conflictingPairs`. Afterward, count the number of non-empty subarrays of `nums` which do not contain both `a` and `b` for any remaining conflicting pair `[a, b]`.
8+
9+
Return the **maximum** number of subarrays possible after removing **exactly** one conflicting pair.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 4, conflictingPairs = [[2,3],[1,4]]
14+
15+
**Output:** 9
16+
17+
**Explanation:**
18+
19+
* Remove `[2, 3]` from `conflictingPairs`. Now, `conflictingPairs = [[1, 4]]`.
20+
* There are 9 subarrays in `nums` where `[1, 4]` do not appear together. They are `[1]`, `[2]`, `[3]`, `[4]`, `[1, 2]`, `[2, 3]`, `[3, 4]`, `[1, 2, 3]` and `[2, 3, 4]`.
21+
* The maximum number of subarrays we can achieve after removing one element from `conflictingPairs` is 9.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]
26+
27+
**Output:** 12
28+
29+
**Explanation:**
30+
31+
* Remove `[1, 2]` from `conflictingPairs`. Now, `conflictingPairs = [[2, 5], [3, 5]]`.
32+
* There are 12 subarrays in `nums` where `[2, 5]` and `[3, 5]` do not appear together.
33+
* The maximum number of subarrays we can achieve after removing one element from `conflictingPairs` is 12.
34+
35+
**Constraints:**
36+
37+
* <code>2 <= n <= 10<sup>5</sup></code>
38+
* `1 <= conflictingPairs.length <= 2 * n`
39+
* `conflictingPairs[i].length == 2`
40+
* `1 <= conflictingPairs[i][j] <= n`
41+
* `conflictingPairs[i][0] != conflictingPairs[i][1]`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3401_3500.s3477_fruits_into_baskets_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void numOfUnplacedFruits() {
11+
assertThat(
12+
new Solution().numOfUnplacedFruits(new int[] {4, 2, 5}, new int[] {3, 5, 4}),
13+
equalTo(1));
14+
}
15+
16+
@Test
17+
void numOfUnplacedFruits2() {
18+
assertThat(
19+
new Solution().numOfUnplacedFruits(new int[] {3, 6, 1}, new int[] {6, 4, 7}),
20+
equalTo(0));
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3401_3500.s3478_choose_k_elements_with_maximum_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findMaxSum() {
11+
assertThat(
12+
new Solution()
13+
.findMaxSum(new int[] {4, 2, 1, 5, 3}, new int[] {10, 20, 30, 40, 50}, 2),
14+
equalTo(new long[] {80L, 30L, 0L, 80L, 50L}));
15+
}
16+
17+
@Test
18+
void findMaxSum2() {
19+
assertThat(
20+
new Solution().findMaxSum(new int[] {2, 2, 2, 2}, new int[] {3, 1, 2, 3}, 1),
21+
equalTo(new long[] {0L, 0L, 0L, 0L}));
22+
}
23+
}

0 commit comments

Comments
 (0)