From 9d7e0e09cdfe6c2651b58b7c032dc60d80839812 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 27 Jun 2024 09:53:21 +0300 Subject: [PATCH 1/4] Added tasks 3190-3197 --- .../Solution.java | 15 +++ .../readme.md | 32 ++++++ .../Solution.java | 28 +++++ .../readme.md | 40 +++++++ .../Solution.java | 17 +++ .../readme.md | 43 ++++++++ .../Solution.java | 47 ++++++++ .../readme.md | 67 +++++++++++ .../Solution.java | 16 +++ .../readme.md | 66 +++++++++++ .../Solution.java | 23 ++++ .../readme.md | 37 +++++++ .../Solution.java | 18 +++ .../readme.md | 64 +++++++++++ .../Solution.java | 104 ++++++++++++++++++ .../readme.md | 43 ++++++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 27 +++++ .../SolutionTest.java | 24 ++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 28 +++++ .../SolutionTest.java | 18 +++ 24 files changed, 829 insertions(+) create mode 100644 src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/Solution.java create mode 100644 src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/readme.md create mode 100644 src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/Solution.java create mode 100644 src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/readme.md create mode 100644 src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/Solution.java create mode 100644 src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/readme.md create mode 100644 src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java create mode 100644 src/main/java/g3101_3200/s3193_count_the_number_of_inversions/readme.md create mode 100644 src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java create mode 100644 src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/readme.md create mode 100644 src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/Solution.java create mode 100644 src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/readme.md create mode 100644 src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/Solution.java create mode 100644 src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/readme.md create mode 100644 src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java create mode 100644 src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/readme.md create mode 100644 src/test/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3193_count_the_number_of_inversions/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/SolutionTest.java diff --git a/src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/Solution.java b/src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/Solution.java new file mode 100644 index 000000000..8577ff787 --- /dev/null +++ b/src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/Solution.java @@ -0,0 +1,15 @@ +package g3101_3200.s3190_find_minimum_operations_to_make_all_elements_divisible_by_three; + +// #Easy #Array #Math #2024_06_26_Time_0_ms_(100.00%)_Space_41.6_MB_(78.56%) + +public class Solution { + public int minimumOperations(int[] nums) { + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] % 3 != 0) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/readme.md b/src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/readme.md new file mode 100644 index 000000000..e91729d4e --- /dev/null +++ b/src/main/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/readme.md @@ -0,0 +1,32 @@ +3190\. Find Minimum Operations to Make All Elements Divisible by Three + +Easy + +You are given an integer array `nums`. In one operation, you can add or subtract 1 from **any** element of `nums`. + +Return the **minimum** number of operations to make all elements of `nums` divisible by 3. + +**Example 1:** + +**Input:** nums = [1,2,3,4] + +**Output:** 3 + +**Explanation:** + +All array elements can be made divisible by 3 using 3 operations: + +* Subtract 1 from 1. +* Add 1 to 2. +* Subtract 1 from 4. + +**Example 2:** + +**Input:** nums = [3,6,9] + +**Output:** 0 + +**Constraints:** + +* `1 <= nums.length <= 50` +* `1 <= nums[i] <= 50` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/Solution.java b/src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/Solution.java new file mode 100644 index 000000000..48803bb69 --- /dev/null +++ b/src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/Solution.java @@ -0,0 +1,28 @@ +package g3101_3200.s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i; + +// #Medium #Array #Bit_Manipulation #Prefix_Sum #Sliding_Window #Queue +// #2024_06_26_Time_6_ms_(99.99%)_Space_57.2_MB_(62.07%) + +public class Solution { + public int minOperations(int[] nums) { + int ans = 0; + // Iterate through the array up to the third-last element + for (int i = 0; i < nums.length - 2; i++) { + // If the current element is 0, perform an operation + if (nums[i] == 0) { + ans++; + // Flip the current element and the next two elements + nums[i] = 1; + nums[i + 1] = nums[i + 1] == 0 ? 1 : 0; + nums[i + 2] = nums[i + 2] == 0 ? 1 : 0; + } + } + // Check the last two elements if they are 0, return -1 as they cannot be flipped + for (int i = nums.length - 2; i < nums.length; i++) { + if (nums[i] == 0) { + return -1; + } + } + return ans; + } +} diff --git a/src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/readme.md b/src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/readme.md new file mode 100644 index 000000000..fcd72690c --- /dev/null +++ b/src/main/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/readme.md @@ -0,0 +1,40 @@ +3191\. Minimum Operations to Make Binary Array Elements Equal to One I + +Medium + +You are given a binary array `nums`. + +You can do the following operation on the array **any** number of times (possibly zero): + +* Choose **any** 3 **consecutive** elements from the array and **flip** **all** of them. + +**Flipping** an element means changing its value from 0 to 1, and from 1 to 0. + +Return the **minimum** number of operations required to make all elements in `nums` equal to 1. If it is impossible, return -1. + +**Example 1:** + +**Input:** nums = [0,1,1,1,0,0] + +**Output:** 3 + +**Explanation:** + We can do the following operations: + +* Choose the elements at indices 0, 1 and 2. The resulting array is nums = [**1**,**0**,**0**,1,0,0]. +* Choose the elements at indices 1, 2 and 3. The resulting array is nums = [1,**1**,**1**,**0**,0,0]. +* Choose the elements at indices 3, 4 and 5. The resulting array is nums = [1,1,1,**1**,**1**,**1**]. + +**Example 2:** + +**Input:** nums = [0,1,1,1] + +**Output:** \-1 + +**Explanation:** + It is impossible to make all elements equal to 1. + +**Constraints:** + +* 3 <= nums.length <= 105 +* `0 <= nums[i] <= 1` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/Solution.java b/src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/Solution.java new file mode 100644 index 000000000..66ed44007 --- /dev/null +++ b/src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/Solution.java @@ -0,0 +1,17 @@ +package g3101_3200.s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii; + +// #Medium #Array #Dynamic_Programming #Greedy #2024_06_26_Time_6_ms_(99.64%)_Space_62.9_MB_(17.52%) + +public class Solution { + public int minOperations(int[] nums) { + int a = 0; + int c = 1; + for (int x : nums) { + if (x != c) { + a++; + c ^= 1; + } + } + return a; + } +} diff --git a/src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/readme.md b/src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/readme.md new file mode 100644 index 000000000..be9ddd643 --- /dev/null +++ b/src/main/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/readme.md @@ -0,0 +1,43 @@ +3192\. Minimum Operations to Make Binary Array Elements Equal to One II + +Medium + +You are given a binary array `nums`. + +You can do the following operation on the array **any** number of times (possibly zero): + +* Choose **any** index `i` from the array and **flip** **all** the elements from index `i` to the end of the array. + +**Flipping** an element means changing its value from 0 to 1, and from 1 to 0. + +Return the **minimum** number of operations required to make all elements in `nums` equal to 1. + +**Example 1:** + +**Input:** nums = [0,1,1,0,1] + +**Output:** 4 + +**Explanation:** + We can do the following operations: + +* Choose the index `i = 1`. The resulting array will be nums = [0,**0**,**0**,**1**,**0**]. +* Choose the index `i = 0`. The resulting array will be nums = [**1**,**1**,**1**,**0**,**1**]. +* Choose the index `i = 4`. The resulting array will be nums = [1,1,1,0,**0**]. +* Choose the index `i = 3`. The resulting array will be nums = [1,1,1,**1**,**1**]. + +**Example 2:** + +**Input:** nums = [1,0,0,0] + +**Output:** 1 + +**Explanation:** + We can do the following operation: + +* Choose the index `i = 1`. The resulting array will be nums = [1,**1**,**1**,**1**]. + +**Constraints:** + +* 1 <= nums.length <= 105 +* `0 <= nums[i] <= 1` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java b/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java new file mode 100644 index 000000000..bfcdac354 --- /dev/null +++ b/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java @@ -0,0 +1,47 @@ +package g3101_3200.s3193_count_the_number_of_inversions; + +// #Hard #Array #Dynamic_Programming #2024_06_26_Time_11_ms_(96.54%)_Space_45.5_MB_(37.54%) + +import java.util.Arrays; + +public class Solution { + private final int mod = 1_000_000_007; + + public int numberOfPermutations(int n, int[][] r) { + Arrays.sort(r, (o1, o2) -> o1[0] - o2[0]); + if (r[0][0] == 0 && r[0][1] > 0) { + return 0; + } + int ri = r[0][0] == 0 ? 1 : 0; + long a = 1; + long t; + int[][] m = new int[n][401]; + m[0][0] = 1; + for (int i = 1; i < m.length; i++) { + m[i][0] = m[i - 1][0]; + for (int j = 1; j <= i; j++) { + m[i][j] = (m[i][j] + m[i][j - 1]) % mod; + m[i][j] = (m[i][j] + m[i - 1][j]) % mod; + } + for (int j = i + 1; j <= r[ri][1]; j++) { + m[i][j] = (m[i][j] + m[i][j - 1]) % mod; + m[i][j] = (m[i][j] + m[i - 1][j]) % mod; + m[i][j] = (m[i][j] - m[i - 1][j - i - 1]); + if (m[i][j] < 0) { + m[i][j] += mod; + } + } + if (r[ri][0] == i) { + t = m[i][r[ri][1]]; + if (t == 0) { + return 0; + } + Arrays.fill(m[i], 0); + m[i][r[ri][1]] = 1; + a = (a * t) % mod; + ri++; + } + } + return (int) a; + } +} diff --git a/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/readme.md b/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/readme.md new file mode 100644 index 000000000..0ca16695a --- /dev/null +++ b/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/readme.md @@ -0,0 +1,67 @@ +3193\. Count the Number of Inversions + +Hard + +You are given an integer `n` and a 2D array `requirements`, where requirements[i] = [endi, cnti] represents the end index and the **inversion** count of each requirement. + +A pair of indices `(i, j)` from an integer array `nums` is called an **inversion** if: + +* `i < j` and `nums[i] > nums[j]` + +Return the number of permutations `perm` of `[0, 1, 2, ..., n - 1]` such that for **all** `requirements[i]`, perm[0..endi] has exactly cnti inversions. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** n = 3, requirements = [[2,2],[0,0]] + +**Output:** 2 + +**Explanation:** + +The two permutations are: + +* `[2, 0, 1]` + * Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`. + * Prefix `[2]` has 0 inversions. +* `[1, 2, 0]` + * Prefix `[1, 2, 0]` has inversions `(0, 2)` and `(1, 2)`. + * Prefix `[1]` has 0 inversions. + +**Example 2:** + +**Input:** n = 3, requirements = [[2,2],[1,1],[0,0]] + +**Output:** 1 + +**Explanation:** + +The only satisfying permutation is `[2, 0, 1]`: + +* Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`. +* Prefix `[2, 0]` has an inversion `(0, 1)`. +* Prefix `[2]` has 0 inversions. + +**Example 3:** + +**Input:** n = 2, requirements = [[0,0],[1,0]] + +**Output:** 1 + +**Explanation:** + +The only satisfying permutation is `[0, 1]`: + +* Prefix `[0]` has 0 inversions. +* Prefix `[0, 1]` has an inversion `(0, 1)`. + +**Constraints:** + +* `2 <= n <= 300` +* `1 <= requirements.length <= n` +* requirements[i] = [endi, cnti] +* 0 <= endi <= n - 1 +* 0 <= cnti <= 400 +* The input is generated such that there is at least one `i` such that endi == n - 1. +* The input is generated such that all endi are unique. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java b/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java new file mode 100644 index 000000000..b6ba0829f --- /dev/null +++ b/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java @@ -0,0 +1,16 @@ +package g3101_3200.s3194_minimum_average_of_smallest_and_largest_elements; + +// #Easy #Array #Sorting #Two_Pointers #2024_06_26_Time_2_ms_(98.88%)_Space_43.5_MB_(88.12%) + +import java.util.Arrays; + +public class Solution { + public double minimumAverage(int[] nums) { + Arrays.sort(nums); + double m = 102.0; + for (int i = 0, l = nums.length; i < l / 2; i++) { + m = Math.min(m, nums[i] + nums[l - i - 1]); + } + return m / 2.0; + } +} diff --git a/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/readme.md b/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/readme.md new file mode 100644 index 000000000..4c1801f3d --- /dev/null +++ b/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/readme.md @@ -0,0 +1,66 @@ +3194\. Minimum Average of Smallest and Largest Elements + +Easy + +You have an array of floating point numbers `averages` which is initially empty. You are given an array `nums` of `n` integers where `n` is even. + +You repeat the following procedure `n / 2` times: + +* Remove the **smallest** element, `minElement`, and the **largest** element `maxElement`, from `nums`. +* Add `(minElement + maxElement) / 2` to `averages`. + +Return the **minimum** element in `averages`. + +**Example 1:** + +**Input:** nums = [7,8,3,4,15,13,4,1] + +**Output:** 5.5 + +**Explanation:** + +| Step | nums | averages | +|------|------------------|------------| +| 0 | [7,8,3,4,15,13,4,1] | [] | +| 1 | [7,8,3,4,13,4] | [8] | +| 2 | [7,8,4,4] | [8, 8] | +| 3 | [7,4] | [8, 8, 6] | +| 4 | [] | [8, 8, 6, 5.5] | + +The smallest element of averages, 5.5, is returned. + +**Example 2:** + +**Input:** nums = [1,9,8,3,10,5] + +**Output:** 5.5 + +**Explanation:** + +| Step | nums | averages | +|------|----------------|------------| +| 0 | [1,9,8,3,10,5] | [] | +| 1 | [9,8,3,5] | [5.5] | +| 2 | [8,5] | [5.5, 6] | +| 3 | [] | [5.5, 6, 6.5] | + +**Example 3:** + +**Input:** nums = [1,2,3,7,8,9] + +**Output:** 5.0 + +**Explanation:** + +| Step | nums | averages | +|------|----------------|------------| +| 0 | [1,2,3,7,8,9] | [] | +| 1 | [2,3,7,8] | [5] | +| 2 | [3,7] | [5, 5] | +| 3 | [] | [5, 5, 5] | + +**Constraints:** + +* `2 <= n == nums.length <= 50` +* `n` is even. +* `1 <= nums[i] <= 50` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/Solution.java b/src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/Solution.java new file mode 100644 index 000000000..c982ccf8a --- /dev/null +++ b/src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/Solution.java @@ -0,0 +1,23 @@ +package g3101_3200.s3195_find_the_minimum_area_to_cover_all_ones_i; + +// #Medium #Array #Matrix #2024_06_26_Time_5_ms_(94.40%)_Space_197.2_MB_(14.87%) + +public class Solution { + public int minimumArea(int[][] grid) { + int xmin = Integer.MAX_VALUE; + int xmax = -1; + int ymin = Integer.MAX_VALUE; + int ymax = -1; + for (int i = 0, m = grid.length, n = grid[0].length; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] == 1) { + xmin = Math.min(xmin, i); + xmax = Math.max(xmax, i); + ymin = Math.min(ymin, j); + ymax = Math.max(ymax, j); + } + } + } + return (xmax - xmin + 1) * (ymax - ymin + 1); + } +} diff --git a/src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/readme.md b/src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/readme.md new file mode 100644 index 000000000..2467b25a0 --- /dev/null +++ b/src/main/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/readme.md @@ -0,0 +1,37 @@ +3195\. Find the Minimum Area to Cover All Ones I + +Medium + +You are given a 2D **binary** array `grid`. Find a rectangle with horizontal and vertical sides with the **smallest** area, such that all the 1's in `grid` lie inside this rectangle. + +Return the **minimum** possible area of the rectangle. + +**Example 1:** + +**Input:** grid = [[0,1,0],[1,0,1]] + +**Output:** 6 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png) + +The smallest rectangle has a height of 2 and a width of 3, so it has an area of `2 * 3 = 6`. + +**Example 2:** + +**Input:** grid = [[1,0],[0,0]] + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png) + +The smallest rectangle has both height and width 1, so its area is `1 * 1 = 1`. + +**Constraints:** + +* `1 <= grid.length, grid[i].length <= 1000` +* `grid[i][j]` is either 0 or 1. +* The input is generated such that there is at least one 1 in `grid`. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/Solution.java b/src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/Solution.java new file mode 100644 index 000000000..274f76417 --- /dev/null +++ b/src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/Solution.java @@ -0,0 +1,18 @@ +package g3101_3200.s3196_maximize_total_cost_of_alternating_subarrays; + +// #Medium #Array #Dynamic_Programming #2024_06_26_Time_1_ms_(100.00%)_Space_61.5_MB_(68.90%) + +public class Solution { + public long maximumTotalCost(int[] nums) { + int n = nums.length; + long addResult = nums[0]; + long subResult = nums[0]; + for (int i = 1; i < n; i++) { + long tempAdd = Math.max(addResult, subResult) + nums[i]; + long tempSub = addResult - nums[i]; + addResult = tempAdd; + subResult = tempSub; + } + return Math.max(addResult, subResult); + } +} diff --git a/src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/readme.md b/src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/readme.md new file mode 100644 index 000000000..03cb06af3 --- /dev/null +++ b/src/main/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/readme.md @@ -0,0 +1,64 @@ +3196\. Maximize Total Cost of Alternating Subarrays + +Medium + +You are given an integer array `nums` with length `n`. + +The **cost** of a subarray `nums[l..r]`, where `0 <= l <= r < n`, is defined as: + +cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (−1)r − l + +Your task is to **split** `nums` into subarrays such that the **total** **cost** of the subarrays is **maximized**, ensuring each element belongs to **exactly one** subarray. + +Formally, if `nums` is split into `k` subarrays, where `k > 1`, at indices i1, i2, ..., ik − 1, where 0 <= i1 < i2 < ... < ik - 1 < n - 1, then the total cost will be: + +cost(0, i1) + cost(i1 + 1, i2) + ... + cost(ik − 1 + 1, n − 1) + +Return an integer denoting the _maximum total cost_ of the subarrays after splitting the array optimally. + +**Note:** If `nums` is not split into subarrays, i.e. `k = 1`, the total cost is simply `cost(0, n - 1)`. + +**Example 1:** + +**Input:** nums = [1,-2,3,4] + +**Output:** 10 + +**Explanation:** + +One way to maximize the total cost is by splitting `[1, -2, 3, 4]` into subarrays `[1, -2, 3]` and `[4]`. The total cost will be `(1 + 2 + 3) + 4 = 10`. + +**Example 2:** + +**Input:** nums = [1,-1,1,-1] + +**Output:** 4 + +**Explanation:** + +One way to maximize the total cost is by splitting `[1, -1, 1, -1]` into subarrays `[1, -1]` and `[1, -1]`. The total cost will be `(1 + 1) + (1 + 1) = 4`. + +**Example 3:** + +**Input:** nums = [0] + +**Output:** 0 + +**Explanation:** + +We cannot split the array further, so the answer is 0. + +**Example 4:** + +**Input:** nums = [1,-1] + +**Output:** 2 + +**Explanation:** + +Selecting the whole array gives a total cost of `1 + 1 = 2`, which is the maximum. + +**Constraints:** + +* 1 <= nums.length <= 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java new file mode 100644 index 000000000..3631cbdb8 --- /dev/null +++ b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java @@ -0,0 +1,104 @@ +package g3101_3200.s3197_find_the_minimum_area_to_cover_all_ones_ii; + +// #Hard #Array #Matrix #Enumeration #2024_06_26_Time_10_ms_(99.66%)_Space_44.1_MB_(85.42%) + +public class Solution { + private int[][] ruc; // rectangle unit count + private int height; + private int width; + + // r0, c0 incl., r1, c1 excl. + private int unitsInRectangle(int r0, int c0, int r1, int c1) { + return ruc[r1][c1] - ruc[r0][c1] - ruc[r1][c0] + ruc[r0][c0]; + } + + private int minArea(int r0, int c0, int r1, int c1) { + if (unitsInRectangle(r0, c0, r1, c1) == 0) return 0; + int minRow = r0; + while (unitsInRectangle(r0, c0, minRow + 1, c1) == 0) minRow++; + int maxRow = r1 - 1; + while (unitsInRectangle(maxRow, c0, r1, c1) == 0) maxRow--; + int minCol = c0; + while (unitsInRectangle(r0, c0, r1, minCol + 1) == 0) minCol++; + int maxCol = c1 - 1; + while (unitsInRectangle(r0, maxCol, r1, c1) == 0) maxCol--; + return (maxRow - minRow + 1) * (maxCol - minCol + 1); + } + + private int minSum2(int r0, int c0, int r1, int c1, boolean splitVertical) { + int min = Integer.MAX_VALUE; + if (splitVertical) + for (int c = c0 + 1; c < c1; c++) { + int a1 = minArea(r0, c0, r1, c); + if (a1 == 0) continue; + int a2 = minArea(r0, c, r1, c1); + if (a2 != 0) min = Math.min(min, a1 + a2); + } + else + for (int r = r0 + 1; r < r1; r++) { + int a1 = minArea(r0, c0, r, c1); + if (a1 == 0) continue; + int a2 = minArea(r, c0, r1, c1); + if (a2 != 0) min = Math.min(min, a1 + a2); + } + return min; + } + + private int minSum3( + boolean firstSplitVertical, boolean takeLower, boolean secondSplitVertical) { + int min = Integer.MAX_VALUE; + if (firstSplitVertical) + for (int c = 1; c < width; c++) { + int a1, a2; + if (takeLower) { + a1 = minArea(0, 0, height, c); + if (a1 == 0) continue; + a2 = minSum2(0, c, height, width, secondSplitVertical); + } else { + a1 = minArea(0, c, height, width); + if (a1 == 0) continue; + a2 = minSum2(0, 0, height, c, secondSplitVertical); + } + if (a2 != Integer.MAX_VALUE) min = Math.min(min, a1 + a2); + } + else + for (int r = 1; r < height; r++) { + int a1, a2; + if (takeLower) { + a1 = minArea(0, 0, r, width); + if (a1 == 0) continue; + a2 = minSum2(r, 0, height, width, secondSplitVertical); + } else { + a1 = minArea(r, 0, height, width); + if (a1 == 0) continue; + a2 = minSum2(0, 0, r, width, secondSplitVertical); + } + if (a2 != Integer.MAX_VALUE) min = Math.min(min, a1 + a2); + } + return min; + } + + public int minimumSum(int[][] grid) { + height = grid.length; + width = grid[0].length; + ruc = new int[height + 1][width + 1]; + for (int i = 0; i < height; i++) { + int[] gRow = grid[i]; + int[] cRow0 = ruc[i]; + int[] cRow1 = ruc[i + 1]; + int c = 0; + for (int j = 0; j < width; j++) { + c += gRow[j]; + cRow1[j + 1] = cRow0[j + 1] + c; + } + } + int min = Integer.MAX_VALUE; + min = Math.min(min, minSum3(true, true, true)); + min = Math.min(min, minSum3(true, true, false)); + min = Math.min(min, minSum3(true, false, false)); + min = Math.min(min, minSum3(false, true, true)); + min = Math.min(min, minSum3(false, true, false)); + min = Math.min(min, minSum3(false, false, true)); + return min; + } +} diff --git a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/readme.md b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/readme.md new file mode 100644 index 000000000..e1d35dbbb --- /dev/null +++ b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/readme.md @@ -0,0 +1,43 @@ +3197\. Find the Minimum Area to Cover All Ones II + +Hard + +You are given a 2D **binary** array `grid`. You need to find 3 **non-overlapping** rectangles having **non-zero** areas with horizontal and vertical sides such that all the 1's in `grid` lie inside these rectangles. + +Return the **minimum** possible sum of the area of these rectangles. + +**Note** that the rectangles are allowed to touch. + +**Example 1:** + +**Input:** grid = [[1,0,1],[1,1,1]] + +**Output:** 5 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/14/example0rect21.png) + +* The 1's at `(0, 0)` and `(1, 0)` are covered by a rectangle of area 2. +* The 1's at `(0, 2)` and `(1, 2)` are covered by a rectangle of area 2. +* The 1 at `(1, 1)` is covered by a rectangle of area 1. + +**Example 2:** + +**Input:** grid = [[1,0,1,0],[0,1,0,1]] + +**Output:** 5 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/05/14/example1rect2.png) + +* The 1's at `(0, 0)` and `(0, 2)` are covered by a rectangle of area 3. +* The 1 at `(1, 1)` is covered by a rectangle of area 1. +* The 1 at `(1, 3)` is covered by a rectangle of area 1. + +**Constraints:** + +* `1 <= grid.length, grid[i].length <= 30` +* `grid[i][j]` is either 0 or 1. +* The input is generated such that there are at least three 1's in `grid`. \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/SolutionTest.java b/src/test/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/SolutionTest.java new file mode 100644 index 000000000..d03514352 --- /dev/null +++ b/src/test/java/g3101_3200/s3190_find_minimum_operations_to_make_all_elements_divisible_by_three/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3190_find_minimum_operations_to_make_all_elements_divisible_by_three; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumOperations() { + assertThat(new Solution().minimumOperations(new int[] {1, 2, 3, 4}), equalTo(3)); + } + + @Test + void minimumOperations2() { + assertThat(new Solution().minimumOperations(new int[] {3, 6, 9}), equalTo(0)); + } +} diff --git a/src/test/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/SolutionTest.java b/src/test/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/SolutionTest.java new file mode 100644 index 000000000..b42f7f3ee --- /dev/null +++ b/src/test/java/g3101_3200/s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_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[] {0, 1, 1, 1, 0, 0}), equalTo(3)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {0, 1, 1, 1}), equalTo(-1)); + } +} diff --git a/src/test/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/SolutionTest.java b/src/test/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/SolutionTest.java new file mode 100644 index 000000000..418be8584 --- /dev/null +++ b/src/test/java/g3101_3200/s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_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[] {0, 1, 1, 0, 1}), equalTo(4)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {1, 0, 0, 0}), equalTo(1)); + } +} diff --git a/src/test/java/g3101_3200/s3193_count_the_number_of_inversions/SolutionTest.java b/src/test/java/g3101_3200/s3193_count_the_number_of_inversions/SolutionTest.java new file mode 100644 index 000000000..058aedf63 --- /dev/null +++ b/src/test/java/g3101_3200/s3193_count_the_number_of_inversions/SolutionTest.java @@ -0,0 +1,27 @@ +package g3101_3200.s3193_count_the_number_of_inversions; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfPermutations() { + assertThat( + new Solution().numberOfPermutations(3, new int[][] {{2, 2}, {0, 0}}), equalTo(2)); + } + + @Test + void numberOfPermutations2() { + assertThat( + new Solution().numberOfPermutations(3, new int[][] {{2, 2}, {1, 1}, {0, 0}}), + equalTo(1)); + } + + @Test + void numberOfPermutations3() { + assertThat( + new Solution().numberOfPermutations(2, new int[][] {{0, 0}, {1, 0}}), equalTo(1)); + } +} diff --git a/src/test/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/SolutionTest.java b/src/test/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/SolutionTest.java new file mode 100644 index 000000000..7ac021d11 --- /dev/null +++ b/src/test/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/SolutionTest.java @@ -0,0 +1,24 @@ +package g3101_3200.s3194_minimum_average_of_smallest_and_largest_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumAverage() { + assertThat( + new Solution().minimumAverage(new int[] {7, 8, 3, 4, 15, 13, 4, 1}), equalTo(5.5)); + } + + @Test + void minimumAverage2() { + assertThat(new Solution().minimumAverage(new int[] {1, 9, 8, 3, 10, 5}), equalTo(5.5)); + } + + @Test + void minimumAverage3() { + assertThat(new Solution().minimumAverage(new int[] {1, 2, 3, 7, 8, 9}), equalTo(5.0)); + } +} diff --git a/src/test/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/SolutionTest.java b/src/test/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/SolutionTest.java new file mode 100644 index 000000000..1b1beeda8 --- /dev/null +++ b/src/test/java/g3101_3200/s3195_find_the_minimum_area_to_cover_all_ones_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3195_find_the_minimum_area_to_cover_all_ones_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumArea() { + assertThat(new Solution().minimumArea(new int[][] {{0, 1, 0}, {1, 0, 1}}), equalTo(6)); + } + + @Test + void minimumArea2() { + assertThat(new Solution().minimumArea(new int[][] {{1, 0}, {0, 0}}), equalTo(1)); + } +} diff --git a/src/test/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/SolutionTest.java b/src/test/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/SolutionTest.java new file mode 100644 index 000000000..c19c9163f --- /dev/null +++ b/src/test/java/g3101_3200/s3196_maximize_total_cost_of_alternating_subarrays/SolutionTest.java @@ -0,0 +1,28 @@ +package g3101_3200.s3196_maximize_total_cost_of_alternating_subarrays; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumTotalCost() { + assertThat(new Solution().maximumTotalCost(new int[] {1, -2, 3, 4}), equalTo(10L)); + } + + @Test + void maximumTotalCost2() { + assertThat(new Solution().maximumTotalCost(new int[] {1, -1, 1, -1}), equalTo(4L)); + } + + @Test + void maximumTotalCost3() { + assertThat(new Solution().maximumTotalCost(new int[] {0}), equalTo(0L)); + } + + @Test + void maximumTotalCost4() { + assertThat(new Solution().maximumTotalCost(new int[] {1, -1}), equalTo(2L)); + } +} diff --git a/src/test/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/SolutionTest.java b/src/test/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/SolutionTest.java new file mode 100644 index 000000000..e1f8185ce --- /dev/null +++ b/src/test/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3197_find_the_minimum_area_to_cover_all_ones_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumSum() { + assertThat(new Solution().minimumSum(new int[][] {{1, 0, 1}, {1, 1, 1}}), equalTo(5)); + } + + @Test + void minimumSum2() { + assertThat(new Solution().minimumSum(new int[][] {{1, 0, 1, 0}, {0, 1, 0, 1}}), equalTo(5)); + } +} From 74c45b85d00d9d510719af4d15cb819b07d28572 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 27 Jun 2024 10:01:33 +0300 Subject: [PATCH 2/4] Fixed style --- .../Solution.java | 65 +++++++++++++------ 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java index 3631cbdb8..258e3bc13 100644 --- a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java +++ b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java @@ -3,7 +3,8 @@ // #Hard #Array #Matrix #Enumeration #2024_06_26_Time_10_ms_(99.66%)_Space_44.1_MB_(85.42%) public class Solution { - private int[][] ruc; // rectangle unit count + // rectangle unit count + private int[][] ruc; private int height; private int width; @@ -13,33 +14,47 @@ private int unitsInRectangle(int r0, int c0, int r1, int c1) { } private int minArea(int r0, int c0, int r1, int c1) { - if (unitsInRectangle(r0, c0, r1, c1) == 0) return 0; + if (unitsInRectangle(r0, c0, r1, c1) == 0) { + return 0; + } int minRow = r0; - while (unitsInRectangle(r0, c0, minRow + 1, c1) == 0) minRow++; + while (unitsInRectangle(r0, c0, minRow + 1, c1) == 0) { + minRow++; + } int maxRow = r1 - 1; - while (unitsInRectangle(maxRow, c0, r1, c1) == 0) maxRow--; + while (unitsInRectangle(maxRow, c0, r1, c1) == 0) { + maxRow--; + } int minCol = c0; - while (unitsInRectangle(r0, c0, r1, minCol + 1) == 0) minCol++; + while (unitsInRectangle(r0, c0, r1, minCol + 1) == 0) { + minCol++; + } int maxCol = c1 - 1; - while (unitsInRectangle(r0, maxCol, r1, c1) == 0) maxCol--; + while (unitsInRectangle(r0, maxCol, r1, c1) == 0) { + maxCol--; + } return (maxRow - minRow + 1) * (maxCol - minCol + 1); } private int minSum2(int r0, int c0, int r1, int c1, boolean splitVertical) { int min = Integer.MAX_VALUE; - if (splitVertical) + if (splitVertical) { for (int c = c0 + 1; c < c1; c++) { int a1 = minArea(r0, c0, r1, c); if (a1 == 0) continue; int a2 = minArea(r0, c, r1, c1); if (a2 != 0) min = Math.min(min, a1 + a2); } - else + } else for (int r = r0 + 1; r < r1; r++) { int a1 = minArea(r0, c0, r, c1); - if (a1 == 0) continue; + if (a1 == 0) { + continue; + } int a2 = minArea(r, c0, r1, c1); - if (a2 != 0) min = Math.min(min, a1 + a2); + if (a2 != 0) { + min = Math.min(min, a1 + a2); + } } return min; } @@ -47,33 +62,45 @@ private int minSum2(int r0, int c0, int r1, int c1, boolean splitVertical) { private int minSum3( boolean firstSplitVertical, boolean takeLower, boolean secondSplitVertical) { int min = Integer.MAX_VALUE; - if (firstSplitVertical) + if (firstSplitVertical) { for (int c = 1; c < width; c++) { - int a1, a2; + int a1; + int a2; if (takeLower) { a1 = minArea(0, 0, height, c); - if (a1 == 0) continue; + if (a1 == 0) { + continue; + } a2 = minSum2(0, c, height, width, secondSplitVertical); } else { a1 = minArea(0, c, height, width); - if (a1 == 0) continue; + if (a1 == 0) { + continue; + } a2 = minSum2(0, 0, height, c, secondSplitVertical); } if (a2 != Integer.MAX_VALUE) min = Math.min(min, a1 + a2); } - else + } else for (int r = 1; r < height; r++) { - int a1, a2; + int a1; + int a2; if (takeLower) { a1 = minArea(0, 0, r, width); - if (a1 == 0) continue; + if (a1 == 0) { + continue; + } a2 = minSum2(r, 0, height, width, secondSplitVertical); } else { a1 = minArea(r, 0, height, width); - if (a1 == 0) continue; + if (a1 == 0) { + continue; + } a2 = minSum2(0, 0, r, width, secondSplitVertical); } - if (a2 != Integer.MAX_VALUE) min = Math.min(min, a1 + a2); + if (a2 != Integer.MAX_VALUE) { + min = Math.min(min, a1 + a2); + } } return min; } From 1af5dfb8e6fcd3d640827e3a44f7a35b8525a2f9 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 27 Jun 2024 10:19:40 +0300 Subject: [PATCH 3/4] Fixed style --- .../Solution.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java index 258e3bc13..1647343ad 100644 --- a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java +++ b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java @@ -41,11 +41,15 @@ private int minSum2(int r0, int c0, int r1, int c1, boolean splitVertical) { if (splitVertical) { for (int c = c0 + 1; c < c1; c++) { int a1 = minArea(r0, c0, r1, c); - if (a1 == 0) continue; + if (a1 == 0) { + continue; + } int a2 = minArea(r0, c, r1, c1); - if (a2 != 0) min = Math.min(min, a1 + a2); + if (a2 != 0) { + min = Math.min(min, a1 + a2); + } } - } else + } else { for (int r = r0 + 1; r < r1; r++) { int a1 = minArea(r0, c0, r, c1); if (a1 == 0) { @@ -56,6 +60,7 @@ private int minSum2(int r0, int c0, int r1, int c1, boolean splitVertical) { min = Math.min(min, a1 + a2); } } + } return min; } @@ -79,9 +84,11 @@ private int minSum3( } a2 = minSum2(0, 0, height, c, secondSplitVertical); } - if (a2 != Integer.MAX_VALUE) min = Math.min(min, a1 + a2); + if (a2 != Integer.MAX_VALUE) { + min = Math.min(min, a1 + a2); + } } - } else + } else { for (int r = 1; r < height; r++) { int a1; int a2; @@ -102,6 +109,7 @@ private int minSum3( min = Math.min(min, a1 + a2); } } + } return min; } From 9ed773b6f9a94d8531e6dcc86ebe38da6f08872e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 27 Jun 2024 10:23:11 +0300 Subject: [PATCH 4/4] Fixed sonar --- .../Solution.java | 14 +++++++------- .../Solution.java | 2 +- .../Solution.java | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java b/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java index bfcdac354..71c7a2565 100644 --- a/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java +++ b/src/main/java/g3101_3200/s3193_count_the_number_of_inversions/Solution.java @@ -5,7 +5,7 @@ import java.util.Arrays; public class Solution { - private final int mod = 1_000_000_007; + private static final int MOD = 1_000_000_007; public int numberOfPermutations(int n, int[][] r) { Arrays.sort(r, (o1, o2) -> o1[0] - o2[0]); @@ -20,15 +20,15 @@ public int numberOfPermutations(int n, int[][] r) { for (int i = 1; i < m.length; i++) { m[i][0] = m[i - 1][0]; for (int j = 1; j <= i; j++) { - m[i][j] = (m[i][j] + m[i][j - 1]) % mod; - m[i][j] = (m[i][j] + m[i - 1][j]) % mod; + m[i][j] = (m[i][j] + m[i][j - 1]) % MOD; + m[i][j] = (m[i][j] + m[i - 1][j]) % MOD; } for (int j = i + 1; j <= r[ri][1]; j++) { - m[i][j] = (m[i][j] + m[i][j - 1]) % mod; - m[i][j] = (m[i][j] + m[i - 1][j]) % mod; + m[i][j] = (m[i][j] + m[i][j - 1]) % MOD; + m[i][j] = (m[i][j] + m[i - 1][j]) % MOD; m[i][j] = (m[i][j] - m[i - 1][j - i - 1]); if (m[i][j] < 0) { - m[i][j] += mod; + m[i][j] += MOD; } } if (r[ri][0] == i) { @@ -38,7 +38,7 @@ public int numberOfPermutations(int n, int[][] r) { } Arrays.fill(m[i], 0); m[i][r[ri][1]] = 1; - a = (a * t) % mod; + a = (a * t) % MOD; ri++; } } diff --git a/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java b/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java index b6ba0829f..dbbb54f70 100644 --- a/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java +++ b/src/main/java/g3101_3200/s3194_minimum_average_of_smallest_and_largest_elements/Solution.java @@ -9,7 +9,7 @@ public double minimumAverage(int[] nums) { Arrays.sort(nums); double m = 102.0; for (int i = 0, l = nums.length; i < l / 2; i++) { - m = Math.min(m, nums[i] + nums[l - i - 1]); + m = Math.min(m, nums[i] + (double) nums[l - i - 1]); } return m / 2.0; } diff --git a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java index 1647343ad..ffd6187a4 100644 --- a/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java +++ b/src/main/java/g3101_3200/s3197_find_the_minimum_area_to_cover_all_ones_ii/Solution.java @@ -2,6 +2,7 @@ // #Hard #Array #Matrix #Enumeration #2024_06_26_Time_10_ms_(99.66%)_Space_44.1_MB_(85.42%) +@SuppressWarnings("java:S135") public class Solution { // rectangle unit count private int[][] ruc;