Skip to content

Commit c0158b7

Browse files
authored
Added tasks 3477-3480
1 parent d7ee11b commit c0158b7

File tree

12 files changed

+508
-0
lines changed

12 files changed

+508
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3401_3500.s3477_fruits_into_baskets_ii;
2+
3+
// #Easy #Array #Binary_Search #Simulation #Segment_Tree
4+
// #2025_03_10_Time_1_ms_(100.00%)_Space_44.78_MB_(36.06%)
5+
6+
public class Solution {
7+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
8+
int n = fruits.length;
9+
int currfruits;
10+
int count = 0;
11+
for (int i = 0; i < n; i++) {
12+
currfruits = fruits[i];
13+
for (int j = 0; j < n; j++) {
14+
if (baskets[j] >= currfruits) {
15+
count++;
16+
baskets[j] = 0;
17+
break;
18+
}
19+
}
20+
}
21+
return n - count;
22+
}
23+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g3401_3500.s3478_choose_k_elements_with_maximum_sum;
2+
3+
// #Medium #Array #Sorting #Heap_Priority_Queue
4+
// #2025_03_10_Time_105_ms_(98.60%)_Space_69.10_MB_(28.75%)
5+
6+
import java.util.Arrays;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public long[] findMaxSum(int[] nums1, int[] nums2, int k) {
11+
int n = nums1.length;
12+
long[] ans = new long[n];
13+
Point[] ps = new Point[n];
14+
for (int i = 0; i < n; i++) {
15+
ps[i] = new Point(nums1[i], nums2[i], i);
16+
}
17+
Arrays.sort(ps, (p1, p2) -> Integer.compare(p1.x, p2.x));
18+
PriorityQueue<Integer> pq = new PriorityQueue<>();
19+
long s = 0;
20+
int i = 0;
21+
while (i < n) {
22+
int j = i;
23+
while (j < n && ps[j].x == ps[i].x) {
24+
ans[ps[j].i] = s;
25+
j++;
26+
}
27+
for (int p = i; p < j; p++) {
28+
int cur = ps[p].y;
29+
if (pq.size() < k) {
30+
pq.offer(cur);
31+
s += cur;
32+
} else if (cur > pq.peek()) {
33+
s -= pq.poll();
34+
pq.offer(cur);
35+
s += cur;
36+
}
37+
}
38+
i = j;
39+
}
40+
return ans;
41+
}
42+
43+
private static class Point {
44+
int x;
45+
int y;
46+
int i;
47+
48+
Point(int x, int y, int i) {
49+
this.x = x;
50+
this.y = y;
51+
this.i = i;
52+
}
53+
}
54+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3401_3500.s3479_fruits_into_baskets_iii;
2+
3+
// #Medium #Array #Binary_Search #Ordered_Set #Segment_Tree
4+
// #2025_03_10_Time_38_ms_(97.76%)_Space_67.52_MB_(38.48%)
5+
6+
public class Solution {
7+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
8+
int n = baskets.length;
9+
int size = 1;
10+
while (size < n) {
11+
size <<= 1;
12+
}
13+
int[] seg = new int[2 * size];
14+
for (int i = 0; i < n; i++) {
15+
seg[size + i] = baskets[i];
16+
}
17+
for (int i = n; i < size; i++) {
18+
seg[size + i] = 0;
19+
}
20+
for (int i = size - 1; i > 0; i--) {
21+
seg[i] = Math.max(seg[i << 1], seg[i << 1 | 1]);
22+
}
23+
int ans = 0;
24+
for (int f : fruits) {
25+
if (seg[1] < f) {
26+
ans++;
27+
continue;
28+
}
29+
int idx = 1;
30+
while (idx < size) {
31+
if (seg[idx << 1] >= f) {
32+
idx = idx << 1;
33+
} else {
34+
idx = idx << 1 | 1;
35+
}
36+
}
37+
update(seg, idx - size, 0, size);
38+
}
39+
return ans;
40+
}
41+
42+
private void update(int[] seg, int pos, int val, int size) {
43+
int i = pos + size;
44+
seg[i] = val;
45+
for (i /= 2; i > 0; i /= 2) {
46+
seg[i] = Math.max(seg[i << 1], seg[i << 1 | 1]);
47+
}
48+
}
49+
}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package g3401_3500.s3480_maximize_subarrays_after_removing_one_conflicting_pair;
2+
3+
// #Hard #Array #Prefix_Sum #Enumeration #Segment_Tree
4+
// #2025_03_10_Time_20_ms_(98.86%)_Space_141.78_MB_(52.27%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public long maxSubarrays(int n, int[][] conflictingPairs) {
10+
long totalSubarrays = (long) n * (n + 1) / 2;
11+
int[] h = new int[n + 1];
12+
int[] d2 = new int[n + 1];
13+
Arrays.fill(h, n + 1);
14+
Arrays.fill(d2, n + 1);
15+
for (int[] pair : conflictingPairs) {
16+
int a = pair[0];
17+
int b = pair[1];
18+
if (a > b) {
19+
int temp = a;
20+
a = b;
21+
b = temp;
22+
}
23+
if (b < h[a]) {
24+
d2[a] = h[a];
25+
h[a] = b;
26+
} else if (b < d2[a]) {
27+
d2[a] = b;
28+
}
29+
}
30+
int[] f = new int[n + 2];
31+
f[n + 1] = n + 1;
32+
f[n] = h[n];
33+
for (int i = n - 1; i >= 1; i--) {
34+
f[i] = Math.min(h[i], f[i + 1]);
35+
}
36+
// forbiddenCount(x) returns (n - x + 1) if x <= n, else 0.
37+
// This is the number of forbidden subarrays starting at some i when f[i] = x.
38+
long originalUnion = 0;
39+
for (int i = 1; i <= n; i++) {
40+
if (f[i] <= n) {
41+
originalUnion += (n - f[i] + 1);
42+
}
43+
}
44+
long originalValid = totalSubarrays - originalUnion;
45+
long best = originalValid;
46+
// For each index j (1 <= j <= n) where a candidate conflicting pair exists,
47+
// simulate removal of the pair that gave h[j] (if any).
48+
// (If there is no candidate pair at j, h[j] remains n+1.)
49+
for (int j = 1; j <= n; j++) {
50+
// no conflicting pair at index j
51+
if (h[j] == n + 1) {
52+
continue;
53+
}
54+
// Simulate removal: new candidate at j becomes d2[j]
55+
int newCandidate = (j < n) ? Math.min(d2[j], f[j + 1]) : d2[j];
56+
// We'll recompute the new f values for indices 1..j.
57+
// Let newF[i] denote the updated value.
58+
// For i > j, newF[i] remains as original f[i].
59+
// For i = j, newF[j] = min( newCandidate, f[j+1] ) (which is newCandidate by
60+
// definition).
61+
int newFj = newCandidate;
62+
// forbiddenCount(x) is defined as (n - x + 1) if x<= n, else 0.
63+
long delta = forbiddenCount(newFj, n) - forbiddenCount(f[j], n);
64+
int cur = newFj;
65+
// Now update backwards for i = j-1 down to 1.
66+
for (int i = j - 1; i >= 1; i--) {
67+
int newVal = Math.min(h[i], cur);
68+
// no further change for i' <= i
69+
if (newVal == f[i]) {
70+
break;
71+
}
72+
delta += forbiddenCount(newVal, n) - forbiddenCount(f[i], n);
73+
cur = newVal;
74+
}
75+
long newUnion = originalUnion + delta;
76+
long newValid = totalSubarrays - newUnion;
77+
best = Math.max(best, newValid);
78+
}
79+
return best;
80+
}
81+
82+
private long forbiddenCount(int x, int n) {
83+
return x <= n ? (n - x + 1) : 0;
84+
}
85+
}

0 commit comments

Comments
 (0)