From 8da9c0b8321d452460c4139759698e54a085dbcf Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 19 Jan 2025 09:22:05 +0200 Subject: [PATCH 01/10] Added tasks 3423-3426 --- .../Solution.java | 15 ++++ .../readme.md | 32 +++++++ .../Solution.java | 21 +++++ .../readme.md | 43 +++++++++ .../s3425_longest_special_path/Solution.java | 88 +++++++++++++++++++ .../s3425_longest_special_path/readme.md | 48 ++++++++++ .../Solution.java | 36 ++++++++ .../readme.md | 53 +++++++++++ .../SolutionTest.java | 18 ++++ .../SolutionTest.java | 20 +++++ .../SolutionTest.java | 25 ++++++ .../SolutionTest.java | 18 ++++ 12 files changed, 417 insertions(+) create mode 100644 src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java create mode 100644 src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/readme.md create mode 100644 src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java create mode 100644 src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/readme.md create mode 100644 src/main/java/g3401_3500/s3425_longest_special_path/Solution.java create mode 100644 src/main/java/g3401_3500/s3425_longest_special_path/readme.md create mode 100644 src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java create mode 100644 src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/readme.md create mode 100644 src/test/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3425_longest_special_path/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/SolutionTest.java diff --git a/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java b/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java new file mode 100644 index 000000000..d517691bf --- /dev/null +++ b/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java @@ -0,0 +1,15 @@ +package g3401_3500.s3423_maximum_difference_between_adjacent_elements_in_a_circular_array; + +// #Easy #2025_01_19_Time_1_(100.00%)_Space_43.31_(100.00%) + +public class Solution { + public int maxAdjacentDistance(int[] nums) { + int maxDiff = 0; + for (int i = 0; i < nums.length; i++) { + int nextIndex = (i + 1) % nums.length; + int diff = Math.abs(nums[i] - nums[nextIndex]); + maxDiff = Math.max(maxDiff, diff); + } + return maxDiff; + } +} diff --git a/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/readme.md b/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/readme.md new file mode 100644 index 000000000..924b26986 --- /dev/null +++ b/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/readme.md @@ -0,0 +1,32 @@ +3423\. Maximum Difference Between Adjacent Elements in a Circular Array + +Easy + +Given a **circular** array `nums`, find the **maximum** absolute difference between adjacent elements. + +**Note**: In a circular array, the first and last elements are adjacent. + +**Example 1:** + +**Input:** nums = [1,2,4] + +**Output:** 3 + +**Explanation:** + +Because `nums` is circular, `nums[0]` and `nums[2]` are adjacent. They have the maximum absolute difference of `|4 - 1| = 3`. + +**Example 2:** + +**Input:** nums = [-5,-10,-5] + +**Output:** 5 + +**Explanation:** + +The adjacent elements `nums[0]` and `nums[1]` have the maximum absolute difference of `|-5 - (-10)| = 5`. + +**Constraints:** + +* `2 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java b/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java new file mode 100644 index 000000000..93be4b3fe --- /dev/null +++ b/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java @@ -0,0 +1,21 @@ +package g3401_3500.s3424_minimum_cost_to_make_arrays_identical; + +// #Medium #2025_01_19_Time_62_(_%)_Space_57.38_(_%) + +import java.util.Arrays; + +public class Solution { + public long minCost(int[] arr, int[] brr, long k) { + long res1 = 0; + long res2 = 0; + for (int i = 0; i < arr.length; ++i) { + res1 += Math.abs(arr[i] - brr[i]); + } + Arrays.sort(arr); + Arrays.sort(brr); + for (int i = 0; i < arr.length; ++i) { + res2 += Math.abs(arr[i] - brr[i]); + } + return Math.min(res1, res2 + k); + } +} diff --git a/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/readme.md b/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/readme.md new file mode 100644 index 000000000..2cd554df5 --- /dev/null +++ b/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/readme.md @@ -0,0 +1,43 @@ +3424\. Minimum Cost to Make Arrays Identical + +Medium + +You are given two integer arrays `arr` and `brr` of length `n`, and an integer `k`. You can perform the following operations on `arr` _any_ number of times: + +* Split `arr` into _any_ number of **contiguous** **non-empty subarrays** and rearrange these subarrays in _any order_. This operation has a fixed cost of `k`. +* Choose any element in `arr` and add or subtract a positive integer `x` to it. The cost of this operation is `x`. + + +Return the **minimum** total cost to make `arr` **equal** to `brr`. + +**Example 1:** + +**Input:** arr = [-7,9,5], brr = [7,-2,-5], k = 2 + +**Output:** 13 + +**Explanation:** + +* Split `arr` into two contiguous subarrays: `[-7]` and `[9, 5]` and rearrange them as `[9, 5, -7]`, with a cost of 2. +* Subtract 2 from element `arr[0]`. The array becomes `[7, 5, -7]`. The cost of this operation is 2. +* Subtract 7 from element `arr[1]`. The array becomes `[7, -2, -7]`. The cost of this operation is 7. +* Add 2 to element `arr[2]`. The array becomes `[7, -2, -5]`. The cost of this operation is 2. + +The total cost to make the arrays equal is `2 + 2 + 7 + 2 = 13`. + +**Example 2:** + +**Input:** arr = [2,1], brr = [2,1], k = 0 + +**Output:** 0 + +**Explanation:** + +Since the arrays are already equal, no operations are needed, and the total cost is 0. + +**Constraints:** + +* 1 <= arr.length == brr.length <= 105 +* 0 <= k <= 2 * 1010 +* -105 <= arr[i] <= 105 +* -105 <= brr[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java b/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java new file mode 100644 index 000000000..66a3f2878 --- /dev/null +++ b/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java @@ -0,0 +1,88 @@ +package g3401_3500.s3425_longest_special_path; + +// #Hard #2025_01_19_Time_57_(100.00%)_Space_94.11_(100.00%) + +import java.util.ArrayList; +import java.util.Arrays; + +@SuppressWarnings("unchecked") +public class Solution { + private ArrayList[] adj; + private int[] nums; + private int[] dist; + private int[] lastOccur; + private ArrayList pathStack; + private int minIndex; + private int maxLen; + private int minNodesForMaxLen; + + public int[] longestSpecialPath(int[][] edges, int[] nums) { + int n = nums.length; + this.nums = nums; + adj = new ArrayList[n]; + for (int i = 0; i < n; i++) { + adj[i] = new ArrayList<>(); + } + for (int[] e : edges) { + int u = e[0], v = e[1], w = e[2]; + adj[u].add(new int[] {v, w}); + adj[v].add(new int[] {u, w}); + } + dist = new int[n]; + buildDist(0, -1, 0); + int maxVal = 0; + for (int val : nums) { + if (val > maxVal) { + maxVal = val; + } + } + lastOccur = new int[maxVal + 1]; + Arrays.fill(lastOccur, -1); + pathStack = new ArrayList<>(); + minIndex = 0; + maxLen = 0; + minNodesForMaxLen = Integer.MAX_VALUE; + dfs(0, -1); + return new int[] {maxLen, minNodesForMaxLen}; + } + + private void buildDist(int u, int parent, int currDist) { + dist[u] = currDist; + for (int[] edge : adj[u]) { + int v = edge[0], w = edge[1]; + if (v == parent) continue; + buildDist(v, u, currDist + w); + } + } + + private void dfs(int u, int parent) { + int stackPos = pathStack.size(); + pathStack.add(u); + int val = nums[u]; + int oldPos = lastOccur[val]; + int oldMinIndex = minIndex; + lastOccur[val] = stackPos; + if (oldPos >= minIndex) { + minIndex = oldPos + 1; + } + if (minIndex <= stackPos) { + int ancestor = pathStack.get(minIndex); + int pathLength = dist[u] - dist[ancestor]; + int pathNodes = stackPos - minIndex + 1; + if (pathLength > maxLen) { + maxLen = pathLength; + minNodesForMaxLen = pathNodes; + } else if (pathLength == maxLen && pathNodes < minNodesForMaxLen) { + minNodesForMaxLen = pathNodes; + } + } + for (int[] edge : adj[u]) { + int v = edge[0]; + if (v == parent) continue; + dfs(v, u); + } + pathStack.remove(pathStack.size() - 1); + lastOccur[val] = oldPos; + minIndex = oldMinIndex; + } +} diff --git a/src/main/java/g3401_3500/s3425_longest_special_path/readme.md b/src/main/java/g3401_3500/s3425_longest_special_path/readme.md new file mode 100644 index 000000000..53b174799 --- /dev/null +++ b/src/main/java/g3401_3500/s3425_longest_special_path/readme.md @@ -0,0 +1,48 @@ +3425\. Longest Special Path + +Hard + +You are given an undirected tree rooted at node `0` with `n` nodes numbered from `0` to `n - 1`, represented by a 2D array `edges` of length `n - 1`, where edges[i] = [ui, vi, lengthi] indicates an edge between nodes ui and vi with length lengthi. You are also given an integer array `nums`, where `nums[i]` represents the value at node `i`. + +A **special path** is defined as a **downward** path from an ancestor node to a descendant node such that all the values of the nodes in that path are **unique**. + +**Note** that a path may start and end at the same node. + +Return an array `result` of size 2, where `result[0]` is the **length** of the **longest** special path, and `result[1]` is the **minimum** number of nodes in all _possible_ **longest** special paths. + +**Example 1:** + +**Input:** edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1] + +**Output:** [6,2] + +**Explanation:** + +#### In the image below, nodes are colored by their corresponding values in `nums` + +![](https://assets.leetcode.com/uploads/2024/11/02/tree3.jpeg) + +The longest special paths are `2 -> 5` and `0 -> 1 -> 4`, both having a length of 6. The minimum number of nodes across all longest special paths is 2. + +**Example 2:** + +**Input:** edges = [[1,0,8]], nums = [2,2] + +**Output:** [0,1] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/11/02/tree4.jpeg) + +The longest special paths are `0` and `1`, both having a length of 0. The minimum number of nodes across all longest special paths is 1. + +**Constraints:** + +* 2 <= n <= 5 * 104 +* `edges.length == n - 1` +* `edges[i].length == 3` +* 0 <= ui, vi < n +* 1 <= lengthi <= 103 +* `nums.length == n` +* 0 <= nums[i] <= 5 * 104 +* The input is generated such that `edges` represents a valid tree. \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java new file mode 100644 index 000000000..ba2c4b85d --- /dev/null +++ b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java @@ -0,0 +1,36 @@ +package g3401_3500.s3426_manhattan_distances_of_all_arrangements_of_pieces; + +// #Hard #2025_01_19_Time_20_(100.00%)_Space_41.18_(100.00%) + +public class Solution { + private long comb(long a, long b, long mod) { + if (b > a) return 0; + long numer = 1, denom = 1; + for (long i = 0; i < b; ++i) { + numer = numer * (a - i) % mod; + denom = denom * (i + 1) % mod; + } + long denom_inv = 1; + long exp = mod - 2; + while (exp > 0) { + if (exp % 2 > 0) { + denom_inv = denom_inv * denom % mod; + } + denom = denom * denom % mod; + exp /= 2; + } + return numer * denom_inv % mod; + } + + public int distanceSum(int m, int n, int k) { + long res = 0, mod = 1000000007; + long base = comb((long) m * n - 2, k - 2, mod); + for (int d = 1; d < n; ++d) { + res = (res + (long) d * (n - d) % mod * m % mod * m % mod) % mod; + } + for (int d = 1; d < m; ++d) { + res = (res + (long) d * (m - d) % mod * n % mod * n % mod) % mod; + } + return (int) (res * base % mod); + } +} diff --git a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/readme.md b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/readme.md new file mode 100644 index 000000000..4e262e62c --- /dev/null +++ b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/readme.md @@ -0,0 +1,53 @@ +3426\. Manhattan Distances of All Arrangements of Pieces + +Hard + +You are given three integers `m`, `n`, and `k`. + +There is a rectangular grid of size `m × n` containing `k` identical pieces. Return the sum of Manhattan distances between every pair of pieces over all **valid arrangements** of pieces. + +A **valid arrangement** is a placement of all `k` pieces on the grid with **at most** one piece per cell. + +Since the answer may be very large, return it **modulo** 109 + 7. + +The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|. + +**Example 1:** + +**Input:** m = 2, n = 2, k = 2 + +**Output:** 8 + +**Explanation:** + +The valid arrangements of pieces on the board are: + +![](https://assets.leetcode.com/uploads/2024/12/25/4040example1.drawio)![](https://assets.leetcode.com/uploads/2024/12/25/untitled-diagramdrawio.png) + +* In the first 4 arrangements, the Manhattan distance between the two pieces is 1. +* In the last 2 arrangements, the Manhattan distance between the two pieces is 2. + +Thus, the total Manhattan distance across all valid arrangements is `1 + 1 + 1 + 1 + 2 + 2 = 8`. + +**Example 2:** + +**Input:** m = 1, n = 4, k = 3 + +**Output:** 20 + +**Explanation:** + +The valid arrangements of pieces on the board are: + +![](https://assets.leetcode.com/uploads/2024/12/25/4040example2drawio.png) + +* The first and last arrangements have a total Manhattan distance of `1 + 1 + 2 = 4`. +* The middle two arrangements have a total Manhattan distance of `1 + 2 + 3 = 6`. + +The total Manhattan distance between all pairs of pieces across all arrangements is `4 + 6 + 6 + 4 = 20`. + +**Constraints:** + +* 1 <= m, n <= 105 +* 2 <= m * n <= 105 +* `2 <= k <= m * n` \ No newline at end of file diff --git a/src/test/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/SolutionTest.java b/src/test/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/SolutionTest.java new file mode 100644 index 000000000..2ee238636 --- /dev/null +++ b/src/test/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3423_maximum_difference_between_adjacent_elements_in_a_circular_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxAdjacentDistance() { + assertThat(new Solution().maxAdjacentDistance(new int[] {1, 2, 4}), equalTo(3)); + } + + @Test + void maxAdjacentDistance2() { + assertThat(new Solution().maxAdjacentDistance(new int[] {-5, -10, -5}), equalTo(5)); + } +} diff --git a/src/test/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/SolutionTest.java b/src/test/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/SolutionTest.java new file mode 100644 index 000000000..db9f6ac08 --- /dev/null +++ b/src/test/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/SolutionTest.java @@ -0,0 +1,20 @@ +package g3401_3500.s3424_minimum_cost_to_make_arrays_identical; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minCost() { + assertThat( + new Solution().minCost(new int[] {-7, 9, 5}, new int[] {7, -2, -5}, 2), + equalTo(13L)); + } + + @Test + void minCost2() { + assertThat(new Solution().minCost(new int[] {2, 1}, new int[] {2, 1}, 0), equalTo(0L)); + } +} diff --git a/src/test/java/g3401_3500/s3425_longest_special_path/SolutionTest.java b/src/test/java/g3401_3500/s3425_longest_special_path/SolutionTest.java new file mode 100644 index 000000000..f848f5887 --- /dev/null +++ b/src/test/java/g3401_3500/s3425_longest_special_path/SolutionTest.java @@ -0,0 +1,25 @@ +package g3401_3500.s3425_longest_special_path; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void longestSpecialPath() { + assertThat( + new Solution() + .longestSpecialPath( + new int[][] {{0, 1, 2}, {1, 2, 3}, {1, 3, 5}, {1, 4, 4}, {2, 5, 6}}, + new int[] {2, 1, 2, 1, 3, 1}), + equalTo(new int[] {6, 2})); + } + + @Test + void longestSpecialPath2() { + assertThat( + new Solution().longestSpecialPath(new int[][] {{1, 0, 8}}, new int[] {2, 2}), + equalTo(new int[] {0, 1})); + } +} diff --git a/src/test/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/SolutionTest.java b/src/test/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/SolutionTest.java new file mode 100644 index 000000000..b4fb1df5d --- /dev/null +++ b/src/test/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3426_manhattan_distances_of_all_arrangements_of_pieces; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void distanceSum() { + assertThat(new Solution().distanceSum(2, 2, 2), equalTo(8)); + } + + @Test + void distanceSum2() { + assertThat(new Solution().distanceSum(1, 4, 3), equalTo(20)); + } +} From 3536d4c1cb9359b91bd16e2c70fde5e328fcc8c3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 19 Jan 2025 11:03:19 +0200 Subject: [PATCH 02/10] Fixed style --- .../s3425_longest_special_path/Solution.java | 15 +++++++++++---- .../Solution.java | 16 ++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java b/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java index 66a3f2878..4b3b03faf 100644 --- a/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java +++ b/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java @@ -24,7 +24,9 @@ public int[] longestSpecialPath(int[][] edges, int[] nums) { adj[i] = new ArrayList<>(); } for (int[] e : edges) { - int u = e[0], v = e[1], w = e[2]; + int u = e[0]; + int v = e[1]; + int w = e[2]; adj[u].add(new int[] {v, w}); adj[v].add(new int[] {u, w}); } @@ -49,8 +51,11 @@ public int[] longestSpecialPath(int[][] edges, int[] nums) { private void buildDist(int u, int parent, int currDist) { dist[u] = currDist; for (int[] edge : adj[u]) { - int v = edge[0], w = edge[1]; - if (v == parent) continue; + int v = edge[0]; + int w = edge[1]; + if (v == parent) { + continue; + } buildDist(v, u, currDist + w); } } @@ -78,7 +83,9 @@ private void dfs(int u, int parent) { } for (int[] edge : adj[u]) { int v = edge[0]; - if (v == parent) continue; + if (v == parent) { + continue; + } dfs(v, u); } pathStack.remove(pathStack.size() - 1); diff --git a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java index ba2c4b85d..35f814e23 100644 --- a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java +++ b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java @@ -4,26 +4,30 @@ public class Solution { private long comb(long a, long b, long mod) { - if (b > a) return 0; - long numer = 1, denom = 1; + if (b > a) { + return 0; + } + long numer = 1; + long denom = 1; for (long i = 0; i < b; ++i) { numer = numer * (a - i) % mod; denom = denom * (i + 1) % mod; } - long denom_inv = 1; + long denomInv = 1; long exp = mod - 2; while (exp > 0) { if (exp % 2 > 0) { - denom_inv = denom_inv * denom % mod; + denomInv = denomInv * denom % mod; } denom = denom * denom % mod; exp /= 2; } - return numer * denom_inv % mod; + return numer * denomInv % mod; } public int distanceSum(int m, int n, int k) { - long res = 0, mod = 1000000007; + long res = 0; + long mod = 1000000007; long base = comb((long) m * n - 2, k - 2, mod); for (int d = 1; d < n; ++d) { res = (res + (long) d * (n - d) % mod * m % mod * m % mod) % mod; From c1a75cb3a083daa63125f1f48bbd8832f1fbbc7a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 19 Jan 2025 11:10:08 +0200 Subject: [PATCH 03/10] Fixed sonar --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java index 35f814e23..7fb7d474f 100644 --- a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java +++ b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java @@ -28,7 +28,7 @@ private long comb(long a, long b, long mod) { public int distanceSum(int m, int n, int k) { long res = 0; long mod = 1000000007; - long base = comb((long) m * n - 2, k - 2, mod); + long base = comb((long) m * n - 2, k - 2L, mod); for (int d = 1; d < n; ++d) { res = (res + (long) d * (n - d) % mod * m % mod * m % mod) % mod; } From efe2320a1aa4630bb4eddc5318df37dc622c2a85 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 02:05:26 +0200 Subject: [PATCH 04/10] Updated tags --- .../Solution.java | 2 +- .../s3424_minimum_cost_to_make_arrays_identical/Solution.java | 2 +- .../java/g3401_3500/s3425_longest_special_path/Solution.java | 3 ++- .../Solution.java | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java b/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java index d517691bf..b6b97952a 100644 --- a/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java +++ b/src/main/java/g3401_3500/s3423_maximum_difference_between_adjacent_elements_in_a_circular_array/Solution.java @@ -1,6 +1,6 @@ package g3401_3500.s3423_maximum_difference_between_adjacent_elements_in_a_circular_array; -// #Easy #2025_01_19_Time_1_(100.00%)_Space_43.31_(100.00%) +// #Easy #Array #2025_01_22_Time_1_(99.86%)_Space_43.72_(36.06%) public class Solution { public int maxAdjacentDistance(int[] nums) { diff --git a/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java b/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java index 93be4b3fe..794d94979 100644 --- a/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java +++ b/src/main/java/g3401_3500/s3424_minimum_cost_to_make_arrays_identical/Solution.java @@ -1,6 +1,6 @@ package g3401_3500.s3424_minimum_cost_to_make_arrays_identical; -// #Medium #2025_01_19_Time_62_(_%)_Space_57.38_(_%) +// #Medium #Array #Sorting #Greedy #2025_01_22_Time_60_(98.08%)_Space_57.68_(39.04%) import java.util.Arrays; diff --git a/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java b/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java index 4b3b03faf..1be9ee2aa 100644 --- a/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java +++ b/src/main/java/g3401_3500/s3425_longest_special_path/Solution.java @@ -1,6 +1,7 @@ package g3401_3500.s3425_longest_special_path; -// #Hard #2025_01_19_Time_57_(100.00%)_Space_94.11_(100.00%) +// #Hard #Array #Hash_Table #Depth_First_Search #Tree #Sliding_Window +// #2025_01_22_Time_49_(74.66%)_Space_98.04_(44.26%) import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java index 7fb7d474f..7016aa07f 100644 --- a/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java +++ b/src/main/java/g3401_3500/s3426_manhattan_distances_of_all_arrangements_of_pieces/Solution.java @@ -1,6 +1,6 @@ package g3401_3500.s3426_manhattan_distances_of_all_arrangements_of_pieces; -// #Hard #2025_01_19_Time_20_(100.00%)_Space_41.18_(100.00%) +// #Hard #Math #Combinatorics #2025_01_22_Time_20_(87.92%)_Space_40.82_(98.07%) public class Solution { private long comb(long a, long b, long mod) { From ef39e0cbc3e9268d5922892e01695b1e2f9443bf Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 02:28:17 +0200 Subject: [PATCH 05/10] Added tasks 3427-3430 --- .../Solution.java | 15 +++ .../readme.md | 94 +++++++++++++++ .../Solution.java | 62 ++++++++++ .../readme.md | 109 ++++++++++++++++++ .../s3429_paint_house_iv/Solution.java | 33 ++++++ .../g3401_3500/s3429_paint_house_iv/readme.md | 53 +++++++++ .../Solution.java | 41 +++++++ .../readme.md | 53 +++++++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 18 +++ .../s3429_paint_house_iv/SolutionTest.java | 27 +++++ .../SolutionTest.java | 18 +++ 12 files changed, 541 insertions(+) create mode 100644 src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/Solution.java create mode 100644 src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md create mode 100644 src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java create mode 100644 src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md create mode 100644 src/main/java/g3401_3500/s3429_paint_house_iv/Solution.java create mode 100644 src/main/java/g3401_3500/s3429_paint_house_iv/readme.md create mode 100644 src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java create mode 100644 src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/readme.md create mode 100644 src/test/java/g3401_3500/s3427_sum_of_variable_length_subarrays/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3429_paint_house_iv/SolutionTest.java create mode 100644 src/test/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/SolutionTest.java diff --git a/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/Solution.java b/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/Solution.java new file mode 100644 index 000000000..c713e549d --- /dev/null +++ b/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/Solution.java @@ -0,0 +1,15 @@ +package g3401_3500.s3427_sum_of_variable_length_subarrays; + +// #Easy #Array #Prefix_Sum #2025_01_22_Time_0_(100.00%)_Space_43.77_(58.41%) + +public class Solution { + public int subarraySum(int[] nums) { + int res = nums[0]; + for (int i = 1; i < nums.length; i++) { + int j = i - nums[i] - 1; + nums[i] += nums[i - 1]; + res += nums[i] - (j < 0 ? 0 : nums[j]); + } + return res; + } +} diff --git a/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md b/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md new file mode 100644 index 000000000..b40691b8c --- /dev/null +++ b/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md @@ -0,0 +1,94 @@ +3427\. Sum of Variable Length Subarrays + +Easy + +You are given an integer array `nums` of size `n`. For **each** index `i` where `0 <= i < n`, define a **non-empty subarrays** `nums[start ... i]` where `start = max(0, i - nums[i])`. + +Return the total sum of all elements from the subarray defined for each index in the array. + +**Example 1:** + +**Input:** nums = [2,3,1] + +**Output:** 11 + +**Explanation:** + +i + +Subarray + +Sum + +0 + +`nums[0] = [2]` + +2 + +1 + +`nums[0 ... 1] = [2, 3]` + +5 + +2 + +`nums[1 ... 2] = [3, 1]` + +4 + +**Total Sum** + +11 + +The total sum is 11. Hence, 11 is the output. + +**Example 2:** + +**Input:** nums = [3,1,1,2] + +**Output:** 13 + +**Explanation:** + +i + +Subarray + +Sum + +0 + +`nums[0] = [3]` + +3 + +1 + +`nums[0 ... 1] = [3, 1]` + +4 + +2 + +`nums[1 ... 2] = [1, 1]` + +2 + +3 + +`nums[1 ... 3] = [1, 1, 2]` + +4 + +**Total Sum** + +13 + +The total sum is 13. Hence, 13 is the output. + +**Constraints:** + +* `1 <= n == nums.length <= 100` +* `1 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java new file mode 100644 index 000000000..a11df6a88 --- /dev/null +++ b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java @@ -0,0 +1,62 @@ +package g3401_3500.s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences; + +// #Medium #Array #Dynamic_Programming #Math #Sorting #Combinatorics +// #2025_01_22_Time_28_(99.74%)_Space_65.01_(35.71%) + +import java.util.Arrays; + +public class Solution { + private static final int MOD = (int) 1e9 + 7; + private static long[] fact, invFact; + + public int minMaxSums(int[] nums, int k) { + int n = nums.length; + Arrays.sort(nums); + if (fact == null || fact.length < n + 1) { + buildFactorials(n); + } + long[] sum = new long[n + 1]; + sum[0] = 1; + for (int i = 0; i < n; i++) { + long val = (2L * sum[i]) % MOD; + if (i + 1 >= k) { + val = (val - comb(i, k - 1) + MOD) % MOD; + } + sum[i + 1] = val; + } + long res = 0; + for (int i = 0; i < n; i++) { + long add = (sum[i] + sum[n - 1 - i]) % MOD; + res = (res + (nums[i] % MOD) * add) % MOD; + } + return (int) res; + } + + private void buildFactorials(int n) { + fact = new long[n + 1]; + invFact = new long[n + 1]; + fact[0] = 1; + for (int i = 1; i <= n; i++) { + fact[i] = (fact[i - 1] * i) % MOD; + } + invFact[n] = pow(fact[n], MOD - 2); + for (int i = n - 1; i >= 0; i--) { + invFact[i] = (invFact[i + 1] * (i + 1)) % MOD; + } + } + + private long comb(int n, int r) { + if (r < 0 || r > n) return 0; + return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD; + } + + private long pow(long base, int exp) { + long ans = 1L; + while (exp > 0) { + if ((exp & 1) == 1) ans = (ans * base) % MOD; + base = (base * base) % MOD; + exp >>= 1; + } + return ans; + } +} diff --git a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md new file mode 100644 index 000000000..c17010603 --- /dev/null +++ b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md @@ -0,0 +1,109 @@ +3428\. Maximum and Minimum Sums of at Most Size K Subsequences + +Medium + +You are given an integer array `nums` and a positive integer `k`. Return the sum of the **maximum** and **minimum** elements of all + +**subsequences** + +of `nums` with **at most** `k` elements. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 2 + +**Output:** 24 + +**Explanation:** + +The subsequences of `nums` with at most 2 elements are: + +**Subsequence** + +Minimum + +Maximum + +Sum + +`[1]` + +1 + +1 + +2 + +`[2]` + +2 + +2 + +4 + +`[3]` + +3 + +3 + +6 + +`[1, 2]` + +1 + +2 + +3 + +`[1, 3]` + +1 + +3 + +4 + +`[2, 3]` + +2 + +3 + +5 + +**Final Total** + +24 + +The output would be 24. + +**Example 2:** + +**Input:** nums = [5,0,6], k = 1 + +**Output:** 22 + +**Explanation:** + +For subsequences with exactly 1 element, the minimum and maximum values are the element itself. Therefore, the total is `5 + 5 + 0 + 0 + 6 + 6 = 22`. + +**Example 3:** + +**Input:** nums = [1,1,1], k = 2 + +**Output:** 12 + +**Explanation:** + +The subsequences `[1, 1]` and `[1]` each appear 3 times. For all of them, the minimum and maximum are both 1. Thus, the total is 12. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 109 +* `1 <= k <= min(70, nums.length)` \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3429_paint_house_iv/Solution.java b/src/main/java/g3401_3500/s3429_paint_house_iv/Solution.java new file mode 100644 index 000000000..596ca0701 --- /dev/null +++ b/src/main/java/g3401_3500/s3429_paint_house_iv/Solution.java @@ -0,0 +1,33 @@ +package g3401_3500.s3429_paint_house_iv; + +// #Medium #Array #Dynamic_Programming #2025_01_22_Time_5_(100.00%)_Space_106.29_(78.64%) + +public class Solution { + public long minCost(int n, int[][] cost) { + long dp0 = 0; + long dp1 = 0; + long dp2 = 0; + long dp3 = 0; + long dp4 = 0; + long dp5 = 0; + for (int i = 0; i < n / 2; ++i) { + long nextdp0 = Math.min(Math.min(dp2, dp3), dp4) + cost[i][0] + cost[n - i - 1][1]; + long nextdp1 = Math.min(Math.min(dp2, dp4), dp5) + cost[i][0] + cost[n - i - 1][2]; + long nextdp2 = Math.min(Math.min(dp0, dp1), dp5) + cost[i][1] + cost[n - i - 1][0]; + long nextdp3 = Math.min(Math.min(dp0, dp4), dp5) + cost[i][1] + cost[n - i - 1][2]; + long nextdp4 = Math.min(Math.min(dp0, dp1), dp3) + cost[i][2] + cost[n - i - 1][0]; + long nextdp5 = Math.min(Math.min(dp1, dp2), dp3) + cost[i][2] + cost[n - i - 1][1]; + dp0 = nextdp0; + dp1 = nextdp1; + dp2 = nextdp2; + dp3 = nextdp3; + dp4 = nextdp4; + dp5 = nextdp5; + } + long ans = Long.MAX_VALUE; + for (long x : new long[] {dp0, dp1, dp2, dp3, dp4, dp5}) { + ans = Math.min(ans, x); + } + return ans; + } +} diff --git a/src/main/java/g3401_3500/s3429_paint_house_iv/readme.md b/src/main/java/g3401_3500/s3429_paint_house_iv/readme.md new file mode 100644 index 000000000..8610512fd --- /dev/null +++ b/src/main/java/g3401_3500/s3429_paint_house_iv/readme.md @@ -0,0 +1,53 @@ +3429\. Paint House IV + +Medium + +You are given an **even** integer `n` representing the number of houses arranged in a straight line, and a 2D array `cost` of size `n x 3`, where `cost[i][j]` represents the cost of painting house `i` with color `j + 1`. + +The houses will look **beautiful** if they satisfy the following conditions: + +* No **two** adjacent houses are painted the same color. +* Houses **equidistant** from the ends of the row are **not** painted the same color. For example, if `n = 6`, houses at positions `(0, 5)`, `(1, 4)`, and `(2, 3)` are considered equidistant. + +Return the **minimum** cost to paint the houses such that they look **beautiful**. + +**Example 1:** + +**Input:** n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]] + +**Output:** 9 + +**Explanation:** + +The optimal painting sequence is `[1, 2, 3, 2]` with corresponding costs `[3, 2, 1, 3]`. This satisfies the following conditions: + +* No adjacent houses have the same color. +* Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color `(1 != 2)`. +* Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color `(2 != 3)`. + +The minimum cost to paint the houses so that they look beautiful is `3 + 2 + 1 + 3 = 9`. + +**Example 2:** + +**Input:** n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]] + +**Output:** 18 + +**Explanation:** + +The optimal painting sequence is `[1, 3, 2, 3, 1, 2]` with corresponding costs `[2, 8, 1, 2, 3, 2]`. This satisfies the following conditions: + +* No adjacent houses have the same color. +* Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color `(1 != 2)`. +* Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color `(3 != 1)`. +* Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color `(2 != 3)`. + +The minimum cost to paint the houses so that they look beautiful is `2 + 8 + 1 + 2 + 3 + 2 = 18`. + +**Constraints:** + +* 2 <= n <= 105 +* `n` is even. +* `cost.length == n` +* `cost[i].length == 3` +* 0 <= cost[i]\[j] <= 105 \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java new file mode 100644 index 000000000..b2d14b25d --- /dev/null +++ b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java @@ -0,0 +1,41 @@ +package g3401_3500.s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays; + +// #Hard #Array #Math #Stack #Monotonic_Stack #2025_01_22_Time_27_(99.40%)_Space_56.05_(94.64%) + +public class Solution { + public long minMaxSubarraySum(int[] nums, int k) { + long sum = solve(nums, k); + for (int i = 0; i < nums.length; i++) { + nums[i] = -nums[i]; + } + return sum - solve(nums, k); + } + + private long solve(int[] nums, int k) { + int n = nums.length; + int[] left = new int[n], right = new int[n], st = new int[n]; + int top = -1; + for (int i = 0; i < n; i++) { + int num = nums[i]; + while (top != -1 && num < nums[st[top]]) { + right[st[top--]] = i; + } + left[i] = top == -1 ? -1 : st[top]; + st[++top] = i; + } + while (0 <= top) { + right[st[top--]] = n; + } + long ans = 0; + for (int i = 0; i < n; i++) { + int num = nums[i], l = Math.min(i - left[i], k), r = Math.min(right[i] - i, k); + if (l + r - 1 <= k) { + ans += (long) num * l * r; + } else { + long cnt = (long) (k - r + 1) * r + (long) (l + r - k - 1) * (r + k - l) / 2; + ans += (long) num * cnt; + } + } + return ans; + } +} diff --git a/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/readme.md b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/readme.md new file mode 100644 index 000000000..a728efe17 --- /dev/null +++ b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/readme.md @@ -0,0 +1,53 @@ +3430\. Maximum and Minimum Sums of at Most Size K Subarrays + +Hard + +You are given an integer array `nums` and a **positive** integer `k`. Return the sum of the **maximum** and **minimum** elements of all **non-empty subarrays** with **at most** `k` elements. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 2 + +**Output:** 20 + +**Explanation:** + +The subarrays of `nums` with at most 2 elements are: + +| **Subarray** | Minimum | Maximum | Sum | +|--------------|---------|---------|-----| +| `[1]` | 1 | 1 | 2 | +| `[2]` | 2 | 2 | 4 | +| `[3]` | 3 | 3 | 6 | +| `[1, 2]` | 1 | 2 | 3 | +| `[2, 3]` | 2 | 3 | 5 | +| **Final Total** | | | 20 | + +The output would be 20. + +**Example 2:** + +**Input:** nums = [1,-3,1], k = 2 + +**Output:** \-6 + +**Explanation:** + +The subarrays of `nums` with at most 2 elements are: + +| **Subarray** | Minimum | Maximum | Sum | +|----------------|---------|---------|------| +| `[1]` | 1 | 1 | 2 | +| `[-3]` | -3 | -3 | -6 | +| `[1]` | 1 | 1 | 2 | +| `[1, -3]` | -3 | 1 | -2 | +| `[-3, 1]` | -3 | 1 | -2 | +| **Final Total**| | | -6 | + +The output would be -6. + +**Constraints:** + +* `1 <= nums.length <= 80000` +* `1 <= k <= nums.length` +* -106 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/test/java/g3401_3500/s3427_sum_of_variable_length_subarrays/SolutionTest.java b/src/test/java/g3401_3500/s3427_sum_of_variable_length_subarrays/SolutionTest.java new file mode 100644 index 000000000..3f910b5f6 --- /dev/null +++ b/src/test/java/g3401_3500/s3427_sum_of_variable_length_subarrays/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3427_sum_of_variable_length_subarrays; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void subarraySum() { + assertThat(new Solution().subarraySum(new int[] {2, 3, 1}), equalTo(11)); + } + + @Test + void subarraySum2() { + assertThat(new Solution().subarraySum(new int[] {3, 1, 1, 2}), equalTo(13)); + } +} diff --git a/src/test/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/SolutionTest.java b/src/test/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/SolutionTest.java new file mode 100644 index 000000000..24e7225e8 --- /dev/null +++ b/src/test/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMaxSums() { + assertThat(new Solution().minMaxSums(new int[] {1, 2, 3}, 2), equalTo(24)); + } + + @Test + void minMaxSums2() { + assertThat(new Solution().minMaxSums(new int[] {5, 0, 6}, 1), equalTo(22)); + } +} diff --git a/src/test/java/g3401_3500/s3429_paint_house_iv/SolutionTest.java b/src/test/java/g3401_3500/s3429_paint_house_iv/SolutionTest.java new file mode 100644 index 000000000..4f718128b --- /dev/null +++ b/src/test/java/g3401_3500/s3429_paint_house_iv/SolutionTest.java @@ -0,0 +1,27 @@ +package g3401_3500.s3429_paint_house_iv; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minCost() { + assertThat( + new Solution().minCost(4, new int[][] {{3, 5, 7}, {6, 2, 9}, {4, 8, 1}, {7, 3, 5}}), + equalTo(9L)); + } + + @Test + void minCost2() { + assertThat( + new Solution() + .minCost( + 6, + new int[][] { + {2, 4, 6}, {5, 3, 8}, {7, 1, 9}, {4, 6, 2}, {3, 5, 7}, {8, 2, 4} + }), + equalTo(18L)); + } +} diff --git a/src/test/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/SolutionTest.java b/src/test/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/SolutionTest.java new file mode 100644 index 000000000..0e29fbc5b --- /dev/null +++ b/src/test/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/SolutionTest.java @@ -0,0 +1,18 @@ +package g3401_3500.s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMaxSubarraySum() { + assertThat(new Solution().minMaxSubarraySum(new int[] {1, 2, 3}, 2), equalTo(20L)); + } + + @Test + void minMaxSubarraySum2() { + assertThat(new Solution().minMaxSubarraySum(new int[] {1, -3, 1}, 2), equalTo(-6L)); + } +} From 1f77e19fe63390f4f5dbbb003f2ec8f8fbdeba1f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 02:31:39 +0200 Subject: [PATCH 06/10] Fixed style --- .../Solution.java | 11 ++++++++--- .../Solution.java | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java index a11df6a88..ea279314c 100644 --- a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java +++ b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java @@ -7,7 +7,8 @@ public class Solution { private static final int MOD = (int) 1e9 + 7; - private static long[] fact, invFact; + private long[] fact; + private long[] invFact; public int minMaxSums(int[] nums, int k) { int n = nums.length; @@ -46,14 +47,18 @@ private void buildFactorials(int n) { } private long comb(int n, int r) { - if (r < 0 || r > n) return 0; + if (r < 0 || r > n) { + return 0; + } return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD; } private long pow(long base, int exp) { long ans = 1L; while (exp > 0) { - if ((exp & 1) == 1) ans = (ans * base) % MOD; + if ((exp & 1) == 1) { + ans = (ans * base) % MOD; + } base = (base * base) % MOD; exp >>= 1; } diff --git a/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java index b2d14b25d..b650ffa9b 100644 --- a/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java +++ b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java @@ -13,7 +13,9 @@ public long minMaxSubarraySum(int[] nums, int k) { private long solve(int[] nums, int k) { int n = nums.length; - int[] left = new int[n], right = new int[n], st = new int[n]; + int[] left = new int[n]; + int[] right = new int[n]; + int[] st = new int[n]; int top = -1; for (int i = 0; i < n; i++) { int num = nums[i]; @@ -28,7 +30,9 @@ private long solve(int[] nums, int k) { } long ans = 0; for (int i = 0; i < n; i++) { - int num = nums[i], l = Math.min(i - left[i], k), r = Math.min(right[i] - i, k); + int num = nums[i]; + int l = Math.min(i - left[i], k); + int r = Math.min(right[i] - i, k); if (l + r - 1 <= k) { ans += (long) num * l * r; } else { From a9e07e9cd0ff8ced79024b5b072bd88ba184b9af Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 02:35:45 +0200 Subject: [PATCH 07/10] Fixed sonar --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java index b650ffa9b..0b343f7c6 100644 --- a/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java +++ b/src/main/java/g3401_3500/s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays/Solution.java @@ -37,7 +37,7 @@ private long solve(int[] nums, int k) { ans += (long) num * l * r; } else { long cnt = (long) (k - r + 1) * r + (long) (l + r - k - 1) * (r + k - l) / 2; - ans += (long) num * cnt; + ans += num * cnt; } } return ans; From 8ed1db54752fa03fcf1fd6a51b70282dae286a41 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 02:40:34 +0200 Subject: [PATCH 08/10] Fixed coverage --- .../Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java index ea279314c..6875b956b 100644 --- a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java +++ b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/Solution.java @@ -47,9 +47,6 @@ private void buildFactorials(int n) { } private long comb(int n, int r) { - if (r < 0 || r > n) { - return 0; - } return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD; } From 8c1f652ef9cf6f43f1398b6577d70322471d0cc5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 02:56:50 +0200 Subject: [PATCH 09/10] Improved task 3428 --- .../readme.md | 68 +++---------------- 1 file changed, 9 insertions(+), 59 deletions(-) diff --git a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md index c17010603..83f0095da 100644 --- a/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md +++ b/src/main/java/g3401_3500/s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences/readme.md @@ -20,65 +20,15 @@ Since the answer may be very large, return it **modulo** 109 + The subsequences of `nums` with at most 2 elements are: -**Subsequence** - -Minimum - -Maximum - -Sum - -`[1]` - -1 - -1 - -2 - -`[2]` - -2 - -2 - -4 - -`[3]` - -3 - -3 - -6 - -`[1, 2]` - -1 - -2 - -3 - -`[1, 3]` - -1 - -3 - -4 - -`[2, 3]` - -2 - -3 - -5 - -**Final Total** - -24 +| **Subsequence** | Minimum | Maximum | Sum | +|-----------------|---------|---------|------| +| `[1]` | 1 | 1 | 2 | +| `[2]` | 2 | 2 | 4 | +| `[3]` | 3 | 3 | 6 | +| `[1, 2]` | 1 | 2 | 3 | +| `[1, 3]` | 1 | 3 | 4 | +| `[2, 3]` | 2 | 3 | 5 | +| **Final Total** | | | 24 | The output would be 24. From fcf371d7789e3627848210aee7c199541d520d83 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 22 Jan 2025 03:00:57 +0200 Subject: [PATCH 10/10] Improved 3427 --- .../readme.md | 74 ++++--------------- 1 file changed, 15 insertions(+), 59 deletions(-) diff --git a/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md b/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md index b40691b8c..a3d7f6d72 100644 --- a/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md +++ b/src/main/java/g3401_3500/s3427_sum_of_variable_length_subarrays/readme.md @@ -14,33 +14,12 @@ Return the total sum of all elements from the subarray defined for each index in **Explanation:** -i - -Subarray - -Sum - -0 - -`nums[0] = [2]` - -2 - -1 - -`nums[0 ... 1] = [2, 3]` - -5 - -2 - -`nums[1 ... 2] = [3, 1]` - -4 - -**Total Sum** - -11 +| i | Subarray | Sum | +|-----|------------------------------|-----| +| 0 | `nums[0] = [2]` | 2 | +| 1 | `nums[0 ... 1] = [2, 3]` | 5 | +| 2 | `nums[1 ... 2] = [3, 1]` | 4 | +| **Total Sum** | | 11 | The total sum is 11. Hence, 11 is the output. @@ -52,40 +31,17 @@ The total sum is 11. Hence, 11 is the output. **Explanation:** -i - -Subarray - -Sum - -0 - -`nums[0] = [3]` - -3 - -1 - -`nums[0 ... 1] = [3, 1]` - -4 - -2 - -`nums[1 ... 2] = [1, 1]` - -2 - -3 - -`nums[1 ... 3] = [1, 1, 2]` - -4 - -**Total Sum** +Here's the HTML table converted to Markdown: -13 +| i | Subarray | Sum | +|-----|------------------------------|-----| +| 0 | `nums[0] = [3]` | 3 | +| 1 | `nums[0 ... 1] = [3, 1]` | 4 | +| 2 | `nums[1 ... 2] = [1, 1]` | 2 | +| 3 | `nums[1 ... 3] = [1, 1, 2]` | 4 | +| **Total Sum** | | 13 | +This Markdown table replicates the structure and content of the original HTML table. The total sum is 13. Hence, 13 is the output. **Constraints:**