Skip to content

Added tasks 3492-3495 #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package g3401_3500.s3492_maximum_containers_on_a_ship

// #Easy #Math #2025_03_23_Time_0_ms_(100.00%)_Space_40.86_MB_(89.29%)

import kotlin.math.min

class Solution {
fun maxContainers(n: Int, w: Int, maxWeight: Int): Int {
val c = n * n
val count = maxWeight / w
return min(c, count)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
3492\. Maximum Containers on a Ship

Easy

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`.

However, the total weight of all containers, if loaded onto the deck, must not exceed the ship's maximum weight capacity, `maxWeight`.

Return the **maximum** number of containers that can be loaded onto the ship.

**Example 1:**

**Input:** n = 2, w = 3, maxWeight = 15

**Output:** 4

**Explanation:**

The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed `maxWeight`.

**Example 2:**

**Input:** n = 3, w = 5, maxWeight = 20

**Output:** 4

**Explanation:**

The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding `maxWeight` is 4.

**Constraints:**

* `1 <= n <= 1000`
* `1 <= w <= 1000`
* <code>1 <= maxWeight <= 10<sup>9</sup></code>
69 changes: 69 additions & 0 deletions src/main/kotlin/g3401_3500/s3493_properties_graph/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package g3401_3500.s3493_properties_graph

// #Medium #Array #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
// #2025_03_23_Time_45_ms_(100.00%)_Space_65.05_MB_(60.00%)

import java.util.BitSet

class Solution {
private lateinit var parent: IntArray

fun numberOfComponents(properties: Array<IntArray>, k: Int): Int {
val al = convertToList(properties)
val n = al.size
val bs: MutableList<BitSet> = ArrayList<BitSet>(n)
for (integers in al) {
val bitset = BitSet(101)
for (num in integers) {
bitset.set(num)
}
bs.add(bitset)
}
parent = IntArray(n)
for (i in 0..<n) {
parent[i] = i
}
for (i in 0..<n) {
for (j in i + 1..<n) {
val temp = bs[i].clone() as BitSet
temp.and(bs[j])
val common = temp.cardinality()
if (common >= k) {
unionn(i, j)
}
}
}
val comps: MutableSet<Int> = HashSet<Int>()
for (i in 0..<n) {
comps.add(findp(i))
}
return comps.size
}

private fun findp(x: Int): Int {
if (parent[x] != x) {
parent[x] = findp(parent[x])
}
return parent[x]
}

private fun unionn(a: Int, b: Int) {
val pa = findp(a)
val pb = findp(b)
if (pa != pb) {
parent[pa] = pb
}
}

private fun convertToList(arr: Array<IntArray>): MutableList<MutableList<Int>> {
val list: MutableList<MutableList<Int>> = ArrayList<MutableList<Int>>()
for (row in arr) {
val temp: MutableList<Int> = ArrayList<Int>()
for (num in row) {
temp.add(num)
}
list.add(temp)
}
return list
}
}
52 changes: 52 additions & 0 deletions src/main/kotlin/g3401_3500/s3493_properties_graph/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3493\. Properties Graph

Medium

You are given a 2D integer array `properties` having dimensions `n x m` and an integer `k`.

Define a function `intersect(a, b)` that returns the **number of distinct integers** common to both arrays `a` and `b`.

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`.

Return the number of **connected components** in the resulting graph.

**Example 1:**

**Input:** properties = [[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]], k = 1

**Output:** 3

**Explanation:**

The graph formed has 3 connected components:

![](https://assets.leetcode.com/uploads/2025/02/27/image.png)

**Example 2:**

**Input:** properties = [[1,2,3],[2,3,4],[4,3,5]], k = 2

**Output:** 1

**Explanation:**

The graph formed has 1 connected component:

![](https://assets.leetcode.com/uploads/2025/02/27/screenshot-from-2025-02-27-23-58-34.png)

**Example 3:**

**Input:** properties = [[1,1],[1,1]], k = 2

**Output:** 2

**Explanation:**

`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.

**Constraints:**

* `1 <= n == properties.length <= 100`
* `1 <= m == properties[i].length <= 100`
* `1 <= properties[i][j] <= 100`
* `1 <= k <= m`
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g3401_3500.s3494_find_the_minimum_amount_of_time_to_brew_potions

// #Medium #Array #Simulation #Prefix_Sum #2025_03_23_Time_70_ms_(100.00%)_Space_50.98_MB_(100.00%)

import kotlin.math.max

class Solution {
fun minTime(skill: IntArray, mana: IntArray): Long {
val endTime = LongArray(skill.size)
endTime.fill(0)
for (k in mana) {
var t: Long = 0
var maxDiff: Long = 0
for (j in skill.indices) {
maxDiff = max(maxDiff, endTime[j] - t)
t += skill[j].toLong() * k.toLong()
endTime[j] = t
}
for (j in skill.indices) {
endTime[j] += maxDiff
}
}
return endTime[endTime.size - 1]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3494\. Find the Minimum Amount of Time to Brew Potions

Medium

You are given two integer arrays, `skill` and `mana`, of length `n` and `m`, respectively.

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>.

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.

Return the **minimum** amount of time required for the potions to be brewed properly.

**Example 1:**

**Input:** skill = [1,5,2,4], mana = [5,1,4,2]

**Output:** 110

**Explanation:**

| Potion Number | Start time | Wizard 0 done by | Wizard 1 done by | Wizard 2 done by | Wizard 3 done by |
|--------------|-----------|------------------|------------------|------------------|------------------|
| 0 | 0 | 5 | 30 | 40 | 60 |
| 1 | 52 | 53 | 58 | 60 | 64 |
| 2 | 54 | 58 | 78 | 86 | 102 |
| 3 | 86 | 88 | 98 | 102 | 110 |

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`.

**Example 2:**

**Input:** skill = [1,1,1], mana = [1,1,1]

**Output:** 5

**Explanation:**

1. Preparation of the 0<sup>th</sup> potion begins at time `t = 0`, and is completed by time `t = 3`.
2. Preparation of the 1<sup>st</sup> potion begins at time `t = 1`, and is completed by time `t = 4`.
3. Preparation of the 2<sup>nd</sup> potion begins at time `t = 2`, and is completed by time `t = 5`.

**Example 3:**

**Input:** skill = [1,2,3,4], mana = [1,2]

**Output:** 21

**Constraints:**

* `n == skill.length`
* `m == mana.length`
* `1 <= n, m <= 5000`
* `1 <= mana[i], skill[i] <= 5000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g3401_3500.s3495_minimum_operations_to_make_array_elements_zero

// #Hard #Array #Math #Bit_Manipulation #2025_03_23_Time_12_ms_(100.00%)_Space_105.09_MB_(100.00%)

class Solution {
fun minOperations(queries: Array<IntArray>): Long {
var result: Long = 0
for (query in queries) {
var v: Long = 4
var req: Long = 1
var totalReq: Long = 0
while (query[0] >= v) {
v *= 4
req++
}
var group: Long
if (query[1] < v) {
group = query[1] - query[0] + 1L
totalReq += group * req
result += (totalReq + 1) / 2
continue
}
group = v - query[0]
totalReq += group * req
var bottom = v
while (true) {
v *= 4
req++
if (query[1] < v) {
group = query[1] - bottom + 1
totalReq += group * req
break
}
group = v - bottom
totalReq += group * req
bottom = v
}
result += (totalReq + 1) / 2
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
3495\. Minimum Operations to Make Array Elements Zero

Hard

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**.

In one operation, you can:

* Select two integers `a` and `b` from the array.
* Replace them with `floor(a / 4)` and `floor(b / 4)`.

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.

**Example 1:**

**Input:** queries = [[1,2],[2,4]]

**Output:** 3

**Explanation:**

For `queries[0]`:

* The initial array is `nums = [1, 2]`.
* In the first operation, select `nums[0]` and `nums[1]`. The array becomes `[0, 0]`.
* The minimum number of operations required is 1.

For `queries[1]`:

* The initial array is `nums = [2, 3, 4]`.
* In the first operation, select `nums[0]` and `nums[2]`. The array becomes `[0, 3, 1]`.
* In the second operation, select `nums[1]` and `nums[2]`. The array becomes `[0, 0, 0]`.
* The minimum number of operations required is 2.

The output is `1 + 2 = 3`.

**Example 2:**

**Input:** queries = [[2,6]]

**Output:** 4

**Explanation:**

For `queries[0]`:

* The initial array is `nums = [2, 3, 4, 5, 6]`.
* In the first operation, select `nums[0]` and `nums[3]`. The array becomes `[0, 3, 4, 1, 6]`.
* In the second operation, select `nums[2]` and `nums[4]`. The array becomes `[0, 3, 1, 1, 1]`.
* In the third operation, select `nums[1]` and `nums[2]`. The array becomes `[0, 0, 0, 1, 1]`.
* In the fourth operation, select `nums[3]` and `nums[4]`. The array becomes `[0, 0, 0, 0, 0]`.
* The minimum number of operations required is 4.

The output is 4.

**Constraints:**

* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* `queries[i] == [l, r]`
* <code>1 <= l < r <= 10<sup>9</sup></code>
Loading