Skip to content

Commit 73c6e3e

Browse files
committed
Added tasks 3477-3480
1 parent 7ef90a3 commit 73c6e3e

File tree

12 files changed

+460
-0
lines changed

12 files changed

+460
-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_6_ms_(100.00%)_Space_47.78_MB_(100.00%)
4+
5+
class Solution {
6+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
7+
val n = fruits.size
8+
var ct = 0
9+
val used = BooleanArray(n)
10+
for (fruit in fruits) {
11+
var flag = true
12+
for (i in 0..<n) {
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3401_3500.s3478_choose_k_elements_with_maximum_sum
2+
3+
// #Medium #2025_03_09_Time_241_ms_(100.00%)_Space_89.04_MB_(100.00%)
4+
5+
import java.util.PriorityQueue
6+
7+
class Solution {
8+
fun findMaxSum(nums1: IntArray, nums2: IntArray, k: Int): LongArray {
9+
val n = nums1.size
10+
val arr = Array<IntArray>(n) { IntArray(3) }
11+
for (i in 0..<n) {
12+
arr[i][0] = nums1[i]
13+
arr[i][1] = nums2[i]
14+
arr[i][2] = i
15+
}
16+
arr.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
17+
val ans = LongArray(n)
18+
val pq = PriorityQueue<Int>()
19+
var sum: Long = 0
20+
for (i in 0..<n) {
21+
if (i > 0 && arr[i - 1][0] == arr[i][0]) {
22+
ans[arr[i][2]] = ans[arr[i - 1][2]]
23+
} else {
24+
ans[arr[i][2]] = sum
25+
}
26+
pq.add(arr[i][1])
27+
sum += arr[i][1].toLong()
28+
if (pq.size > k) {
29+
sum -= pq.remove()!!.toLong()
30+
}
31+
}
32+
return ans
33+
}
34+
}
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3401_3500.s3479_fruits_into_baskets_iii
2+
3+
// #Medium #2025_03_09_Time_605_ms_(100.00%)_Space_76.36_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
9+
var unp = 0
10+
var max = 0
11+
val list: MutableList<Int> = ArrayList<Int>()
12+
for (basket in baskets) {
13+
list.add(basket)
14+
max = max(max, basket)
15+
}
16+
for (fruit in fruits) {
17+
if (fruit > max) {
18+
unp++
19+
continue
20+
}
21+
var placed = false
22+
for (j in list.indices) {
23+
if (list[j] >= fruit) {
24+
list.removeAt(j)
25+
placed = true
26+
break
27+
}
28+
}
29+
if (!placed) {
30+
unp++
31+
}
32+
}
33+
return unp
34+
}
35+
}
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g3401_3500.s3480_maximize_subarrays_after_removing_one_conflicting_pair
2+
3+
// #Hard #2025_03_09_Time_768_ms_(100.00%)_Space_170.46_MB_(100.00%)
4+
5+
import java.util.TreeMap
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxSubarrays(n: Int, cp: Array<IntArray>): Long {
10+
for (pair in cp) {
11+
if (pair[0] > pair[1]) {
12+
val temp = pair[0]
13+
pair[0] = pair[1]
14+
pair[1] = temp
15+
}
16+
}
17+
cp.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
18+
val contributions = LongArray(cp.size)
19+
var totalPossible = n.toLong() * (n + 1) / 2
20+
val endPointMap = TreeMap<Int, MutableList<Int>>()
21+
var currentIndex = cp.size - 1
22+
for (start in n downTo 1) {
23+
while (currentIndex >= 0 && cp[currentIndex][0] >= start) {
24+
val end = cp[currentIndex][1]
25+
endPointMap.computeIfAbsent(end) { k: Int -> ArrayList<Int>() }
26+
.add(currentIndex)
27+
currentIndex--
28+
}
29+
if (endPointMap.isEmpty()) {
30+
continue
31+
}
32+
val firstEntry = endPointMap.firstEntry()
33+
val smallestEnd: Int = firstEntry.key!!
34+
val pairIndex: Int = firstEntry.value!![0]
35+
if (firstEntry.value!!.size == 1) {
36+
endPointMap.remove(smallestEnd)
37+
} else {
38+
firstEntry.value!!.removeAt(0)
39+
}
40+
val covered = n - smallestEnd + 1L
41+
totalPossible -= covered
42+
if (endPointMap.isEmpty()) {
43+
contributions[pairIndex] += covered
44+
} else {
45+
val nextEnd: Int = endPointMap.firstKey()!!
46+
contributions[pairIndex] += (nextEnd - smallestEnd).toLong()
47+
}
48+
endPointMap.computeIfAbsent(smallestEnd) { k: Int? -> ArrayList<Int>() }
49+
.add(pairIndex)
50+
}
51+
var result = totalPossible
52+
for (contribution in contributions) {
53+
result = max(result, (totalPossible + contribution))
54+
}
55+
return result
56+
}
57+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3401_3500.s3477_fruits_into_baskets_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun numOfUnplacedFruits() {
10+
assertThat<Int>(
11+
Solution().numOfUnplacedFruits(intArrayOf(4, 2, 5), intArrayOf(3, 5, 4)),
12+
equalTo<Int>(1),
13+
)
14+
}
15+
16+
@Test
17+
fun numOfUnplacedFruits2() {
18+
assertThat<Int>(
19+
Solution().numOfUnplacedFruits(intArrayOf(3, 6, 1), intArrayOf(6, 4, 7)),
20+
equalTo<Int>(0),
21+
)
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3401_3500.s3478_choose_k_elements_with_maximum_sum
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun findMaxSum() {
10+
assertThat<LongArray>(
11+
Solution()
12+
.findMaxSum(intArrayOf(4, 2, 1, 5, 3), intArrayOf(10, 20, 30, 40, 50), 2),
13+
equalTo<LongArray>(longArrayOf(80L, 30L, 0L, 80L, 50L)),
14+
)
15+
}
16+
17+
@Test
18+
fun findMaxSum2() {
19+
assertThat<LongArray>(
20+
Solution().findMaxSum(intArrayOf(2, 2, 2, 2), intArrayOf(3, 1, 2, 3), 1),
21+
equalTo<LongArray>(longArrayOf(0L, 0L, 0L, 0L)),
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)