Skip to content

Commit 641cf1e

Browse files
authored
Added tasks 3492-3495
1 parent c8f1c19 commit 641cf1e

File tree

13 files changed

+506
-9
lines changed

13 files changed

+506
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3401_3500.s3492_maximum_containers_on_a_ship
2+
3+
// #Easy #Math #2025_03_23_Time_0_ms_(100.00%)_Space_40.86_MB_(89.29%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun maxContainers(n: Int, w: Int, maxWeight: Int): Int {
9+
val c = n * n
10+
val count = maxWeight / w
11+
return min(c, count)
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3492\. Maximum Containers on a Ship
2+
3+
Easy
4+
5+
You are given a positive integer `n` representing an `n x n` cargo deck on a ship. Each cell on the deck can hold one container with a weight of **exactly** `w`.
6+
7+
However, the total weight of all containers, if loaded onto the deck, must not exceed the ship's maximum weight capacity, `maxWeight`.
8+
9+
Return the **maximum** number of containers that can be loaded onto the ship.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 2, w = 3, maxWeight = 15
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed `maxWeight`.
20+
21+
**Example 2:**
22+
23+
**Input:** n = 3, w = 5, maxWeight = 20
24+
25+
**Output:** 4
26+
27+
**Explanation:**
28+
29+
The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding `maxWeight` is 4.
30+
31+
**Constraints:**
32+
33+
* `1 <= n <= 1000`
34+
* `1 <= w <= 1000`
35+
* <code>1 <= maxWeight <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g3401_3500.s3493_properties_graph
2+
3+
// #Medium #Array #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
// #2025_03_23_Time_45_ms_(100.00%)_Space_65.05_MB_(60.00%)
5+
6+
import java.util.BitSet
7+
8+
class Solution {
9+
private lateinit var parent: IntArray
10+
11+
fun numberOfComponents(properties: Array<IntArray>, k: Int): Int {
12+
val al = convertToList(properties)
13+
val n = al.size
14+
val bs: MutableList<BitSet> = ArrayList<BitSet>(n)
15+
for (integers in al) {
16+
val bitset = BitSet(101)
17+
for (num in integers) {
18+
bitset.set(num)
19+
}
20+
bs.add(bitset)
21+
}
22+
parent = IntArray(n)
23+
for (i in 0..<n) {
24+
parent[i] = i
25+
}
26+
for (i in 0..<n) {
27+
for (j in i + 1..<n) {
28+
val temp = bs[i].clone() as BitSet
29+
temp.and(bs[j])
30+
val common = temp.cardinality()
31+
if (common >= k) {
32+
unionn(i, j)
33+
}
34+
}
35+
}
36+
val comps: MutableSet<Int> = HashSet<Int>()
37+
for (i in 0..<n) {
38+
comps.add(findp(i))
39+
}
40+
return comps.size
41+
}
42+
43+
private fun findp(x: Int): Int {
44+
if (parent[x] != x) {
45+
parent[x] = findp(parent[x])
46+
}
47+
return parent[x]
48+
}
49+
50+
private fun unionn(a: Int, b: Int) {
51+
val pa = findp(a)
52+
val pb = findp(b)
53+
if (pa != pb) {
54+
parent[pa] = pb
55+
}
56+
}
57+
58+
private fun convertToList(arr: Array<IntArray>): MutableList<MutableList<Int>> {
59+
val list: MutableList<MutableList<Int>> = ArrayList<MutableList<Int>>()
60+
for (row in arr) {
61+
val temp: MutableList<Int> = ArrayList<Int>()
62+
for (num in row) {
63+
temp.add(num)
64+
}
65+
list.add(temp)
66+
}
67+
return list
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3493\. Properties Graph
2+
3+
Medium
4+
5+
You are given a 2D integer array `properties` having dimensions `n x m` and an integer `k`.
6+
7+
Define a function `intersect(a, b)` that returns the **number of distinct integers** common to both arrays `a` and `b`.
8+
9+
Construct an **undirected** graph where each index `i` corresponds to `properties[i]`. There is an edge between node `i` and node `j` if and only if `intersect(properties[i], properties[j]) >= k`, where `i` and `j` are in the range `[0, n - 1]` and `i != j`.
10+
11+
Return the number of **connected components** in the resulting graph.
12+
13+
**Example 1:**
14+
15+
**Input:** properties = [[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]], k = 1
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
The graph formed has 3 connected components:
22+
23+
![](https://assets.leetcode.com/uploads/2025/02/27/image.png)
24+
25+
**Example 2:**
26+
27+
**Input:** properties = [[1,2,3],[2,3,4],[4,3,5]], k = 2
28+
29+
**Output:** 1
30+
31+
**Explanation:**
32+
33+
The graph formed has 1 connected component:
34+
35+
![](https://assets.leetcode.com/uploads/2025/02/27/screenshot-from-2025-02-27-23-58-34.png)
36+
37+
**Example 3:**
38+
39+
**Input:** properties = [[1,1],[1,1]], k = 2
40+
41+
**Output:** 2
42+
43+
**Explanation:**
44+
45+
`intersect(properties[0], properties[1]) = 1`, which is less than `k`. This means there is no edge between `properties[0]` and `properties[1]` in the graph.
46+
47+
**Constraints:**
48+
49+
* `1 <= n == properties.length <= 100`
50+
* `1 <= m == properties[i].length <= 100`
51+
* `1 <= properties[i][j] <= 100`
52+
* `1 <= k <= m`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3401_3500.s3494_find_the_minimum_amount_of_time_to_brew_potions
2+
3+
// #Medium #Array #Simulation #Prefix_Sum #2025_03_23_Time_70_ms_(100.00%)_Space_50.98_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minTime(skill: IntArray, mana: IntArray): Long {
9+
val endTime = LongArray(skill.size)
10+
endTime.fill(0)
11+
for (k in mana) {
12+
var t: Long = 0
13+
var maxDiff: Long = 0
14+
for (j in skill.indices) {
15+
maxDiff = max(maxDiff, endTime[j] - t)
16+
t += skill[j].toLong() * k.toLong()
17+
endTime[j] = t
18+
}
19+
for (j in skill.indices) {
20+
endTime[j] += maxDiff
21+
}
22+
}
23+
return endTime[endTime.size - 1]
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3494\. Find the Minimum Amount of Time to Brew Potions
2+
3+
Medium
4+
5+
You are given two integer arrays, `skill` and `mana`, of length `n` and `m`, respectively.
6+
7+
In a laboratory, `n` wizards must brew `m` potions _in order_. Each potion has a mana capacity `mana[j]` and **must** pass through **all** the wizards sequentially to be brewed properly. The time taken by the <code>i<sup>th</sup></code> wizard on the <code>j<sup>th</sup></code> potion is <code>time<sub>ij</sub> = skill[i] * mana[j]</code>.
8+
9+
Since the brewing process is delicate, a potion **must** be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be _synchronized_ so that each wizard begins working on a potion **exactly** when it arrives.
10+
11+
Return the **minimum** amount of time required for the potions to be brewed properly.
12+
13+
**Example 1:**
14+
15+
**Input:** skill = [1,5,2,4], mana = [5,1,4,2]
16+
17+
**Output:** 110
18+
19+
**Explanation:**
20+
21+
| Potion Number | Start time | Wizard 0 done by | Wizard 1 done by | Wizard 2 done by | Wizard 3 done by |
22+
|--------------|-----------|------------------|------------------|------------------|------------------|
23+
| 0 | 0 | 5 | 30 | 40 | 60 |
24+
| 1 | 52 | 53 | 58 | 60 | 64 |
25+
| 2 | 54 | 58 | 78 | 86 | 102 |
26+
| 3 | 86 | 88 | 98 | 102 | 110 |
27+
28+
As an example for why wizard 0 cannot start working on the 1<sup>st</sup> potion before time `t = 52`, consider the case where the wizards started preparing the 1<sup>st</sup> potion at time `t = 50`. At time `t = 58`, wizard 2 is done with the 1<sup>st</sup> potion, but wizard 3 will still be working on the 0<sup>th</sup> potion till time `t = 60`.
29+
30+
**Example 2:**
31+
32+
**Input:** skill = [1,1,1], mana = [1,1,1]
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
1. Preparation of the 0<sup>th</sup> potion begins at time `t = 0`, and is completed by time `t = 3`.
39+
2. Preparation of the 1<sup>st</sup> potion begins at time `t = 1`, and is completed by time `t = 4`.
40+
3. Preparation of the 2<sup>nd</sup> potion begins at time `t = 2`, and is completed by time `t = 5`.
41+
42+
**Example 3:**
43+
44+
**Input:** skill = [1,2,3,4], mana = [1,2]
45+
46+
**Output:** 21
47+
48+
**Constraints:**
49+
50+
* `n == skill.length`
51+
* `m == mana.length`
52+
* `1 <= n, m <= 5000`
53+
* `1 <= mana[i], skill[i] <= 5000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3401_3500.s3495_minimum_operations_to_make_array_elements_zero
2+
3+
// #Hard #Array #Math #Bit_Manipulation #2025_03_23_Time_12_ms_(100.00%)_Space_105.09_MB_(100.00%)
4+
5+
class Solution {
6+
fun minOperations(queries: Array<IntArray>): Long {
7+
var result: Long = 0
8+
for (query in queries) {
9+
var v: Long = 4
10+
var req: Long = 1
11+
var totalReq: Long = 0
12+
while (query[0] >= v) {
13+
v *= 4
14+
req++
15+
}
16+
var group: Long
17+
if (query[1] < v) {
18+
group = query[1] - query[0] + 1L
19+
totalReq += group * req
20+
result += (totalReq + 1) / 2
21+
continue
22+
}
23+
group = v - query[0]
24+
totalReq += group * req
25+
var bottom = v
26+
while (true) {
27+
v *= 4
28+
req++
29+
if (query[1] < v) {
30+
group = query[1] - bottom + 1
31+
totalReq += group * req
32+
break
33+
}
34+
group = v - bottom
35+
totalReq += group * req
36+
bottom = v
37+
}
38+
result += (totalReq + 1) / 2
39+
}
40+
return result
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
3495\. Minimum Operations to Make Array Elements Zero
2+
3+
Hard
4+
5+
You are given a 2D array `queries`, where `queries[i]` is of the form `[l, r]`. Each `queries[i]` defines an array of integers `nums` consisting of elements ranging from `l` to `r`, both **inclusive**.
6+
7+
In one operation, you can:
8+
9+
* Select two integers `a` and `b` from the array.
10+
* Replace them with `floor(a / 4)` and `floor(b / 4)`.
11+
12+
Your task is to determine the **minimum** number of operations required to reduce all elements of the array to zero for each query. Return the sum of the results for all queries.
13+
14+
**Example 1:**
15+
16+
**Input:** queries = [[1,2],[2,4]]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
For `queries[0]`:
23+
24+
* The initial array is `nums = [1, 2]`.
25+
* In the first operation, select `nums[0]` and `nums[1]`. The array becomes `[0, 0]`.
26+
* The minimum number of operations required is 1.
27+
28+
For `queries[1]`:
29+
30+
* The initial array is `nums = [2, 3, 4]`.
31+
* In the first operation, select `nums[0]` and `nums[2]`. The array becomes `[0, 3, 1]`.
32+
* In the second operation, select `nums[1]` and `nums[2]`. The array becomes `[0, 0, 0]`.
33+
* The minimum number of operations required is 2.
34+
35+
The output is `1 + 2 = 3`.
36+
37+
**Example 2:**
38+
39+
**Input:** queries = [[2,6]]
40+
41+
**Output:** 4
42+
43+
**Explanation:**
44+
45+
For `queries[0]`:
46+
47+
* The initial array is `nums = [2, 3, 4, 5, 6]`.
48+
* In the first operation, select `nums[0]` and `nums[3]`. The array becomes `[0, 3, 4, 1, 6]`.
49+
* In the second operation, select `nums[2]` and `nums[4]`. The array becomes `[0, 3, 1, 1, 1]`.
50+
* In the third operation, select `nums[1]` and `nums[2]`. The array becomes `[0, 0, 0, 1, 1]`.
51+
* In the fourth operation, select `nums[3]` and `nums[4]`. The array becomes `[0, 0, 0, 0, 0]`.
52+
* The minimum number of operations required is 4.
53+
54+
The output is 4.
55+
56+
**Constraints:**
57+
58+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
59+
* `queries[i].length == 2`
60+
* `queries[i] == [l, r]`
61+
* <code>1 <= l < r <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)