Skip to content

Added tasks 3492-3495 #1947

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 7 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
@@ -1,6 +1,6 @@
package g3401_3500.s3486_longest_special_path_ii;

// #Hard #Array #Hash_Table #Tree #Prefix_Sum #Depth_First_Search
// #Hard #Array #Hash_Table #Depth_First_Search #Tree #Prefix_Sum
// #2025_03_17_Time_166_ms_(100.00%)_Space_105.50_MB_(100.00%)

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package g3401_3500.s3492_maximum_containers_on_a_ship;

// #Easy #Math #2025_03_24_Time_0_ms_(100.00%)_Space_40.73_MB_(100.00%)

public class Solution {
public int maxContainers(int n, int w, int maxWeight) {
int c = n * n;
int count = maxWeight / w;
return Math.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>
73 changes: 73 additions & 0 deletions src/main/java/g3401_3500/s3493_properties_graph/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package g3401_3500.s3493_properties_graph;

// #Medium #Array #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
// #2025_03_24_Time_27_ms_(99.82%)_Space_46.06_MB_(37.59%)

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution {
private int[] parent;

public int numberOfComponents(int[][] properties, int k) {
List<List<Integer>> al = convertToList(properties);
int n = al.size();
List<BitSet> bs = new ArrayList<>(n);
for (List<Integer> integers : al) {
BitSet bitset = new BitSet(101);
for (int num : integers) {
bitset.set(num);
}
bs.add(bitset);
}
parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
BitSet temp = (BitSet) bs.get(i).clone();
temp.and(bs.get(j));
int common = temp.cardinality();
if (common >= k) {
unionn(i, j);
}
}
}
Set<Integer> comps = new HashSet<>();
for (int i = 0; i < n; i++) {
comps.add(findp(i));
}
return comps.size();
}

private int findp(int x) {
if (parent[x] != x) {
parent[x] = findp(parent[x]);
}
return parent[x];
}

private void unionn(int a, int b) {
int pa = findp(a);
int pb = findp(b);
if (pa != pb) {
parent[pa] = pb;
}
}

private List<List<Integer>> convertToList(int[][] arr) {
List<List<Integer>> list = new ArrayList<>();
for (int[] row : arr) {
List<Integer> temp = new ArrayList<>();
for (int num : row) {
temp.add(num);
}
list.add(temp);
}
return list;
}
}
52 changes: 52 additions & 0 deletions src/main/java/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_24_Time_113_ms_(90.95%)_Space_44.81_MB_(64.17%)

import java.util.Arrays;

public class Solution {
public long minTime(int[] skill, int[] mana) {
long[] endTime = new long[skill.length];
Arrays.fill(endTime, 0);
for (int k : mana) {
long t = 0;
long maxDiff = 0;
for (int j = 0; j < skill.length; ++j) {
maxDiff = Math.max(maxDiff, endTime[j] - t);
t += (long) skill[j] * (long) k;
endTime[j] = t;
}
for (int j = 0; j < skill.length; ++j) {
endTime[j] += maxDiff;
}
}
return endTime[endTime.length - 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_24_Time_9_ms_(99.71%)_Space_101.35_MB_(52.17%)

public class Solution {
public long minOperations(int[][] queries) {
long result = 0;
for (int[] query : queries) {
long v = 4;
long req = 1;
long totalReq = 0;
while (query[0] >= v) {
v *= 4;
req++;
}
long group;
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;
long 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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3401_3500.s3492_maximum_containers_on_a_ship;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void maxContainers() {
assertThat(new Solution().maxContainers(2, 3, 15), equalTo(4));
}

@Test
void maxContainers2() {
assertThat(new Solution().maxContainers(3, 5, 20), equalTo(4));
}
}
Loading