From 5da17bafbc59c98022f45e689fb6cefb93e6e8a2 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:10:19 +0300 Subject: [PATCH 1/7] Added tasks 3065-3069 --- .../Solution.java | 15 +++++ .../readme.md | 48 ++++++++++++++ .../Solution.java | 43 ++++++++++++ .../readme.md | 54 +++++++++++++++ .../Solution.java | 53 +++++++++++++++ .../readme.md | 51 +++++++++++++++ .../Solution.java | 18 +++++ .../readme.md | 65 +++++++++++++++++++ .../Solution.java | 31 +++++++++ .../readme.md | 49 ++++++++++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 18 +++++ .../SolutionTest.java | 32 +++++++++ .../SolutionTest.java | 34 ++++++++++ .../SolutionTest.java | 20 ++++++ 15 files changed, 554 insertions(+) create mode 100644 src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/Solution.java create mode 100644 src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/readme.md create mode 100644 src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java create mode 100644 src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/readme.md create mode 100644 src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java create mode 100644 src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/readme.md create mode 100644 src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java create mode 100644 src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md create mode 100644 src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java create mode 100644 src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/readme.md create mode 100644 src/test/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/SolutionTest.java diff --git a/src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/Solution.java b/src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/Solution.java new file mode 100644 index 000000000..04173e3c1 --- /dev/null +++ b/src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/Solution.java @@ -0,0 +1,15 @@ +package g3001_3100.s3065_minimum_operations_to_exceed_threshold_value_i; + +// #Easy #Array #2024_03_31_Time_0_ms_(100.00%)_Space_42.7_MB_(48.42%) + +public class Solution { + public int minOperations(int[] nums, int k) { + int count = 0; + for (int num : nums) { + if (num >= k) { + count++; + } + } + return nums.length - count; + } +} diff --git a/src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/readme.md b/src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/readme.md new file mode 100644 index 000000000..e4ecaf1d1 --- /dev/null +++ b/src/main/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/readme.md @@ -0,0 +1,48 @@ +3065\. Minimum Operations to Exceed Threshold Value I + +Easy + +You are given a **0-indexed** integer array `nums`, and an integer `k`. + +In one operation, you can remove one occurrence of the smallest element of `nums`. + +Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`. + +**Example 1:** + +**Input:** nums = [2,11,10,1,3], k = 10 + +**Output:** 3 + +**Explanation:** After one operation, nums becomes equal to [2, 11, 10, 3]. + +After two operations, nums becomes equal to [11, 10, 3]. + +After three operations, nums becomes equal to [11, 10]. + +At this stage, all the elements of nums are greater than or equal to 10 so we can stop. + +It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10. + +**Example 2:** + +**Input:** nums = [1,1,2,4,9], k = 1 + +**Output:** 0 + +**Explanation:** All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums. + +**Example 3:** + +**Input:** nums = [1,1,2,4,9], k = 9 + +**Output:** 4 + +**Explanation:** only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums. + +**Constraints:** + +* `1 <= nums.length <= 50` +* 1 <= nums[i] <= 109 +* 1 <= k <= 109 +* The input is generated such that there is at least one index `i` such that `nums[i] >= k`. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java new file mode 100644 index 000000000..634100fbb --- /dev/null +++ b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java @@ -0,0 +1,43 @@ +package g3001_3100.s3066_minimum_operations_to_exceed_threshold_value_ii; + +// #Medium #Array #Heap_Priority_Queue #Simulation +// #2024_03_31_Time_26_ms_(99.91%)_Space_65.7_MB_(97.28%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + public int minOperations(int[] nums, int k) { + int n = nums.length; + int steps = 0; + Arrays.sort(nums); + List extra = new ArrayList<>(); + boolean increase = true; + int i = 0; + int j = 0; + while (increase) { + if ((i >= n || nums[i] >= k) && (j >= extra.size() || extra.get(j) >= k)) { + increase = false; + break; + } + int min = -1, max = -1; + if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) { + min = nums[i++]; + } else { + min = extra.get(j++); + } + if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) { + max = nums[i++]; + } else { + max = extra.get(j++); + } + steps++; + long res = min; + res = 2 * res + max; + if (res > Integer.MAX_VALUE) extra.add(Integer.MAX_VALUE); + else extra.add((int) res); + } + return steps; + } +} diff --git a/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/readme.md b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/readme.md new file mode 100644 index 000000000..f8f651e34 --- /dev/null +++ b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/readme.md @@ -0,0 +1,54 @@ +3066\. Minimum Operations to Exceed Threshold Value II + +Medium + +You are given a **0-indexed** integer array `nums`, and an integer `k`. + +In one operation, you will: + +* Take the two smallest integers `x` and `y` in `nums`. +* Remove `x` and `y` from `nums`. +* Add `min(x, y) * 2 + max(x, y)` anywhere in the array. + +**Note** that you can only apply the described operation if `nums` contains at least two elements. + +Return _the **minimum** number of operations needed so that all elements of the array are greater than or equal to_ `k`. + +**Example 1:** + +**Input:** nums = [2,11,10,1,3], k = 10 + +**Output:** 2 + +**Explanation:** In the first operation, we remove elements 1 and 2, then add 1 \* 2 + 2 to nums. nums becomes equal to [4, 11, 10, 3]. + +In the second operation, we remove elements 3 and 4, then add 3 \* 2 + 4 to nums. nums becomes equal to [10, 11, 10]. + +At this stage, all the elements of nums are greater than or equal to 10 so we can stop. + +It can be shown that 2 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10. + +**Example 2:** + +**Input:** nums = [1,1,2,4,9], k = 20 + +**Output:** 4 + +**Explanation:** After one operation, nums becomes equal to [2, 4, 9, 3]. + +After two operations, nums becomes equal to [7, 4, 9]. + +After three operations, nums becomes equal to [15, 9]. + +After four operations, nums becomes equal to [33]. + +At this stage, all the elements of nums are greater than 20 so we can stop. + +It can be shown that 4 is the minimum number of operations needed so that all elements of the array are greater than or equal to 20. + +**Constraints:** + +* 2 <= nums.length <= 2 * 105 +* 1 <= nums[i] <= 109 +* 1 <= k <= 109 +* The input is generated such that an answer always exists. That is, there exists some sequence of operations after which all elements of the array are greater than or equal to `k`. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java new file mode 100644 index 000000000..2efdc5857 --- /dev/null +++ b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java @@ -0,0 +1,53 @@ +package g3001_3100.s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network; + +// #Medium #Array #Tree #Depth_First_Search #2024_03_31_Time_69_ms_(99.83%)_Space_45.5_MB_(81.49%) + +import java.util.ArrayList; + +public class Solution { + private ArrayList adj[]; + + public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) { + int n = edges.length + 1; + adj = new ArrayList[n]; + for (int i = 0; i < n; i++) { + adj[i] = new ArrayList<>(); + } + for (int edge[] : edges) { + int u = edge[0], v = edge[1], w = edge[2]; + adj[u].add(v); + adj[v].add(u); + adj[u].add(w); + adj[v].add(w); + } + int res[] = new int[n]; + for (int i = 0; i < n; i++) { + if (adj[i].size() > 2) { + ArrayList al = new ArrayList<>(); + for (int j = 0; j < adj[i].size(); j += 2) { + int cnt[] = new int[1]; + dfs(adj[i].get(j), i, adj[i].get(j + 1), cnt, signalSpeed); + al.add(cnt[0]); + } + int sum = 0; + for (int j : al) { + res[i] += (sum * j); + sum += j; + } + } + } + return res; + } + + void dfs(int node, int par, int sum, int cnt[], int ss) { + if (sum % ss == 0) { + cnt[0]++; + } + for (int i = 0; i < adj[node].size(); i += 2) { + int child = adj[node].get(i); + if (child != par) { + dfs(child, node, sum + adj[node].get(i + 1), cnt, ss); + } + } + } +} diff --git a/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/readme.md b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/readme.md new file mode 100644 index 000000000..d4b289555 --- /dev/null +++ b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/readme.md @@ -0,0 +1,51 @@ +3067\. Count Pairs of Connectable Servers in a Weighted Tree Network + +Medium + +You are given an unrooted weighted tree with `n` vertices representing servers numbered from `0` to `n - 1`, an array `edges` where edges[i] = [ai, bi, weighti] represents a bidirectional edge between vertices ai and bi of weight weighti. You are also given an integer `signalSpeed`. + +Two servers `a` and `b` are **connectable** through a server `c` if: + +* `a < b`, `a != c` and `b != c`. +* The distance from `c` to `a` is divisible by `signalSpeed`. +* The distance from `c` to `b` is divisible by `signalSpeed`. +* The path from `c` to `b` and the path from `c` to `a` do not share any edges. + +Return _an integer array_ `count` _of length_ `n` _where_ `count[i]` _is the **number** of server pairs that are **connectable** through_ _the server_ `i`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/01/21/example22.png) + +**Input:** edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1 + +**Output:** [0,4,6,6,4,0] + +**Explanation:** Since signalSpeed is 1, count[c] is equal to the number of pairs of paths that start at c and do not share any edges. + +In the case of the given path graph, count[c] is equal to the number of servers to the left of c multiplied by the servers to the right of c. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/01/21/example11.png) + +**Input:** edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3 + +**Output:** [2,0,0,0,0,0,2] + +**Explanation:** Through server 0, there are 2 pairs of connectable servers: (4, 5) and (4, 6). + +Through server 6, there are 2 pairs of connectable servers: (4, 5) and (0, 5). + +It can be shown that no two servers are connectable through servers other than 0 and 6. + +**Constraints:** + +* `2 <= n <= 1000` +* `edges.length == n - 1` +* `edges[i].length == 3` +* 0 <= ai, bi < n +* edges[i] = [ai, bi, weighti] +* 1 <= weighti <= 106 +* 1 <= signalSpeed <= 106 +* The input is generated such that `edges` represents a valid tree. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java new file mode 100644 index 000000000..9df7e3f14 --- /dev/null +++ b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java @@ -0,0 +1,18 @@ +package g3001_3100.s3068_find_the_maximum_sum_of_node_values; + +// #Hard #Array #Dynamic_Programming #Sorting #Greedy #Tree #Bit_Manipulation +// #2024_03_31_Time_1_ms_(100.00%)_Space_54.5_MB_(67.07%) + +public class Solution { + public long maximumValueSum(int[] A, int k, int[][] edges) { + long res = 0; + int d = 1 << 30, c = 0; + for (int a : A) { + int b = a ^ k; + res += Math.max(a, b); + c ^= a < b ? 1 : 0; + d = Math.min(d, Math.abs(a - b)); + } + return res - d * c; + } +} diff --git a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md new file mode 100644 index 000000000..31e9d00e9 --- /dev/null +++ b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/readme.md @@ -0,0 +1,65 @@ +3068\. Find the Maximum Sum of Node Values + +Hard + +There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a **0-indexed** 2D integer array `edges` of length `n - 1`, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. You are also given a **positive** integer `k`, and a **0-indexed** array of **non-negative** integers `nums` of length `n`, where `nums[i]` represents the **value** of the node numbered `i`. + +Alice wants the sum of values of tree nodes to be **maximum**, for which Alice can perform the following operation **any** number of times (**including zero**) on the tree: + +* Choose any edge `[u, v]` connecting the nodes `u` and `v`, and update their values as follows: + * `nums[u] = nums[u] XOR k` + * `nums[v] = nums[v] XOR k` + +Return _the **maximum** possible **sum** of the **values** Alice can achieve by performing the operation **any** number of times_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png) + +**Input:** nums = [1,2,1], k = 3, edges = [[0,1],[0,2]] + +**Output:** 6 + +**Explanation:** Alice can achieve the maximum sum of 6 using a single operation: + +- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2]. + +The total sum of values is 2 + 2 + 2 = 6. + +It can be shown that 6 is the maximum achievable sum of values. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png) + +**Input:** nums = [2,3], k = 7, edges = [[0,1]] + +**Output:** 9 + +**Explanation:** Alice can achieve the maximum sum of 9 using a single operation: + +- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4]. + +The total sum of values is 5 + 4 = 9. + +It can be shown that 9 is the maximum achievable sum of values. + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png) + +**Input:** nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]] + +**Output:** 42 + +**Explanation:** The maximum achievable sum is 42 which can be achieved by Alice performing no operations. + +**Constraints:** + +* 2 <= n == nums.length <= 2 * 104 +* 1 <= k <= 109 +* 0 <= nums[i] <= 109 +* `edges.length == n - 1` +* `edges[i].length == 2` +* `0 <= edges[i][0], edges[i][1] <= n - 1` +* The input is generated such that `edges` represent a valid tree. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java b/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java new file mode 100644 index 000000000..cb624a83b --- /dev/null +++ b/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java @@ -0,0 +1,31 @@ +package g3001_3100.s3069_distribute_elements_into_two_arrays_i; + +// #Easy #Array #Simulation #2024_03_31_Time_0_ms_(100.00%)_Space_44.6_MB_(70.15%) + +public class Solution { + public int[] resultArray(int[] nums) { + int s = 0, t = 1; + for (int i = 2; i < nums.length; i++) { + int p = i; + if (nums[s] > nums[t]) { + for (int q = s + 1; q < i; q++) { + int temp = nums[p]; + nums[p] = nums[p - 1]; + nums[p - 1] = temp; + p = p - 1; + } + s++; + t++; + } else { + for (int q = t + 1; q < i; q++) { + int temp = nums[p]; + nums[p] = nums[p - 1]; + nums[p - 1] = temp; + p = p - 1; + } + t++; + } + } + return nums; + } +} diff --git a/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/readme.md b/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/readme.md new file mode 100644 index 000000000..6942ede45 --- /dev/null +++ b/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/readme.md @@ -0,0 +1,49 @@ +3069\. Distribute Elements Into Two Arrays I + +Easy + +You are given a **1-indexed** array of **distinct** integers `nums` of length `n`. + +You need to distribute all the elements of `nums` between two arrays `arr1` and `arr2` using `n` operations. In the first operation, append `nums[1]` to `arr1`. In the second operation, append `nums[2]` to `arr2`. Afterwards, in the ith operation: + +* If the last element of `arr1` is **greater** than the last element of `arr2`, append `nums[i]` to `arr1`. Otherwise, append `nums[i]` to `arr2`. + +The array `result` is formed by concatenating the arrays `arr1` and `arr2`. For example, if `arr1 == [1,2,3]` and `arr2 == [4,5,6]`, then `result = [1,2,3,4,5,6]`. + +Return _the array_ `result`. + +**Example 1:** + +**Input:** nums = [2,1,3] + +**Output:** [2,3,1] + +**Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1]. + +In the 3rd operation, as the last element of arr1 is greater than the last element of arr2 (2 > 1), append nums[3] to arr1. + +After 3 operations, arr1 = [2,3] and arr2 = [1]. + +Hence, the array result formed by concatenation is [2,3,1]. + +**Example 2:** + +**Input:** nums = [5,4,3,8] + +**Output:** [5,3,4,8] + +**Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [4]. + +In the 3rd operation, as the last element of arr1 is greater than the last element of arr2 (5 > 4), append nums[3] to arr1, hence arr1 becomes [5,3]. + +In the 4th operation, as the last element of arr2 is greater than the last element of arr1 (4 > 3), append nums[4] to arr2, hence arr2 becomes [4,8]. + +After 4 operations, arr1 = [5,3] and arr2 = [4,8]. + +Hence, the array result formed by concatenation is [5,3,4,8]. + +**Constraints:** + +* `3 <= n <= 50` +* `1 <= nums[i] <= 100` +* All elements in `nums` are distinct. \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/SolutionTest.java b/src/test/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/SolutionTest.java new file mode 100644 index 000000000..5ed7e046b --- /dev/null +++ b/src/test/java/g3001_3100/s3065_minimum_operations_to_exceed_threshold_value_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3065_minimum_operations_to_exceed_threshold_value_i; + +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[] {2, 11, 10, 1, 3}, 10), equalTo(3)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {1, 1, 2, 4, 9}, 1), equalTo(0)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {1, 1, 2, 4, 9}, 9), equalTo(4)); + } +} diff --git a/src/test/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/SolutionTest.java b/src/test/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/SolutionTest.java new file mode 100644 index 000000000..000a382a0 --- /dev/null +++ b/src/test/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3066_minimum_operations_to_exceed_threshold_value_ii; + +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[] {2, 11, 10, 1, 3}, 10), equalTo(2)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {1, 1, 2, 4, 9}, 20), equalTo(4)); + } +} diff --git a/src/test/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/SolutionTest.java b/src/test/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/SolutionTest.java new file mode 100644 index 000000000..d925488e3 --- /dev/null +++ b/src/test/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/SolutionTest.java @@ -0,0 +1,32 @@ +package g3001_3100.s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countPairsOfConnectableServers() { + assertThat( + new Solution() + .countPairsOfConnectableServers( + new int[][] { + {0, 1, 1}, {1, 2, 5}, {2, 3, 13}, {3, 4, 9}, {4, 5, 2} + }, + 1), + equalTo(new int[] {0, 4, 6, 6, 4, 0})); + } + + @Test + void countPairsOfConnectableServers2() { + assertThat( + new Solution() + .countPairsOfConnectableServers( + new int[][] { + {0, 6, 3}, {6, 5, 3}, {0, 3, 1}, {3, 2, 7}, {3, 1, 6}, {3, 4, 2} + }, + 3), + equalTo(new int[] {2, 0, 0, 0, 0, 0, 2})); + } +} diff --git a/src/test/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/SolutionTest.java b/src/test/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/SolutionTest.java new file mode 100644 index 000000000..e03262285 --- /dev/null +++ b/src/test/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/SolutionTest.java @@ -0,0 +1,34 @@ +package g3001_3100.s3068_find_the_maximum_sum_of_node_values; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumValueSum() { + assertThat( + new Solution() + .maximumValueSum(new int[] {1, 2, 1}, 3, new int[][] {{0, 1}, {0, 2}}), + equalTo(6L)); + } + + @Test + void maximumValueSum2() { + assertThat( + new Solution().maximumValueSum(new int[] {2, 3}, 7, new int[][] {{0, 1}}), + equalTo(9L)); + } + + @Test + void maximumValueSum3() { + assertThat( + new Solution() + .maximumValueSum( + new int[] {7, 7, 7, 7, 7, 7}, + 3, + new int[][] {{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}}), + equalTo(42L)); + } +} diff --git a/src/test/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/SolutionTest.java b/src/test/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/SolutionTest.java new file mode 100644 index 000000000..91c05dd28 --- /dev/null +++ b/src/test/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/SolutionTest.java @@ -0,0 +1,20 @@ +package g3001_3100.s3069_distribute_elements_into_two_arrays_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void resultArray() { + assertThat(new Solution().resultArray(new int[] {2, 1, 3}), equalTo(new int[] {2, 3, 1})); + } + + @Test + void resultArray2() { + assertThat( + new Solution().resultArray(new int[] {5, 4, 3, 8}), + equalTo(new int[] {5, 3, 4, 8})); + } +} From 41f8716d046659453477c0f0bd521ab1ff7fa5ef Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:12:18 +0300 Subject: [PATCH 2/7] Update Solution.java --- .../s3069_distribute_elements_into_two_arrays_i/Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java b/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java index cb624a83b..a946f0f4e 100644 --- a/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java +++ b/src/main/java/g3001_3100/s3069_distribute_elements_into_two_arrays_i/Solution.java @@ -4,7 +4,8 @@ public class Solution { public int[] resultArray(int[] nums) { - int s = 0, t = 1; + int s = 0; + int t = 1; for (int i = 2; i < nums.length; i++) { int p = i; if (nums[s] > nums[t]) { From 0740b604ba2cbfe43074d429344973e43038030c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:12:47 +0300 Subject: [PATCH 3/7] Update Solution.java --- .../s3068_find_the_maximum_sum_of_node_values/Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java index 9df7e3f14..6144d246d 100644 --- a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java +++ b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java @@ -6,7 +6,8 @@ public class Solution { public long maximumValueSum(int[] A, int k, int[][] edges) { long res = 0; - int d = 1 << 30, c = 0; + int d = 1 << 30; + int c = 0; for (int a : A) { int b = a ^ k; res += Math.max(a, b); From 3214001e024c9f64bb3ea3ac01313a7a516f9bd4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:13:19 +0300 Subject: [PATCH 4/7] Update Solution.java --- .../s3068_find_the_maximum_sum_of_node_values/Solution.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java index 6144d246d..c90e82ff8 100644 --- a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java +++ b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java @@ -4,11 +4,11 @@ // #2024_03_31_Time_1_ms_(100.00%)_Space_54.5_MB_(67.07%) public class Solution { - public long maximumValueSum(int[] A, int k, int[][] edges) { + public long maximumValueSum(int[] nums, int k, int[][] edges) { long res = 0; int d = 1 << 30; int c = 0; - for (int a : A) { + for (int a : nums) { int b = a ^ k; res += Math.max(a, b); c ^= a < b ? 1 : 0; From 4b197cbe2cb285539cd0c5d95de5e9bd579608fd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:17:00 +0300 Subject: [PATCH 5/7] Fixed style --- .../Solution.java | 10 +++++++--- .../Solution.java | 13 ++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java index 634100fbb..40a7951b7 100644 --- a/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java +++ b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java @@ -21,7 +21,8 @@ public int minOperations(int[] nums, int k) { increase = false; break; } - int min = -1, max = -1; + int min = -1; + int max = -1; if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) { min = nums[i++]; } else { @@ -35,8 +36,11 @@ public int minOperations(int[] nums, int k) { steps++; long res = min; res = 2 * res + max; - if (res > Integer.MAX_VALUE) extra.add(Integer.MAX_VALUE); - else extra.add((int) res); + if (res > Integer.MAX_VALUE) { + extra.add(Integer.MAX_VALUE); + } else { + extra.add((int) res); + } } return steps; } diff --git a/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java index 2efdc5857..647669920 100644 --- a/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java +++ b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java @@ -4,8 +4,9 @@ import java.util.ArrayList; +@SuppressWarnings("unchecked") public class Solution { - private ArrayList adj[]; + private ArrayList[] adj; public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) { int n = edges.length + 1; @@ -13,19 +14,21 @@ public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) { for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } - for (int edge[] : edges) { - int u = edge[0], v = edge[1], w = edge[2]; + for (int[] edge : edges) { + int u = edge[0]; + int v = edge[1]; + int w = edge[2]; adj[u].add(v); adj[v].add(u); adj[u].add(w); adj[v].add(w); } - int res[] = new int[n]; + int[] res = new int[n]; for (int i = 0; i < n; i++) { if (adj[i].size() > 2) { ArrayList al = new ArrayList<>(); for (int j = 0; j < adj[i].size(); j += 2) { - int cnt[] = new int[1]; + int[] cnt = new int[1]; dfs(adj[i].get(j), i, adj[i].get(j + 1), cnt, signalSpeed); al.add(cnt[0]); } From 4df660c394981ba65cd176e7303fb934ebb7d48f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:23:38 +0300 Subject: [PATCH 6/7] Fixed style --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java index 647669920..afd375600 100644 --- a/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java +++ b/src/main/java/g3001_3100/s3067_count_pairs_of_connectable_servers_in_a_weighted_tree_network/Solution.java @@ -42,7 +42,7 @@ public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) { return res; } - void dfs(int node, int par, int sum, int cnt[], int ss) { + void dfs(int node, int par, int sum, int[] cnt, int ss) { if (sum % ss == 0) { cnt[0]++; } From 593c62205f7bc918ce52669bdbe665c35a91e2fc Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 31 Mar 2024 11:38:13 +0300 Subject: [PATCH 7/7] Fixed sonar --- .../Solution.java | 11 +++-------- .../Solution.java | 1 + 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java index 40a7951b7..db184cdb0 100644 --- a/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java +++ b/src/main/java/g3001_3100/s3066_minimum_operations_to_exceed_threshold_value_ii/Solution.java @@ -13,16 +13,11 @@ public int minOperations(int[] nums, int k) { int steps = 0; Arrays.sort(nums); List extra = new ArrayList<>(); - boolean increase = true; int i = 0; int j = 0; - while (increase) { - if ((i >= n || nums[i] >= k) && (j >= extra.size() || extra.get(j) >= k)) { - increase = false; - break; - } - int min = -1; - int max = -1; + while ((i < n && nums[i] < k) || (j < extra.size() && extra.get(j) < k)) { + int min; + int max; if (i < n && (j >= extra.size() || extra.get(j) > nums[i])) { min = nums[i++]; } else { diff --git a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java index c90e82ff8..bfc2e767c 100644 --- a/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java +++ b/src/main/java/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.java @@ -3,6 +3,7 @@ // #Hard #Array #Dynamic_Programming #Sorting #Greedy #Tree #Bit_Manipulation // #2024_03_31_Time_1_ms_(100.00%)_Space_54.5_MB_(67.07%) +@SuppressWarnings("java:S1172") public class Solution { public long maximumValueSum(int[] nums, int k, int[][] edges) { long res = 0;