Skip to content

Commit 8fbb68d

Browse files
committed
Added tasks 3492-3495
1 parent fba4231 commit 8fbb68d

File tree

12 files changed

+494
-0
lines changed

12 files changed

+494
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g3401_3500.s3492_maximum_containers_on_a_ship;
2+
3+
// #Easy #2025_03_23_Time_0_ms_(100.00%)_Space_40.80_MB_(88.06%)
4+
5+
public class Solution {
6+
public int maxContainers(int n, int w, int maxWeight) {
7+
int c = n * n;
8+
int count = maxWeight / w;
9+
return Math.min(c, count);
10+
}
11+
}
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,72 @@
1+
package g3401_3500.s3493_properties_graph;
2+
3+
// #Medium #2025_03_23_Time_28_ms_(99.84%)_Space_46.29_MB_(19.15%)
4+
5+
import java.util.ArrayList;
6+
import java.util.BitSet;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
public class Solution {
12+
private int[] parent;
13+
14+
public int numberOfComponents(int[][] properties, int k) {
15+
List<List<Integer>> al = convertToList(properties);
16+
int n = al.size();
17+
List<BitSet> bs = new ArrayList<>(n);
18+
for (List<Integer> integers : al) {
19+
BitSet bitset = new BitSet(101);
20+
for (int num : integers) {
21+
bitset.set(num);
22+
}
23+
bs.add(bitset);
24+
}
25+
parent = new int[n];
26+
for (int i = 0; i < n; i++) {
27+
parent[i] = i;
28+
}
29+
for (int i = 0; i < n; i++) {
30+
for (int j = i + 1; j < n; j++) {
31+
BitSet temp = (BitSet) bs.get(i).clone();
32+
temp.and(bs.get(j));
33+
int common = temp.cardinality();
34+
if (common >= k) {
35+
unionn(i, j);
36+
}
37+
}
38+
}
39+
Set<Integer> comps = new HashSet<>();
40+
for (int i = 0; i < n; i++) {
41+
comps.add(findp(i));
42+
}
43+
return comps.size();
44+
}
45+
46+
private int findp(int x) {
47+
if (parent[x] != x) {
48+
parent[x] = findp(parent[x]); // Path compression
49+
}
50+
return parent[x];
51+
}
52+
53+
private void unionn(int a, int b) {
54+
int pa = findp(a);
55+
int pb = findp(b);
56+
if (pa != pb) {
57+
parent[pa] = pb;
58+
}
59+
}
60+
61+
private List<List<Integer>> convertToList(int[][] arr) {
62+
List<List<Integer>> list = new ArrayList<>();
63+
for (int[] row : arr) {
64+
List<Integer> temp = new ArrayList<>();
65+
for (int num : row) {
66+
temp.add(num);
67+
}
68+
list.add(temp);
69+
}
70+
return list;
71+
}
72+
}
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 #2025_03_23_Time_115_ms_(91.61%)_Space_44.66_MB_(83.22%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long minTime(int[] skill, int[] mana) {
9+
long[] endTime = new long[skill.length];
10+
Arrays.fill(endTime, 0);
11+
for (int k : mana) {
12+
long t = 0;
13+
long maxDiff = 0;
14+
for (int j = 0; j < skill.length; ++j) {
15+
maxDiff = Math.max(maxDiff, endTime[j] - t);
16+
t += (long) skill[j] * (long) k;
17+
endTime[j] = t;
18+
}
19+
for (int j = 0; j < skill.length; ++j) {
20+
endTime[j] += maxDiff;
21+
}
22+
}
23+
return endTime[endTime.length - 1];
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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
22+
23+
Start time
24+
25+
Wizard 0 done by
26+
27+
Wizard 1 done by
28+
29+
Wizard 2 done by
30+
31+
Wizard 3 done by
32+
33+
0
34+
35+
0
36+
37+
5
38+
39+
30
40+
41+
40
42+
43+
60
44+
45+
1
46+
47+
52
48+
49+
53
50+
51+
58
52+
53+
60
54+
55+
64
56+
57+
2
58+
59+
54
60+
61+
58
62+
63+
78
64+
65+
86
66+
67+
102
68+
69+
3
70+
71+
86
72+
73+
88
74+
75+
98
76+
77+
102
78+
79+
110
80+
81+
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`.
82+
83+
**Example 2:**
84+
85+
**Input:** skill = [1,1,1], mana = [1,1,1]
86+
87+
**Output:** 5
88+
89+
**Explanation:**
90+
91+
1. Preparation of the 0<sup>th</sup> potion begins at time `t = 0`, and is completed by time `t = 3`.
92+
2. Preparation of the 1<sup>st</sup> potion begins at time `t = 1`, and is completed by time `t = 4`.
93+
3. Preparation of the 2<sup>nd</sup> potion begins at time `t = 2`, and is completed by time `t = 5`.
94+
95+
**Example 3:**
96+
97+
**Input:** skill = [1,2,3,4], mana = [1,2]
98+
99+
**Output:** 21
100+
101+
**Constraints:**
102+
103+
* `n == skill.length`
104+
* `m == mana.length`
105+
* `1 <= n, m <= 5000`
106+
* `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 #2025_03_23_Time_9_ms_(100.00%)_Space_101.25_MB_(65.40%)
4+
5+
public class Solution {
6+
public long minOperations(int[][] queries) {
7+
long result = 0;
8+
for (int[] query : queries) {
9+
long v = 4;
10+
long req = 1;
11+
long totalReq = 0;
12+
while (query[0] >= v) {
13+
v *= 4;
14+
req++;
15+
}
16+
long group;
17+
if (query[1] < v) {
18+
group = query[1] - query[0] + 1;
19+
totalReq += group * req;
20+
result += (totalReq + 1) / 2;
21+
continue;
22+
}
23+
group = v - query[0];
24+
totalReq += group * req;
25+
long 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+
}

0 commit comments

Comments
 (0)