Skip to content

Commit 1451493

Browse files
committed
Added tasks 3477-3480
1 parent 44ccc1c commit 1451493

File tree

5 files changed

+405
-0
lines changed
  • src/main/java/g3401_3500
    • s3477_fruits_into_baskets_ii
    • s3478_choose_k_elements_with_maximum_sum
    • s3479_fruits_into_baskets_iii
    • s3480_maximize_subarrays_after_removing_one_conflicting_pair

5 files changed

+405
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3480 |[Maximize Subarrays After Removing One Conflicting Pair](src/main/java/g3401_3500/s3480_maximize_subarrays_after_removing_one_conflicting_pair)| Hard | Array, Prefix_Sum, Enumeration, Segment_Tree | 20 | 98.86
2092+
| 3479 |[Fruits Into Baskets III](src/main/java/g3401_3500/s3479_fruits_into_baskets_iii)| Medium | Array, Binary_Search, Ordered_Set, Segment_Tree | 38 | 97.76
2093+
| 3478 |[Choose K Elements With Maximum Sum](src/main/java/g3401_3500/s3478_choose_k_elements_with_maximum_sum)| Medium | Array, Sorting, Heap_Priority_Queue | 105 | 98.60
2094+
| 3477 |[Fruits Into Baskets II](src/main/java/g3401_3500/s3477_fruits_into_baskets_ii)| Easy | Array, Binary_Search, Simulation, Segment_Tree | 1 | 100.00
20912095
| 3475 |[DNA Pattern Recognition](src/main/java/g3401_3500/s3475_dna_pattern_recognition)| Medium | Database | 362 | 83.49
20922096
| 3474 |[Lexicographically Smallest Generated String](src/main/java/g3401_3500/s3474_lexicographically_smallest_generated_string)| Hard | String, Greedy, String_Matching | 17 | 64.86
20932097
| 3473 |[Sum of K Subarrays With Length at Least M](src/main/java/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m)| Medium | Array, Dynamic_Programming, Prefix_Sum | 191 | 52.16
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3477\. Fruits Into Baskets II
5+
6+
Easy
7+
8+
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.
9+
10+
From left to right, place the fruits according to these rules:
11+
12+
* 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.
13+
* Each basket can hold **only one** type of fruit.
14+
* If a fruit type **cannot be placed** in any basket, it remains **unplaced**.
15+
16+
Return the number of fruit types that remain unplaced after all possible allocations are made.
17+
18+
**Example 1:**
19+
20+
**Input:** fruits = [4,2,5], baskets = [3,5,4]
21+
22+
**Output:** 1
23+
24+
**Explanation:**
25+
26+
* `fruits[0] = 4` is placed in `baskets[1] = 5`.
27+
* `fruits[1] = 2` is placed in `baskets[0] = 3`.
28+
* `fruits[2] = 5` cannot be placed in `baskets[2] = 4`.
29+
30+
Since one fruit type remains unplaced, we return 1.
31+
32+
**Example 2:**
33+
34+
**Input:** fruits = [3,6,1], baskets = [6,4,7]
35+
36+
**Output:** 0
37+
38+
**Explanation:**
39+
40+
* `fruits[0] = 3` is placed in `baskets[0] = 6`.
41+
* `fruits[1] = 6` cannot be placed in `baskets[1] = 4` (insufficient capacity) but can be placed in the next available basket, `baskets[2] = 7`.
42+
* `fruits[2] = 1` is placed in `baskets[1] = 4`.
43+
44+
Since all fruits are successfully placed, we return 0.
45+
46+
**Constraints:**
47+
48+
* `n == fruits.length == baskets.length`
49+
* `1 <= n <= 100`
50+
* `1 <= fruits[i], baskets[i] <= 1000`
51+
52+
## Solution
53+
54+
```java
55+
public class Solution {
56+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
57+
int n = fruits.length;
58+
int currfruits;
59+
int count = 0;
60+
for (int i = 0; i < n; i++) {
61+
currfruits = fruits[i];
62+
for (int j = 0; j < n; j++) {
63+
if (baskets[j] >= currfruits) {
64+
count++;
65+
baskets[j] = 0;
66+
break;
67+
}
68+
}
69+
}
70+
return n - count;
71+
}
72+
}
73+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3478\. Choose K Elements With Maximum Sum
5+
6+
Medium
7+
8+
You are given two integer arrays, `nums1` and `nums2`, both of length `n`, along with a positive integer `k`.
9+
10+
For each index `i` from `0` to `n - 1`, perform the following:
11+
12+
* Find **all** indices `j` where `nums1[j]` is less than `nums1[i]`.
13+
* Choose **at most** `k` values of `nums2[j]` at these indices to **maximize** the total sum.
14+
15+
Return an array `answer` of size `n`, where `answer[i]` represents the result for the corresponding index `i`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2
20+
21+
**Output:** [80,30,0,80,50]
22+
23+
**Explanation:**
24+
25+
* 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`.
26+
* For `i = 1`: Select the 2 largest values from `nums2` at index `[2]` where `nums1[j] < nums1[1]`, resulting in 30.
27+
* For `i = 2`: No indices satisfy `nums1[j] < nums1[2]`, resulting in 0.
28+
* 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`.
29+
* For `i = 4`: Select the 2 largest values from `nums2` at indices `[1, 2]` where `nums1[j] < nums1[4]`, resulting in `30 + 20 = 50`.
30+
31+
**Example 2:**
32+
33+
**Input:** nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1
34+
35+
**Output:** [0,0,0,0]
36+
37+
**Explanation:**
38+
39+
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.
40+
41+
**Constraints:**
42+
43+
* `n == nums1.length == nums2.length`
44+
* <code>1 <= n <= 10<sup>5</sup></code>
45+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
46+
* `1 <= k <= n`
47+
48+
## Solution
49+
50+
```java
51+
import java.util.Arrays;
52+
import java.util.PriorityQueue;
53+
54+
public class Solution {
55+
public long[] findMaxSum(int[] nums1, int[] nums2, int k) {
56+
int n = nums1.length;
57+
long[] ans = new long[n];
58+
Point[] ps = new Point[n];
59+
for (int i = 0; i < n; i++) {
60+
ps[i] = new Point(nums1[i], nums2[i], i);
61+
}
62+
Arrays.sort(ps, (p1, p2) -> Integer.compare(p1.x, p2.x));
63+
PriorityQueue<Integer> pq = new PriorityQueue<>();
64+
long s = 0;
65+
int i = 0;
66+
while (i < n) {
67+
int j = i;
68+
while (j < n && ps[j].x == ps[i].x) {
69+
ans[ps[j].i] = s;
70+
j++;
71+
}
72+
for (int p = i; p < j; p++) {
73+
int cur = ps[p].y;
74+
if (pq.size() < k) {
75+
pq.offer(cur);
76+
s += cur;
77+
} else if (cur > pq.peek()) {
78+
s -= pq.poll();
79+
pq.offer(cur);
80+
s += cur;
81+
}
82+
}
83+
i = j;
84+
}
85+
return ans;
86+
}
87+
88+
private static class Point {
89+
int x;
90+
int y;
91+
int i;
92+
93+
Point(int x, int y, int i) {
94+
this.x = x;
95+
this.y = y;
96+
this.i = i;
97+
}
98+
}
99+
}
100+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3479\. Fruits Into Baskets III
5+
6+
Medium
7+
8+
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.
9+
10+
From left to right, place the fruits according to these rules:
11+
12+
* 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.
13+
* Each basket can hold **only one** type of fruit.
14+
* If a fruit type **cannot be placed** in any basket, it remains **unplaced**.
15+
16+
Return the number of fruit types that remain unplaced after all possible allocations are made.
17+
18+
**Example 1:**
19+
20+
**Input:** fruits = [4,2,5], baskets = [3,5,4]
21+
22+
**Output:** 1
23+
24+
**Explanation:**
25+
26+
* `fruits[0] = 4` is placed in `baskets[1] = 5`.
27+
* `fruits[1] = 2` is placed in `baskets[0] = 3`.
28+
* `fruits[2] = 5` cannot be placed in `baskets[2] = 4`.
29+
30+
Since one fruit type remains unplaced, we return 1.
31+
32+
**Example 2:**
33+
34+
**Input:** fruits = [3,6,1], baskets = [6,4,7]
35+
36+
**Output:** 0
37+
38+
**Explanation:**
39+
40+
* `fruits[0] = 3` is placed in `baskets[0] = 6`.
41+
* `fruits[1] = 6` cannot be placed in `baskets[1] = 4` (insufficient capacity) but can be placed in the next available basket, `baskets[2] = 7`.
42+
* `fruits[2] = 1` is placed in `baskets[1] = 4`.
43+
44+
Since all fruits are successfully placed, we return 0.
45+
46+
**Constraints:**
47+
48+
* `n == fruits.length == baskets.length`
49+
* <code>1 <= n <= 10<sup>5</sup></code>
50+
* <code>1 <= fruits[i], baskets[i] <= 10<sup>9</sup></code>
51+
52+
## Solution
53+
54+
```java
55+
public class Solution {
56+
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
57+
int n = baskets.length;
58+
int size = 1;
59+
while (size < n) {
60+
size <<= 1;
61+
}
62+
int[] seg = new int[2 * size];
63+
for (int i = 0; i < n; i++) {
64+
seg[size + i] = baskets[i];
65+
}
66+
for (int i = n; i < size; i++) {
67+
seg[size + i] = 0;
68+
}
69+
for (int i = size - 1; i > 0; i--) {
70+
seg[i] = Math.max(seg[i << 1], seg[i << 1 | 1]);
71+
}
72+
int ans = 0;
73+
for (int f : fruits) {
74+
if (seg[1] < f) {
75+
ans++;
76+
continue;
77+
}
78+
int idx = 1;
79+
while (idx < size) {
80+
if (seg[idx << 1] >= f) {
81+
idx = idx << 1;
82+
} else {
83+
idx = idx << 1 | 1;
84+
}
85+
}
86+
update(seg, idx - size, 0, size);
87+
}
88+
return ans;
89+
}
90+
91+
private void update(int[] seg, int pos, int val, int size) {
92+
int i = pos + size;
93+
seg[i] = val;
94+
for (i /= 2; i > 0; i /= 2) {
95+
seg[i] = Math.max(seg[i << 1], seg[i << 1 | 1]);
96+
}
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)