Skip to content

Commit b6205a0

Browse files
authored
Added tasks 3477-3480
1 parent 7ef90a3 commit b6205a0

File tree

12 files changed

+519
-0
lines changed

12 files changed

+519
-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_11_Time_3_ms_(100.00%)_Space_47.78_MB_(75.61%)
5+
6+
class Solution {
7+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
8+
val n = fruits.size
9+
var currfruits: Int
10+
var count = 0
11+
for (i in 0..<n) {
12+
currfruits = fruits[i]
13+
for (j in 0..<n) {
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,40 @@
1+
package g3401_3500.s3478_choose_k_elements_with_maximum_sum
2+
3+
// #Medium #Array #Sorting #Heap_Priority_Queue
4+
// #2025_03_11_Time_151_ms_(100.00%)_Space_93.64_MB_(40.74%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun findMaxSum(nums1: IntArray, nums2: IntArray, k: Int): LongArray {
10+
val n = nums1.size
11+
val ans = LongArray(n)
12+
val ps = Array<Point>(n) { i -> Point(nums1[i], nums2[i], i) }
13+
ps.sortWith { p1: Point, p2: Point -> p1.x.compareTo(p2.x) }
14+
val pq = PriorityQueue<Int?>()
15+
var s: Long = 0
16+
var i = 0
17+
while (i < n) {
18+
var j = i
19+
while (j < n && ps[j].x == ps[i].x) {
20+
ans[ps[j].i] = s
21+
j++
22+
}
23+
for (p in i..<j) {
24+
val cur = ps[p].y
25+
if (pq.size < k) {
26+
pq.offer(cur)
27+
s += cur.toLong()
28+
} else if (cur > pq.peek()!!) {
29+
s -= pq.poll()!!.toLong()
30+
pq.offer(cur)
31+
s += cur.toLong()
32+
}
33+
}
34+
i = j
35+
}
36+
return ans
37+
}
38+
39+
private class Point(var x: Int, var y: Int, var i: Int)
40+
}
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,53 @@
1+
package g3401_3500.s3479_fruits_into_baskets_iii
2+
3+
// #Medium #Array #Binary_Search #Ordered_Set #Segment_Tree
4+
// #2025_03_11_Time_53_ms_(92.86%)_Space_86.21_MB_(7.14%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
10+
val n = baskets.size
11+
var size = 1
12+
while (size < n) {
13+
size = size shl 1
14+
}
15+
val seg = IntArray(2 * size)
16+
for (i in 0..<n) {
17+
seg[size + i] = baskets[i]
18+
}
19+
for (i in n..<size) {
20+
seg[size + i] = 0
21+
}
22+
for (i in size - 1 downTo 1) {
23+
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
24+
}
25+
var ans = 0
26+
for (f in fruits) {
27+
if (seg[1] < f) {
28+
ans++
29+
continue
30+
}
31+
var idx = 1
32+
while (idx < size) {
33+
if (seg[idx shl 1] >= f) {
34+
idx = idx shl 1
35+
} else {
36+
idx = idx shl 1 or 1
37+
}
38+
}
39+
update(seg, idx - size, 0, size)
40+
}
41+
return ans
42+
}
43+
44+
private fun update(seg: IntArray, pos: Int, `val`: Int, size: Int) {
45+
var i = pos + size
46+
seg[i] = `val`
47+
i /= 2
48+
while (i > 0) {
49+
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
50+
i /= 2
51+
}
52+
}
53+
}
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,86 @@
1+
package g3401_3500.s3480_maximize_subarrays_after_removing_one_conflicting_pair
2+
3+
// #Hard #Array #Prefix_Sum #Enumeration #Segment_Tree
4+
// #2025_03_11_Time_48_ms_(100.00%)_Space_164.15_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun maxSubarrays(n: Int, conflictingPairs: Array<IntArray>): Long {
11+
val totalSubarrays = n.toLong() * (n + 1) / 2
12+
val h = IntArray(n + 1)
13+
val d2 = IntArray(n + 1)
14+
h.fill(n + 1)
15+
d2.fill(n + 1)
16+
for (pair in conflictingPairs) {
17+
var a = pair[0]
18+
var b = pair[1]
19+
if (a > b) {
20+
val temp = a
21+
a = b
22+
b = temp
23+
}
24+
if (b < h[a]) {
25+
d2[a] = h[a]
26+
h[a] = b
27+
} else if (b < d2[a]) {
28+
d2[a] = b
29+
}
30+
}
31+
val f = IntArray(n + 2)
32+
f[n + 1] = n + 1
33+
f[n] = h[n]
34+
for (i in n - 1 downTo 1) {
35+
f[i] = min(h[i], f[i + 1]).toInt()
36+
}
37+
// forbiddenCount(x) returns (n - x + 1) if x <= n, else 0.
38+
// This is the number of forbidden subarrays starting at some i when f[i] = x.
39+
var originalUnion: Long = 0
40+
for (i in 1..n) {
41+
if (f[i] <= n) {
42+
originalUnion += (n - f[i] + 1).toLong()
43+
}
44+
}
45+
val originalValid = totalSubarrays - originalUnion
46+
var best = originalValid
47+
// For each index j (1 <= j <= n) where a candidate conflicting pair exists,
48+
// simulate removal of the pair that gave h[j] (if any).
49+
// (If there is no candidate pair at j, h[j] remains n+1.)
50+
for (j in 1..n) {
51+
// no conflicting pair at index j
52+
if (h[j] == n + 1) {
53+
continue
54+
}
55+
// Simulate removal: new candidate at j becomes d2[j]
56+
val newCandidate = if (j < n) min(d2[j], f[j + 1]).toInt() else d2[j]
57+
// We'll recompute the new f values for indices 1..j.
58+
// Let newF[i] denote the updated value.
59+
// For i > j, newF[i] remains as original f[i].
60+
// For i = j, newF[j] = min( newCandidate, f[j+1] ) (which is newCandidate by
61+
// definition).
62+
val newFj = newCandidate
63+
// forbiddenCount(x) is defined as (n - x + 1) if x<= n, else 0.
64+
var delta = forbiddenCount(newFj, n) - forbiddenCount(f[j], n)
65+
var cur = newFj
66+
// Now update backwards for i = j-1 down to 1.
67+
for (i in j - 1 downTo 1) {
68+
val newVal = min(h[i], cur)
69+
// no further change for i' <= i
70+
if (newVal == f[i]) {
71+
break
72+
}
73+
delta += forbiddenCount(newVal, n) - forbiddenCount(f[i], n)
74+
cur = newVal
75+
}
76+
val newUnion = originalUnion + delta
77+
val newValid = totalSubarrays - newUnion
78+
best = max(best, newValid)
79+
}
80+
return best
81+
}
82+
83+
private fun forbiddenCount(x: Int, n: Int): Long {
84+
return (if (x <= n) (n - x + 1) else 0).toLong()
85+
}
86+
}
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]`

0 commit comments

Comments
 (0)