From a27f73d915ba9abadea6451968f3a4e6d330bfce Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 7 Dec 2024 21:41:52 +0200 Subject: [PATCH 1/6] Added tasks 3375-3378 --- .../Solution.java | 24 ++++++ .../readme.md | 52 +++++++++++++ .../Solution.java | 43 +++++++++++ .../readme.md | 61 +++++++++++++++ .../Solution.java | 56 ++++++++++++++ .../readme.md | 58 ++++++++++++++ .../Solution.java | 77 +++++++++++++++++++ .../readme.md | 44 +++++++++++ .../SolutionTest.java | 23 ++++++ .../SolutionTest.java | 19 +++++ .../SolutionTest.java | 23 ++++++ .../SolutionTest.java | 18 +++++ 12 files changed, 498 insertions(+) create mode 100644 src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java create mode 100644 src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md create mode 100644 src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md create mode 100644 src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java create mode 100644 src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md create mode 100644 src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java create mode 100644 src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md create mode 100644 src/test/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.java diff --git a/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java b/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java new file mode 100644 index 000000000..c00afb880 --- /dev/null +++ b/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java @@ -0,0 +1,24 @@ +package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k; + +// #Easy #2024_12_07_Time_3_ms_(100.00%)_Space_44.5_MB_(100.00%) + +import java.util.HashSet; +import java.util.Set; + +public class Solution { + public int minOperations(int[] nums, int k) { + Set s = new HashSet<>(); + for (int i : nums) { + s.add(i); + } + int res = 0; + for (int i : s) { + if (i > k) { + res++; + } else if (i < k) { + return -1; + } + } + return res; + } +} diff --git a/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md b/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md new file mode 100644 index 000000000..80852a760 --- /dev/null +++ b/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md @@ -0,0 +1,52 @@ +3375\. Minimum Operations to Make Array Values Equal to K + +Easy + +You are given an integer array `nums` and an integer `k`. + +An integer `h` is called **valid** if all values in the array that are **strictly greater** than `h` are _identical_. + +For example, if `nums = [10, 8, 10, 8]`, a **valid** integer is `h = 9` because all `nums[i] > 9` are equal to 10, but 5 is not a **valid** integer. + +You are allowed to perform the following operation on `nums`: + +* Select an integer `h` that is _valid_ for the **current** values in `nums`. +* For each index `i` where `nums[i] > h`, set `nums[i]` to `h`. + +Return the **minimum** number of operations required to make every element in `nums` **equal** to `k`. If it is impossible to make all elements equal to `k`, return -1. + +**Example 1:** + +**Input:** nums = [5,2,5,4,5], k = 2 + +**Output:** 2 + +**Explanation:** + +The operations can be performed in order using valid integers 4 and then 2. + +**Example 2:** + +**Input:** nums = [2,1,2], k = 2 + +**Output:** \-1 + +**Explanation:** + +It is impossible to make all the values equal to 2. + +**Example 3:** + +**Input:** nums = [9,7,5,3], k = 1 + +**Output:** 4 + +**Explanation:** + +The operations can be performed using valid integers in the order 7, 5, 3, and 1. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` +* `1 <= k <= 100` \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java b/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java new file mode 100644 index 000000000..ac94ab8ba --- /dev/null +++ b/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java @@ -0,0 +1,43 @@ +package g3301_3400.s3376_minimum_time_to_break_locks_i; + +// #Medium #2024_12_07_Time_760_ms_(100.00%)_Space_45_MB_(100.00%) + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Solution { + public int findMinimumTime(List strength, int k) { + List perm = new ArrayList<>(strength); + Collections.sort(perm); + int minTime = Integer.MAX_VALUE; + do { + int time = 0; + int factor = 1; + for (int required : perm) { + int neededTime = (required + factor - 1) / factor; + time += neededTime; + factor += k; + } + minTime = Math.min(minTime, time); + } while (nextPermutation(perm)); + return minTime; + } + + private boolean nextPermutation(List nums) { + int i = nums.size() - 2; + while (i >= 0 && nums.get(i) >= nums.get(i + 1)) { + i--; + } + if (i < 0) { + return false; + } + int j = nums.size() - 1; + while (nums.get(j) <= nums.get(i)) { + j--; + } + Collections.swap(nums, i, j); + Collections.reverse(nums.subList(i + 1, nums.size())); + return true; + } +} diff --git a/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md b/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md new file mode 100644 index 000000000..332149eea --- /dev/null +++ b/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md @@ -0,0 +1,61 @@ +3376\. Minimum Time to Break Locks I + +Medium + +Bob is stuck in a dungeon and must break `n` locks, each requiring some amount of **energy** to break. The required energy for each lock is stored in an array called `strength` where `strength[i]` indicates the energy needed to break the ith lock. + +To break a lock, Bob uses a sword with the following characteristics: + +* The initial energy of the sword is 0. +* The initial factor `X` by which the energy of the sword increases is 1. +* Every minute, the energy of the sword increases by the current factor `X`. +* To break the ith lock, the energy of the sword must reach **at least** `strength[i]`. +* After breaking a lock, the energy of the sword resets to 0, and the factor `X` increases by a given value `K`. + +Your task is to determine the **minimum** time in minutes required for Bob to break all `n` locks and escape the dungeon. + +Return the **minimum** time required for Bob to break all `n` locks. + +**Example 1:** + +**Input:** strength = [3,4,1], K = 1 + +**Output:** 4 + +**Explanation:** + +| Time | Energy | X | Action | Updated X | +|------|--------|---|----------------------|-----------| +| 0 | 0 | 1 | Nothing | 1 | +| 1 | 1 | 1 | Break 3rd Lock | 2 | +| 2 | 2 | 2 | Nothing | 2 | +| 3 | 4 | 2 | Break 2nd Lock | 3 | +| 4 | 3 | 3 | Break 1st Lock | 3 | + +The locks cannot be broken in less than 4 minutes; thus, the answer is 4. + +**Example 2:** + +**Input:** strength = [2,5,4], K = 2 + +**Output:** 5 + +**Explanation:** + +| Time | Energy | X | Action | Updated X | +|------|--------|---|----------------------|-----------| +| 0 | 0 | 1 | Nothing | 1 | +| 1 | 1 | 1 | Nothing | 1 | +| 2 | 2 | 1 | Break 1st Lock | 3 | +| 3 | 3 | 3 | Nothing | 3 | +| 4 | 6 | 3 | Break 2nd Lock | 5 | +| 5 | 5 | 5 | Break 3rd Lock | 7 | + +The locks cannot be broken in less than 5 minutes; thus, the answer is 5. + +**Constraints:** + +* `n == strength.length` +* `1 <= n <= 8` +* `1 <= K <= 10` +* 1 <= strength[i] <= 106 \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java b/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java new file mode 100644 index 000000000..efe57db30 --- /dev/null +++ b/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java @@ -0,0 +1,56 @@ +package g3301_3400.s3377_digit_operations_to_make_two_integers_equal; + +// #Medium #2024_12_07_Time_255_ms_(100.00%)_Space_44.9_MB_(100.00%) + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class Solution { + public int minOperations(int n, int m) { + int limit = 100000; + boolean[] sieve = new boolean[limit + 1]; + boolean[] visited = new boolean[limit]; + Arrays.fill(sieve, true); + sieve[0] = false; + sieve[1] = false; + for (int i = 2; i * i <= limit; i++) { + if (sieve[i]) { + for (int j = i * i; j <= limit; j += i) { + sieve[j] = false; + } + } + } + if (sieve[n]) { + return -1; + } + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + visited[n] = true; + pq.add(new int[] {n, n}); + while (!pq.isEmpty()) { + int[] current = pq.poll(); + int cost = current[0]; + int num = current[1]; + char[] temp = Integer.toString(num).toCharArray(); + if (num == m) { + return cost; + } + for (int j = 0; j < temp.length; j++) { + char old = temp[j]; + for (int i = -1; i <= 1; i++) { + int digit = old - '0'; + if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) { + continue; + } + temp[j] = (char) (i + digit + '0'); + int newnum = Integer.parseInt(new String(temp)); + if (!sieve[newnum] && !visited[newnum]) { + visited[newnum] = true; + pq.add(new int[] {cost + newnum, newnum}); + } + } + temp[j] = old; + } + } + return -1; + } +} diff --git a/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md b/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md new file mode 100644 index 000000000..bd6d91cc9 --- /dev/null +++ b/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md @@ -0,0 +1,58 @@ +3377\. Digit Operations to Make Two Integers Equal + +Medium + +You are given two integers `n` and `m` that consist of the **same** number of digits. + +You can perform the following operations **any** number of times: + +* Choose **any** digit from `n` that is not 9 and **increase** it by 1. +* Choose **any** digit from `n` that is not 0 and **decrease** it by 1. + +The integer `n` must not be a **prime** number at any point, including its original value and after each operation. + +The cost of a transformation is the sum of **all** values that `n` takes throughout the operations performed. + +Return the **minimum** cost to transform `n` into `m`. If it is impossible, return -1. + +A prime number is a natural number greater than 1 with only two factors, 1 and itself. + +**Example 1:** + +**Input:** n = 10, m = 12 + +**Output:** 85 + +**Explanation:** + +We perform the following operations: + +* Increase the first digit, now n = **2**0. +* Increase the second digit, now n = 2**1**. +* Increase the second digit, now n = 2**2**. +* Decrease the first digit, now n = **1**2. + +**Example 2:** + +**Input:** n = 4, m = 8 + +**Output:** \-1 + +**Explanation:** + +It is impossible to make `n` equal to `m`. + +**Example 3:** + +**Input:** n = 6, m = 2 + +**Output:** \-1 + +**Explanation:** + +Since 2 is already a prime, we can't make `n` equal to `m`. + +**Constraints:** + +* 1 <= n, m < 104 +* `n` and `m` consist of the same number of digits. \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java new file mode 100644 index 000000000..a0f9ffd5c --- /dev/null +++ b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java @@ -0,0 +1,77 @@ +package g3301_3400.s3378_count_connected_components_in_lcm_graph; + +// #Hard #2024_12_07_Time_48_ms_(100.00%)_Space_59.5_MB_(100.00%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + private static class Unionfind { + int[] parent; + int[] rank; + int totalComponents; + + public Unionfind(int n) { + parent = new int[n]; + rank = new int[n]; + totalComponents = n; + for (int i = 0; i < n; i++) { + parent[i] = i; + } + } + + public int find(int u) { + if (parent[u] == u) { + return u; + } + return parent[u] = find(parent[u]); + } + + public void union(int u, int v) { + int parentU = find(u); + int parentV = find(v); + + if (parentU != parentV) { + totalComponents--; + if (rank[parentU] == rank[parentV]) { + parent[parentV] = parentU; + rank[parentU]++; + } else if (rank[parentU] > rank[parentV]) { + parent[parentV] = parentU; + } else { + parent[parentU] = parentV; + } + } + } + } + + public int countComponents(int[] nums, int threshold) { + List goodNums = new ArrayList<>(); + int totalNums = nums.length; + for (int num : nums) { + if (num <= threshold) { + goodNums.add(num); + } + } + if (goodNums.isEmpty()) { + return totalNums; + } + Unionfind uf = new Unionfind(goodNums.size()); + int[] presentElements = new int[threshold + 1]; + Arrays.fill(presentElements, -1); + for (int i = 0; i < goodNums.size(); i++) { + presentElements[goodNums.get(i)] = i; + } + for (int d : goodNums) { + for (int i = d; i <= threshold; i += d) { + if (presentElements[i] == -1) { + presentElements[i] = presentElements[d]; + } else if (presentElements[i] != presentElements[d]) { + uf.union(presentElements[i], presentElements[d]); + } + } + } + return uf.totalComponents + totalNums - goodNums.size(); + } +} diff --git a/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md new file mode 100644 index 000000000..a2103761b --- /dev/null +++ b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md @@ -0,0 +1,44 @@ +3378\. Count Connected Components in LCM Graph + +Hard + +You are given an array of integers `nums` of size `n` and a **positive** integer `threshold`. + +There is a graph consisting of `n` nodes with the ith node having a value of `nums[i]`. Two nodes `i` and `j` in the graph are connected via an **undirected** edge if `lcm(nums[i], nums[j]) <= threshold`. + +Return the number of **connected components** in this graph. + +A **connected component** is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph. + +The term `lcm(a, b)` denotes the **least common multiple** of `a` and `b`. + +**Example 1:** + +**Input:** nums = [2,4,8,3,9], threshold = 5 + +**Output:** 4 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/31/example0.png) + +The four connected components are `(2, 4)`, `(3)`, `(8)`, `(9)`. + +**Example 2:** + +**Input:** nums = [2,4,8,3,9,12], threshold = 10 + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/31/example1.png) + +The two connected components are `(2, 3, 4, 8, 9)`, and `(12)`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* All elements of `nums` are unique. +* 1 <= threshold <= 2 * 105 \ No newline at end of file diff --git a/src/test/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.java b/src/test/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.java new file mode 100644 index 000000000..63b297c75 --- /dev/null +++ b/src/test/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k; + +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[] {5, 2, 5, 4, 5}, 2), equalTo(2)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {2, 1, 2}, 2), equalTo(-1)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {9, 7, 5, 3}, 1), equalTo(4)); + } +} diff --git a/src/test/java/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.java b/src/test/java/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.java new file mode 100644 index 000000000..2264a7715 --- /dev/null +++ b/src/test/java/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.java @@ -0,0 +1,19 @@ +package g3301_3400.s3376_minimum_time_to_break_locks_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findMinimumTime() { + assertThat(new Solution().findMinimumTime(List.of(3, 4, 1), 1), equalTo(4)); + } + + @Test + void findMinimumTime2() { + assertThat(new Solution().findMinimumTime(List.of(2, 5, 4), 2), equalTo(5)); + } +} diff --git a/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java b/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java new file mode 100644 index 000000000..995a4a021 --- /dev/null +++ b/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3377_digit_operations_to_make_two_integers_equal; + +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(10, 12), equalTo(85)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(4, 8), equalTo(-1)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(6, 2), equalTo(-1)); + } +} diff --git a/src/test/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.java b/src/test/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.java new file mode 100644 index 000000000..87e0f14cb --- /dev/null +++ b/src/test/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3378_count_connected_components_in_lcm_graph; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countComponents() { + assertThat(new Solution().countComponents(new int[] {2, 4, 8, 3, 9}, 5), equalTo(4)); + } + + @Test + void countComponents2() { + assertThat(new Solution().countComponents(new int[] {2, 4, 8, 3, 9, 12}, 10), equalTo(2)); + } +} From 631cee4523fd7f852199ca655c7c941c11411c73 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 7 Dec 2024 22:15:06 +0200 Subject: [PATCH 2/6] Update Solution.java --- .../Solution.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java index a0f9ffd5c..4378f6500 100644 --- a/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java +++ b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java @@ -25,13 +25,13 @@ public int find(int u) { if (parent[u] == u) { return u; } - return parent[u] = find(parent[u]); + parent[u] = find(parent[u]); + return parent[u]; } public void union(int u, int v) { int parentU = find(u); int parentV = find(v); - if (parentU != parentV) { totalComponents--; if (rank[parentU] == rank[parentV]) { From e91619b78cd478d15ab048b85038fcfb915c8a30 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 8 Dec 2024 09:01:33 +0200 Subject: [PATCH 3/6] Added test --- .../SolutionTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java b/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java index 995a4a021..6b8540520 100644 --- a/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java +++ b/src/test/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.java @@ -20,4 +20,9 @@ void minOperations2() { void minOperations3() { assertThat(new Solution().minOperations(6, 2), equalTo(-1)); } + + @Test + void minOperations4() { + assertThat(new Solution().minOperations(17, 72), equalTo(-1)); + } } From 725bb070b1af486e2a0793367f7838b2828d9094 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 10 Dec 2024 03:24:04 +0200 Subject: [PATCH 4/6] Added tasks 3379-3382 --- .../Solution.java | 2 +- .../Solution.java | 66 +++++++++++-------- .../Solution.java | 3 +- .../Solution.java | 3 +- .../s3379_transformed_array/Solution.java | 22 +++++++ .../s3379_transformed_array/readme.md | 45 +++++++++++++ .../Solution.java | 49 ++++++++++++++ .../readme.md | 56 ++++++++++++++++ .../Solution.java | 24 +++++++ .../readme.md | 44 +++++++++++++ .../Solution.java | 66 +++++++++++++++++++ .../readme.md | 55 ++++++++++++++++ .../s3379_transformed_array/SolutionTest.java | 22 +++++++ .../SolutionTest.java | 32 +++++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 32 +++++++++ 16 files changed, 513 insertions(+), 31 deletions(-) create mode 100644 src/main/java/g3301_3400/s3379_transformed_array/Solution.java create mode 100644 src/main/java/g3301_3400/s3379_transformed_array/readme.md create mode 100644 src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md create mode 100644 src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.java create mode 100644 src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md create mode 100644 src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java create mode 100644 src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md create mode 100644 src/test/java/g3301_3400/s3379_transformed_array/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.java diff --git a/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java b/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java index c00afb880..bcfd98dd1 100644 --- a/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java +++ b/src/main/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java @@ -1,6 +1,6 @@ package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k; -// #Easy #2024_12_07_Time_3_ms_(100.00%)_Space_44.5_MB_(100.00%) +// #Easy #Array #Hash_Table #2024_12_10_Time_3_ms_(78.92%)_Space_44.6_MB_(67.39%) import java.util.HashSet; import java.util.Set; diff --git a/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java b/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java index ac94ab8ba..dc6e69786 100644 --- a/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java +++ b/src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java @@ -1,6 +1,7 @@ package g3301_3400.s3376_minimum_time_to_break_locks_i; -// #Medium #2024_12_07_Time_760_ms_(100.00%)_Space_45_MB_(100.00%) +// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask +// #2024_12_10_Time_3_ms_(99.63%)_Space_42_MB_(92.34%) import java.util.ArrayList; import java.util.Collections; @@ -8,36 +9,45 @@ public class Solution { public int findMinimumTime(List strength, int k) { - List perm = new ArrayList<>(strength); - Collections.sort(perm); - int minTime = Integer.MAX_VALUE; - do { - int time = 0; - int factor = 1; - for (int required : perm) { - int neededTime = (required + factor - 1) / factor; - time += neededTime; - factor += k; + List strengthLocal = new ArrayList<>(strength); + Collections.sort(strengthLocal); + int res = strengthLocal.get(0); + strengthLocal.remove(0); + int x = 1; + while (!strengthLocal.isEmpty()) { + x += k; + int nextTime = (strengthLocal.get(0) - 1) / x + 1; + int canBreak = nextTime * x; + int indexRemove = findIndex(strengthLocal, canBreak); + if (strengthLocal.size() > 1) { + int nextTime1 = (strengthLocal.get(1) - 1) / x + 1; + int canBreak1 = nextTime1 * x; + int indexRemove1 = findIndex(strengthLocal, canBreak1); + if (nextTime1 + (strengthLocal.get(0) - 1) / (x + k) + < nextTime + (strengthLocal.get(1) - 1) / (x + k)) { + nextTime = nextTime1; + indexRemove = indexRemove1; + } } - minTime = Math.min(minTime, time); - } while (nextPermutation(perm)); - return minTime; + res += nextTime; + strengthLocal.remove(indexRemove); + } + return res; } - private boolean nextPermutation(List nums) { - int i = nums.size() - 2; - while (i >= 0 && nums.get(i) >= nums.get(i + 1)) { - i--; - } - if (i < 0) { - return false; - } - int j = nums.size() - 1; - while (nums.get(j) <= nums.get(i)) { - j--; + private int findIndex(List strength, int canBreak) { + int l = 0; + int r = strength.size() - 1; + int res = -1; + while (l <= r) { + int mid = (l + r) / 2; + if (strength.get(mid) <= canBreak) { + res = mid; + l = mid + 1; + } else { + r = mid - 1; + } } - Collections.swap(nums, i, j); - Collections.reverse(nums.subList(i + 1, nums.size())); - return true; + return res; } } diff --git a/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java b/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java index efe57db30..f87977e5b 100644 --- a/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java +++ b/src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java @@ -1,6 +1,7 @@ package g3301_3400.s3377_digit_operations_to_make_two_integers_equal; -// #Medium #2024_12_07_Time_255_ms_(100.00%)_Space_44.9_MB_(100.00%) +// #Medium #Math #Heap_Priority_Queue #Graph #Shortest_Path #Number_Theory +// #2024_12_10_Time_246_ms_(38.59%)_Space_45.2_MB_(73.52%) import java.util.Arrays; import java.util.PriorityQueue; diff --git a/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java index 4378f6500..e8c5e7949 100644 --- a/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java +++ b/src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java @@ -1,6 +1,7 @@ package g3301_3400.s3378_count_connected_components_in_lcm_graph; -// #Hard #2024_12_07_Time_48_ms_(100.00%)_Space_59.5_MB_(100.00%) +// #Hard #Array #Hash_Table #Math #Union_Find #Number_Theory +// #2024_12_10_Time_68_ms_(67.83%)_Space_59.8_MB_(62.24%) import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/g3301_3400/s3379_transformed_array/Solution.java b/src/main/java/g3301_3400/s3379_transformed_array/Solution.java new file mode 100644 index 000000000..224eaba3a --- /dev/null +++ b/src/main/java/g3301_3400/s3379_transformed_array/Solution.java @@ -0,0 +1,22 @@ +package g3301_3400.s3379_transformed_array; + +// #Easy #Array #Simulation #2024_12_10_Time_1_ms_(99.87%)_Space_44.9_MB_(75.08%) + +public class Solution { + public int[] constructTransformedArray(int[] nums) { + int n = nums.length; + int[] res = new int[n]; + for (int i = 0; i < n; i++) { + if (nums[i] == 0) { + res[i] = nums[i]; + } + if (nums[i] > 0) { + res[i] = nums[(i + nums[i]) % n]; + } else if (nums[i] < 0) { + int r = (Math.abs(nums[i])) / n; + res[i] = nums[Math.abs((i + nums[i] + r * n + n)) % n]; + } + } + return res; + } +} diff --git a/src/main/java/g3301_3400/s3379_transformed_array/readme.md b/src/main/java/g3301_3400/s3379_transformed_array/readme.md new file mode 100644 index 000000000..f2e5ad44a --- /dev/null +++ b/src/main/java/g3301_3400/s3379_transformed_array/readme.md @@ -0,0 +1,45 @@ +3379\. Transformed Array + +Easy + +You are given an integer array `nums` that represents a circular array. Your task is to create a new array `result` of the **same** size, following these rules: + +For each index `i` (where `0 <= i < nums.length`), perform the following **independent** actions: + +* If `nums[i] > 0`: Start at index `i` and move `nums[i]` steps to the **right** in the circular array. Set `result[i]` to the value of the index where you land. +* If `nums[i] < 0`: Start at index `i` and move `abs(nums[i])` steps to the **left** in the circular array. Set `result[i]` to the value of the index where you land. +* If `nums[i] == 0`: Set `result[i]` to `nums[i]`. + +Return the new array `result`. + +**Note:** Since `nums` is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end. + +**Example 1:** + +**Input:** nums = [3,-2,1,1] + +**Output:** [1,1,1,3] + +**Explanation:** + +* For `nums[0]` that is equal to 3, If we move 3 steps to right, we reach `nums[3]`. So `result[0]` should be 1. +* For `nums[1]` that is equal to -2, If we move 2 steps to left, we reach `nums[3]`. So `result[1]` should be 1. +* For `nums[2]` that is equal to 1, If we move 1 step to right, we reach `nums[3]`. So `result[2]` should be 1. +* For `nums[3]` that is equal to 1, If we move 1 step to right, we reach `nums[0]`. So `result[3]` should be 3. + +**Example 2:** + +**Input:** nums = [-1,4,-1] + +**Output:** [-1,-1,4] + +**Explanation:** + +* For `nums[0]` that is equal to -1, If we move 1 step to left, we reach `nums[2]`. So `result[0]` should be -1. +* For `nums[1]` that is equal to 4, If we move 4 steps to right, we reach `nums[2]`. So `result[1]` should be -1. +* For `nums[2]` that is equal to -1, If we move 1 step to left, we reach `nums[1]`. So `result[2]` should be 4. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.java b/src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.java new file mode 100644 index 000000000..7e99ea808 --- /dev/null +++ b/src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.java @@ -0,0 +1,49 @@ +package g3301_3400.s3380_maximum_area_rectangle_with_point_constraints_i; + +// #Medium #Array #Math #Sorting #Enumeration #Geometry #Segment_Tree #Binary_Indexed_Tree +// #2024_12_10_Time_8_ms_(81.05%)_Space_45_MB_(66.32%) + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Solution { + public int maxRectangleArea(int[][] points) { + Set set = new HashSet<>(); + for (int[] p : points) { + set.add(Arrays.toString(p)); + } + int maxArea = -1; + for (int[] point : points) { + for (int j = 1; j < points.length; j++) { + int[] p2 = points[j]; + if (point[0] == p2[0] + || point[1] == p2[1] + || !set.contains(Arrays.toString(new int[] {point[0], p2[1]})) + || !set.contains(Arrays.toString(new int[] {p2[0], point[1]})) + || !validate(points, point, p2)) { + continue; + } + maxArea = Math.max(maxArea, (p2[1] - point[1]) * (p2[0] - point[0])); + } + } + return maxArea; + } + + private boolean validate(int[][] points, int[] p1, int[] p2) { + int top = Math.max(p1[1], p2[1]); + int bot = Math.min(p1[1], p2[1]); + int left = Math.min(p1[0], p2[0]); + int right = Math.max(p1[0], p2[0]); + for (int[] p : points) { + int x = p[0]; + int y = p[1]; + if ((y == top || y == bot) && x > left && x < right + || (x == left || x == right) && y > bot && y < top + || (x > left && x < right && y > bot && y < top)) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md b/src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md new file mode 100644 index 000000000..8c15f8115 --- /dev/null +++ b/src/main/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md @@ -0,0 +1,56 @@ +3380\. Maximum Area Rectangle With Point Constraints I + +Medium + +You are given an array `points` where points[i] = [xi, yi] represents the coordinates of a point on an infinite plane. + +Your task is to find the **maximum** area of a rectangle that: + +* Can be formed using **four** of these points as its corners. +* Does **not** contain any other point inside or on its border. +* Has its edges **parallel** to the axes. + +Return the **maximum area** that you can obtain or -1 if no such rectangle is possible. + +**Example 1:** + +**Input:** points = [[1,1],[1,3],[3,1],[3,3]] + +**Output:** 4 + +**Explanation:** + +**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)** + +We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4. + +**Example 2:** + +**Input:** points = [[1,1],[1,3],[3,1],[3,3],[2,2]] + +**Output:** \-1 + +**Explanation:** + +**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)** + +There is only one rectangle possible is with points `[1,1], [1,3], [3,1]` and `[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1. + +**Example 3:** + +**Input:** points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]] + +**Output:** 2 + +**Explanation:** + +**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)** + +The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2], [3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1], [3,2]` also form a valid rectangle with the same area. + +**Constraints:** + +* `1 <= points.length <= 10` +* `points[i].length == 2` +* 0 <= xi, yi <= 100 +* All the given points are **unique**. \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.java b/src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.java new file mode 100644 index 000000000..60ba8272b --- /dev/null +++ b/src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.java @@ -0,0 +1,24 @@ +package g3301_3400.s3381_maximum_subarray_sum_with_length_divisible_by_k; + +// #Medium #Array #Hash_Table #Prefix_Sum #2024_12_10_Time_4_ms_(100.00%)_Space_80.2_MB_(22.05%) + +public class Solution { + public long maxSubarraySum(int[] nums, int k) { + int n = nums.length; + long[] maxSum = new long[n]; + long minSum = 0; + for (int i = n - 1; i > n - k; i--) { + maxSum[i] = Integer.MIN_VALUE; + minSum += nums[i]; + } + minSum += nums[n - k]; + maxSum[n - k] = minSum; + long ans = minSum; + for (int i = n - k - 1; i >= 0; i--) { + minSum = minSum + nums[i] - nums[i + k]; + maxSum[i] = Math.max(minSum, minSum + maxSum[i + k]); + ans = Math.max(maxSum[i], ans); + } + return ans; + } +} diff --git a/src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md b/src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md new file mode 100644 index 000000000..e862ba6cd --- /dev/null +++ b/src/main/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md @@ -0,0 +1,44 @@ +3381\. Maximum Subarray Sum With Length Divisible by K + +Medium + +You are given an array of integers `nums` and an integer `k`. + +Return the **maximum** sum of a **non-empty subarray** of `nums`, such that the size of the subarray is **divisible** by `k`. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [1,2], k = 1 + +**Output:** 3 + +**Explanation:** + +The subarray `[1, 2]` with sum 3 has length equal to 2 which is divisible by 1. + +**Example 2:** + +**Input:** nums = [-1,-2,-3,-4,-5], k = 4 + +**Output:** \-10 + +**Explanation:** + +The maximum sum subarray is `[-1, -2, -3, -4]` which has length equal to 4 which is divisible by 4. + +**Example 3:** + +**Input:** nums = [-5,1,2,-3,4], k = 2 + +**Output:** 4 + +**Explanation:** + +The maximum sum subarray is `[1, 2, -3, 4]` which has length equal to 4 which is divisible by 2. + +**Constraints:** + +* 1 <= k <= nums.length <= 2 * 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java b/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java new file mode 100644 index 000000000..d27792cc2 --- /dev/null +++ b/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java @@ -0,0 +1,66 @@ +package g3301_3400.s3382_maximum_area_rectangle_with_point_constraints_ii; + +// #Hard #Array #Math #Sorting #Geometry #Segment_Tree #Binary_Indexed_Tree +// #2024_12_10_Time_320_ms_(95.35%)_Space_83.2_MB_(86.05%) + +import java.util.Arrays; +import java.util.HashMap; +import java.util.TreeSet; + +public class Solution { + public long maxRectangleArea(int[] xCoord, int[] yCoord) { + if (xCoord.length < 4) { + return -1; + } + Pair[] pair = new Pair[xCoord.length]; + for (int i = 0; i < xCoord.length; ++i) { + int x0 = xCoord[i]; + int y0 = yCoord[i]; + pair[i] = new Pair(x0, y0); + } + Arrays.sort(pair); + HashMap map = new HashMap<>(); + TreeSet yVals = new TreeSet<>(); + long best = -1; + for (int i = 0; i < pair.length - 1; ++i) { + if (!yVals.isEmpty()) { + int y0 = pair[i].y; + Integer y1 = yVals.floor(y0); + while (y1 != null) { + Pair p1 = map.get(y1); + if (p1.y < y0) { + break; + } + if (y1 == y0 && pair[i + 1].x == pair[i].x && pair[i + 1].y == p1.y) { + long dY = p1.y - y0; + long dX = pair[i].x - p1.x; + best = Math.max(dY * dX, best); + } + if (p1.x != pair[i].x) { + yVals.remove(y1); + } + y1 = yVals.lower(y1); + } + } + if (pair[i].x == pair[i + 1].x) { + yVals.add(pair[i].y); + map.put(pair[i].y, pair[i + 1]); + } + } + return best; + } + + private static class Pair implements Comparable { + private final int x; + private final int y; + + public Pair(int xx, int yy) { + x = xx; + y = yy; + } + + public int compareTo(Pair p) { + return (x == p.x) ? y - p.y : x - p.x; + } + } +} diff --git a/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md b/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md new file mode 100644 index 000000000..69de05a07 --- /dev/null +++ b/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md @@ -0,0 +1,55 @@ +3382\. Maximum Area Rectangle With Point Constraints II + +Hard + +There are n points on an infinite plane. You are given two integer arrays `xCoord` and `yCoord` where `(xCoord[i], yCoord[i])` represents the coordinates of the ith point. + +Your task is to find the **maximum** area of a rectangle that: + +* Can be formed using **four** of these points as its corners. +* Does **not** contain any other point inside or on its border. +* Has its edges **parallel** to the axes. + +Return the **maximum area** that you can obtain or -1 if no such rectangle is possible. + +**Example 1:** + +**Input:** xCoord = [1,1,3,3], yCoord = [1,3,1,3] + +**Output:** 4 + +**Explanation:** + +**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)** + +We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4. + +**Example 2:** + +**Input:** xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,2] + +**Output:** \-1 + +**Explanation:** + +**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)** + +There is only one rectangle possible is with points `[1,1], [1,3], [3,1]` and `[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1. + +**Example 3:** + +**Input:** xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,2] + +**Output:** 2 + +**Explanation:** + +**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)** + +The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2], [3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1], [3,2]` also form a valid rectangle with the same area. + +**Constraints:** + +* 1 <= xCoord.length == yCoord.length <= 2 * 105 +* 0 <= xCoord[i], yCoord[i] <= 8 * 107 +* All the given points are **unique**. \ No newline at end of file diff --git a/src/test/java/g3301_3400/s3379_transformed_array/SolutionTest.java b/src/test/java/g3301_3400/s3379_transformed_array/SolutionTest.java new file mode 100644 index 000000000..8c6670434 --- /dev/null +++ b/src/test/java/g3301_3400/s3379_transformed_array/SolutionTest.java @@ -0,0 +1,22 @@ +package g3301_3400.s3379_transformed_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void constructTransformedArray() { + assertThat( + new Solution().constructTransformedArray(new int[] {3, -2, 1, 1}), + equalTo(new int[] {1, 1, 1, 3})); + } + + @Test + void constructTransformedArray2() { + assertThat( + new Solution().constructTransformedArray(new int[] {-1, 4, -1}), + equalTo(new int[] {-1, -1, 4})); + } +} diff --git a/src/test/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.java b/src/test/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.java new file mode 100644 index 000000000..8c3fe26b8 --- /dev/null +++ b/src/test/java/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.java @@ -0,0 +1,32 @@ +package g3301_3400.s3380_maximum_area_rectangle_with_point_constraints_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxRectangleArea() { + assertThat( + new Solution().maxRectangleArea(new int[][] {{1, 1}, {1, 3}, {3, 1}, {3, 3}}), + equalTo(4)); + } + + @Test + void maxRectangleArea2() { + assertThat( + new Solution() + .maxRectangleArea(new int[][] {{1, 1}, {1, 3}, {3, 1}, {3, 3}, {2, 2}}), + equalTo(-1)); + } + + @Test + void maxRectangleArea3() { + assertThat( + new Solution() + .maxRectangleArea( + new int[][] {{1, 1}, {1, 3}, {3, 1}, {3, 3}, {1, 2}, {3, 2}}), + equalTo(2)); + } +} diff --git a/src/test/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.java b/src/test/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.java new file mode 100644 index 000000000..c20a54b59 --- /dev/null +++ b/src/test/java/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3381_maximum_subarray_sum_with_length_divisible_by_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxSubarraySum() { + assertThat(new Solution().maxSubarraySum(new int[] {1, 2}, 1), equalTo(3L)); + } + + @Test + void maxSubarraySum2() { + assertThat(new Solution().maxSubarraySum(new int[] {-1, -2, -3, -4, -5}, 4), equalTo(-10L)); + } + + @Test + void maxSubarraySum3() { + assertThat(new Solution().maxSubarraySum(new int[] {-5, 1, 2, -3, 4}, 2), equalTo(4L)); + } +} diff --git a/src/test/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.java b/src/test/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.java new file mode 100644 index 000000000..00350630a --- /dev/null +++ b/src/test/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.java @@ -0,0 +1,32 @@ +package g3301_3400.s3382_maximum_area_rectangle_with_point_constraints_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxRectangleArea() { + assertThat( + new Solution().maxRectangleArea(new int[] {1, 1, 3, 3}, new int[] {1, 3, 1, 3}), + equalTo(4L)); + } + + @Test + void maxRectangleArea2() { + assertThat( + new Solution() + .maxRectangleArea(new int[] {1, 1, 3, 3, 2}, new int[] {1, 3, 1, 3, 2}), + equalTo(-1L)); + } + + @Test + void maxRectangleArea3() { + assertThat( + new Solution() + .maxRectangleArea( + new int[] {1, 1, 3, 3, 1, 3}, new int[] {1, 3, 1, 3, 2, 2}), + equalTo(2L)); + } +} From 8706eb8e96c060a52cc860d2d547a6de901f180c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 10 Dec 2024 03:32:48 +0200 Subject: [PATCH 5/6] Fixed sonar --- .../Solution.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java b/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java index d27792cc2..2904bfcf6 100644 --- a/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java +++ b/src/main/java/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.java @@ -32,8 +32,8 @@ public long maxRectangleArea(int[] xCoord, int[] yCoord) { break; } if (y1 == y0 && pair[i + 1].x == pair[i].x && pair[i + 1].y == p1.y) { - long dY = p1.y - y0; - long dX = pair[i].x - p1.x; + long dY = p1.y - (long) y0; + long dX = pair[i].x - (long) p1.x; best = Math.max(dY * dX, best); } if (p1.x != pair[i].x) { @@ -50,6 +50,7 @@ public long maxRectangleArea(int[] xCoord, int[] yCoord) { return best; } + @SuppressWarnings("java:S1210") private static class Pair implements Comparable { private final int x; private final int y; From ccfd3696b5d3efcab925a95edbb59f6d039cbaef Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 10 Dec 2024 03:37:22 +0200 Subject: [PATCH 6/6] Improved solution --- src/main/java/g3301_3400/s3379_transformed_array/Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g3301_3400/s3379_transformed_array/Solution.java b/src/main/java/g3301_3400/s3379_transformed_array/Solution.java index 224eaba3a..d46270283 100644 --- a/src/main/java/g3301_3400/s3379_transformed_array/Solution.java +++ b/src/main/java/g3301_3400/s3379_transformed_array/Solution.java @@ -7,9 +7,6 @@ public int[] constructTransformedArray(int[] nums) { int n = nums.length; int[] res = new int[n]; for (int i = 0; i < n; i++) { - if (nums[i] == 0) { - res[i] = nums[i]; - } if (nums[i] > 0) { res[i] = nums[(i + nums[i]) % n]; } else if (nums[i] < 0) {