Skip to content

Commit 0b9185a

Browse files
committed
Added tasks 3477-3480
1 parent ff24479 commit 0b9185a

File tree

5 files changed

+396
-0
lines changed
  • src/main/kotlin/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

+396
-0
lines changed

README.md

+4
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/kotlin/g3401_3500/s3480_maximize_subarrays_after_removing_one_conflicting_pair)| Hard | Array, Prefix_Sum, Enumeration, Segment_Tree | 48 | 100.00
2092+
| 3479 |[Fruits Into Baskets III](src/main/kotlin/g3401_3500/s3479_fruits_into_baskets_iii)| Medium | Array, Binary_Search, Ordered_Set, Segment_Tree | 53 | 92.86
2093+
| 3478 |[Choose K Elements With Maximum Sum](src/main/kotlin/g3401_3500/s3478_choose_k_elements_with_maximum_sum)| Medium | Array, Sorting, Heap_Priority_Queue | 151 | 100.00
2094+
| 3477 |[Fruits Into Baskets II](src/main/kotlin/g3401_3500/s3477_fruits_into_baskets_ii)| Easy | Array, Binary_Search, Simulation, Segment_Tree | 3 | 100.00
20912095
| 3475 |[DNA Pattern Recognition](src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition)| Medium | Database | 362 | 83.49
20922096
| 3474 |[Lexicographically Smallest Generated String](src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string)| Hard | String, Greedy, String_Matching | 30 | 100.00
20932097
| 3473 |[Sum of K Subarrays With Length at Least M](src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m)| Medium | Array, Dynamic_Programming, Prefix_Sum | 227 | 24.47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
55+
class Solution {
56+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
57+
val n = fruits.size
58+
var currfruits: Int
59+
var count = 0
60+
for (i in 0..<n) {
61+
currfruits = fruits[i]
62+
for (j in 0..<n) {
63+
if (baskets[j] >= currfruits) {
64+
count++
65+
baskets[j] = 0
66+
break
67+
}
68+
}
69+
}
70+
return n - count
71+
}
72+
}
73+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
51+
import java.util.PriorityQueue
52+
53+
class Solution {
54+
fun findMaxSum(nums1: IntArray, nums2: IntArray, k: Int): LongArray {
55+
val n = nums1.size
56+
val ans = LongArray(n)
57+
val ps = Array<Point>(n) { i -> Point(nums1[i], nums2[i], i) }
58+
ps.sortWith { p1: Point, p2: Point -> p1.x.compareTo(p2.x) }
59+
val pq = PriorityQueue<Int?>()
60+
var s: Long = 0
61+
var i = 0
62+
while (i < n) {
63+
var j = i
64+
while (j < n && ps[j].x == ps[i].x) {
65+
ans[ps[j].i] = s
66+
j++
67+
}
68+
for (p in i..<j) {
69+
val cur = ps[p].y
70+
if (pq.size < k) {
71+
pq.offer(cur)
72+
s += cur.toLong()
73+
} else if (cur > pq.peek()!!) {
74+
s -= pq.poll()!!.toLong()
75+
pq.offer(cur)
76+
s += cur.toLong()
77+
}
78+
}
79+
i = j
80+
}
81+
return ans
82+
}
83+
84+
private class Point(var x: Int, var y: Int, var i: Int)
85+
}
86+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
55+
import kotlin.math.max
56+
57+
class Solution {
58+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
59+
val n = baskets.size
60+
var size = 1
61+
while (size < n) {
62+
size = size shl 1
63+
}
64+
val seg = IntArray(2 * size)
65+
for (i in 0..<n) {
66+
seg[size + i] = baskets[i]
67+
}
68+
for (i in n..<size) {
69+
seg[size + i] = 0
70+
}
71+
for (i in size - 1 downTo 1) {
72+
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
73+
}
74+
var ans = 0
75+
for (f in fruits) {
76+
if (seg[1] < f) {
77+
ans++
78+
continue
79+
}
80+
var idx = 1
81+
while (idx < size) {
82+
if (seg[idx shl 1] >= f) {
83+
idx = idx shl 1
84+
} else {
85+
idx = idx shl 1 or 1
86+
}
87+
}
88+
update(seg, idx - size, 0, size)
89+
}
90+
return ans
91+
}
92+
93+
private fun update(seg: IntArray, pos: Int, `val`: Int, size: Int) {
94+
var i = pos + size
95+
seg[i] = `val`
96+
i /= 2
97+
while (i > 0) {
98+
seg[i] = max(seg[i shl 1].toDouble(), seg[i shl 1 or 1].toDouble()).toInt()
99+
i /= 2
100+
}
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)