diff --git a/src/main/java/g3401_3500/s3486_longest_special_path_ii/Solution.java b/src/main/java/g3401_3500/s3486_longest_special_path_ii/Solution.java index f54901302..cc9140350 100644 --- a/src/main/java/g3401_3500/s3486_longest_special_path_ii/Solution.java +++ b/src/main/java/g3401_3500/s3486_longest_special_path_ii/Solution.java @@ -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; diff --git a/src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship/Solution.java b/src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship/Solution.java new file mode 100644 index 000000000..00db13d10 --- /dev/null +++ b/src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship/Solution.java @@ -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); + } +} diff --git a/src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship/readme.md b/src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship/readme.md new file mode 100644 index 000000000..991f16e56 --- /dev/null +++ b/src/main/java/g3401_3500/s3492_maximum_containers_on_a_ship/readme.md @@ -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` +* 1 <= maxWeight <= 109 \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3493_properties_graph/Solution.java b/src/main/java/g3401_3500/s3493_properties_graph/Solution.java new file mode 100644 index 000000000..b62e218e5 --- /dev/null +++ b/src/main/java/g3401_3500/s3493_properties_graph/Solution.java @@ -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> al = convertToList(properties); + int n = al.size(); + List bs = new ArrayList<>(n); + for (List 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 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> convertToList(int[][] arr) { + List> list = new ArrayList<>(); + for (int[] row : arr) { + List temp = new ArrayList<>(); + for (int num : row) { + temp.add(num); + } + list.add(temp); + } + return list; + } +} diff --git a/src/main/java/g3401_3500/s3493_properties_graph/readme.md b/src/main/java/g3401_3500/s3493_properties_graph/readme.md new file mode 100644 index 000000000..ddd24b010 --- /dev/null +++ b/src/main/java/g3401_3500/s3493_properties_graph/readme.md @@ -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` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/Solution.java b/src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/Solution.java new file mode 100644 index 000000000..7dcd2e0b8 --- /dev/null +++ b/src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/Solution.java @@ -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]; + } +} diff --git a/src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/readme.md b/src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/readme.md new file mode 100644 index 000000000..fd404460c --- /dev/null +++ b/src/main/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/readme.md @@ -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 ith wizard on the jth potion is timeij = skill[i] * mana[j]. + +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 1st potion before time `t = 52`, consider the case where the wizards started preparing the 1st potion at time `t = 50`. At time `t = 58`, wizard 2 is done with the 1st potion, but wizard 3 will still be working on the 0th potion till time `t = 60`. + +**Example 2:** + +**Input:** skill = [1,1,1], mana = [1,1,1] + +**Output:** 5 + +**Explanation:** + +1. Preparation of the 0th potion begins at time `t = 0`, and is completed by time `t = 3`. +2. Preparation of the 1st potion begins at time `t = 1`, and is completed by time `t = 4`. +3. Preparation of the 2nd 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` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/Solution.java b/src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/Solution.java new file mode 100644 index 000000000..73b4d2316 --- /dev/null +++ b/src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/Solution.java @@ -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; + } +} diff --git a/src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/readme.md b/src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/readme.md new file mode 100644 index 000000000..5eb5d513b --- /dev/null +++ b/src/main/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/readme.md @@ -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:** + +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* `queries[i] == [l, r]` +* 1 <= l < r <= 109 \ No newline at end of file diff --git a/src/test/java/g3401_3500/s3492_maximum_containers_on_a_ship/SolutionTest.java b/src/test/java/g3401_3500/s3492_maximum_containers_on_a_ship/SolutionTest.java new file mode 100644 index 000000000..1c8a591ce --- /dev/null +++ b/src/test/java/g3401_3500/s3492_maximum_containers_on_a_ship/SolutionTest.java @@ -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)); + } +} diff --git a/src/test/java/g3401_3500/s3493_properties_graph/SolutionTest.java b/src/test/java/g3401_3500/s3493_properties_graph/SolutionTest.java new file mode 100644 index 000000000..5a008b30b --- /dev/null +++ b/src/test/java/g3401_3500/s3493_properties_graph/SolutionTest.java @@ -0,0 +1,29 @@ +package g3401_3500.s3493_properties_graph; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfComponents() { + assertThat( + new Solution() + .numberOfComponents( + new int[][] {{1, 2}, {1, 1}, {3, 4}, {4, 5}, {5, 6}, {7, 7}}, 1), + equalTo(3)); + } + + @Test + void numberOfComponents2() { + assertThat( + new Solution().numberOfComponents(new int[][] {{1, 2, 3}, {2, 3, 4}, {4, 3, 5}}, 2), + equalTo(1)); + } + + @Test + void numberOfComponents3() { + assertThat(new Solution().numberOfComponents(new int[][] {{1, 1}, {1, 1}}, 2), equalTo(2)); + } +} diff --git a/src/test/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/SolutionTest.java b/src/test/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/SolutionTest.java new file mode 100644 index 000000000..a21ba9b2c --- /dev/null +++ b/src/test/java/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions/SolutionTest.java @@ -0,0 +1,25 @@ +package g3401_3500.s3494_find_the_minimum_amount_of_time_to_brew_potions; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minTime() { + assertThat( + new Solution().minTime(new int[] {1, 5, 2, 4}, new int[] {5, 1, 4, 2}), + equalTo(110L)); + } + + @Test + void minTime2() { + assertThat(new Solution().minTime(new int[] {1, 1, 1}, new int[] {1, 1, 1}), equalTo(5L)); + } + + @Test + void minTime3() { + assertThat(new Solution().minTime(new int[] {1, 2, 3, 4}, new int[] {1, 2}), equalTo(21L)); + } +} diff --git a/src/test/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/SolutionTest.java b/src/test/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/SolutionTest.java new file mode 100644 index 000000000..8b29d67b3 --- /dev/null +++ b/src/test/java/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero/SolutionTest.java @@ -0,0 +1,28 @@ +package g3401_3500.s3495_minimum_operations_to_make_array_elements_zero; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minOperations() { + assertThat(new Solution().minOperations(new int[][] {{1, 2}, {2, 4}}), equalTo(3L)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[][] {{2, 6}}), equalTo(4L)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[][] {{5, 8}}), equalTo(4L)); + } + + @Test + void minOperations4() { + assertThat(new Solution().minOperations(new int[][] {{1, 21}}), equalTo(23L)); + } +}