diff --git a/src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.java b/src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.java new file mode 100644 index 000000000..a52cbe464 --- /dev/null +++ b/src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.java @@ -0,0 +1,29 @@ +package g3101_3200.s3142_check_if_grid_satisfies_conditions; + +// #Easy #Array #Matrix #2024_05_15_Time_1_ms_(95.76%)_Space_44.4_MB_(59.70%) + +public class Solution { + public boolean satisfiesConditions(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + for (int i = 0; i < m - 1; i++) { + if (n > 1) { + for (int j = 0; j < n - 1; j++) { + if ((grid[i][j] != grid[i + 1][j]) || (grid[i][j] == grid[i][j + 1])) { + return false; + } + } + } else { + if (grid[i][0] != grid[i + 1][0]) { + return false; + } + } + } + for (int j = 0; j < n - 1; j++) { + if (grid[m - 1][j] == grid[m - 1][j + 1]) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md b/src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md new file mode 100644 index 000000000..cafc24506 --- /dev/null +++ b/src/main/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md @@ -0,0 +1,51 @@ +3142\. Check if Grid Satisfies Conditions + +Easy + +You are given a 2D matrix `grid` of size `m x n`. You need to check if each cell `grid[i][j]` is: + +* Equal to the cell below it, i.e. `grid[i][j] == grid[i + 1][j]` (if it exists). +* Different from the cell to its right, i.e. `grid[i][j] != grid[i][j + 1]` (if it exists). + +Return `true` if **all** the cells satisfy these conditions, otherwise, return `false`. + +**Example 1:** + +**Input:** grid = [[1,0,2],[1,0,2]] + +**Output:** true + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/15/examplechanged.png)** + +All the cells in the grid satisfy the conditions. + +**Example 2:** + +**Input:** grid = [[1,1,1],[0,0,0]] + +**Output:** false + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/03/27/example21.png)** + +All cells in the first row are equal. + +**Example 3:** + +**Input:** grid = [[1],[2],[3]] + +**Output:** false + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/03/31/changed.png) + +Cells in the first column have different values. + +**Constraints:** + +* `1 <= n, m <= 10` +* `0 <= grid[i][j] <= 9` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/Solution.java b/src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/Solution.java new file mode 100644 index 000000000..a07b56db8 --- /dev/null +++ b/src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/Solution.java @@ -0,0 +1,33 @@ +package g3101_3200.s3143_maximum_points_inside_the_square; + +// #Medium #Array #String #Hash_Table #Sorting #Binary_Search +// #2024_05_15_Time_2_ms_(100.00%)_Space_100.1_MB_(61.27%) + +import java.util.Arrays; + +public class Solution { + public int maxPointsInsideSquare(int[][] points, String s) { + int[] tags = new int[26]; + Arrays.fill(tags, Integer.MAX_VALUE); + int secondMin = Integer.MAX_VALUE; + for (int i = 0; i < s.length(); i++) { + int dist = Math.max(Math.abs(points[i][0]), Math.abs(points[i][1])); + char c = s.charAt(i); + if (tags[c - 'a'] == Integer.MAX_VALUE) { + tags[c - 'a'] = dist; + } else if (dist < tags[c - 'a']) { + secondMin = Math.min(secondMin, tags[c - 'a']); + tags[c - 'a'] = dist; + } else { + secondMin = Math.min(secondMin, dist); + } + } + int count = 0; + for (int dist : tags) { + if (dist < secondMin) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/readme.md b/src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/readme.md new file mode 100644 index 000000000..621f127dd --- /dev/null +++ b/src/main/java/g3101_3200/s3143_maximum_points_inside_the_square/readme.md @@ -0,0 +1,57 @@ +3143\. Maximum Points Inside the Square + +Medium + +You are given a 2D array `points` and a string `s` where, `points[i]` represents the coordinates of point `i`, and `s[i]` represents the **tag** of point `i`. + +A **valid** square is a square centered at the origin `(0, 0)`, has edges parallel to the axes, and **does not** contain two points with the same tag. + +Return the **maximum** number of points contained in a **valid** square. + +Note: + +* A point is considered to be inside the square if it lies on or within the square's boundaries. +* The side length of the square can be zero. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc1.png) + +**Input:** points = [[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s = "abdca" + +**Output:** 2 + +**Explanation:** + +The square of side length 4 covers two points `points[0]` and `points[1]`. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc2.png) + +**Input:** points = [[1,1],[-2,-2],[-2,2]], s = "abb" + +**Output:** 1 + +**Explanation:** + +The square of side length 2 covers one point, which is `points[0]`. + +**Example 3:** + +**Input:** points = [[1,1],[-1,-1],[2,-2]], s = "ccd" + +**Output:** 0 + +**Explanation:** + +It's impossible to make any valid squares centered at the origin such that it covers only one point among `points[0]` and `points[1]`. + +**Constraints:** + +* 1 <= s.length, points.length <= 105 +* `points[i].length == 2` +* -109 <= points[i][0], points[i][1] <= 109 +* `s.length == points.length` +* `points` consists of distinct coordinates. +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.java b/src/main/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.java new file mode 100644 index 000000000..07da190fe --- /dev/null +++ b/src/main/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.java @@ -0,0 +1,34 @@ +package g3101_3200.s3144_minimum_substring_partition_of_equal_character_frequency; + +// #Medium #String #Hash_Table #Dynamic_Programming #Counting +// #2024_05_15_Time_37_ms_(100.00%)_Space_44.9_MB_(72.95%) + +import java.util.Arrays; + +public class Solution { + public int minimumSubstringsInPartition(String s) { + char[] cs = s.toCharArray(); + int n = cs.length; + int[] dp = new int[n + 1]; + Arrays.fill(dp, n); + dp[0] = 0; + for (int i = 1; i <= n; ++i) { + int[] count = new int[26]; + int distinct = 0; + int maxCount = 0; + for (int j = i - 1; j >= 0; --j) { + int index = cs[j] - 'a'; + if (++count[index] == 1) { + distinct++; + } + if (count[index] > maxCount) { + maxCount = count[index]; + } + if (maxCount * distinct == i - j) { + dp[i] = Math.min(dp[i], dp[j] + 1); + } + } + } + return dp[n]; + } +} diff --git a/src/main/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md b/src/main/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md new file mode 100644 index 000000000..c8e5a8c73 --- /dev/null +++ b/src/main/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md @@ -0,0 +1,34 @@ +3144\. Minimum Substring Partition of Equal Character Frequency + +Medium + +Given a string `s`, you need to partition it into one or more **balanced** substrings. For example, if `s == "ababcc"` then `("abab", "c", "c")`, `("ab", "abc", "c")`, and `("ababcc")` are all valid partitions, but ("a", **"bab"**, "cc"), (**"aba"**, "bc", "c"), and ("ab", **"abcc"**) are not. The unbalanced substrings are bolded. + +Return the **minimum** number of substrings that you can partition `s` into. + +**Note:** A **balanced** string is a string where each character in the string occurs the same number of times. + +**Example 1:** + +**Input:** s = "fabccddg" + +**Output:** 3 + +**Explanation:** + +We can partition the string `s` into 3 substrings in one of the following ways: `("fab, "ccdd", "g")`, or `("fabc", "cd", "dg")`. + +**Example 2:** + +**Input:** s = "abababaccddb" + +**Output:** 2 + +**Explanation:** + +We can partition the string `s` into 2 substrings like so: `("abab", "abaccddb")`. + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s` consists only of English lowercase letters. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.java b/src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.java new file mode 100644 index 000000000..91041dedb --- /dev/null +++ b/src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.java @@ -0,0 +1,55 @@ +package g3101_3200.s3145_find_products_of_elements_of_big_array; + +// #Hard #Array #Binary_Search #Bit_Manipulation +// #2024_05_15_Time_3_ms_(98.41%)_Space_44.5_MB_(96.83%) + +public class Solution { + public int[] findProductsOfElements(long[][] queries) { + int[] ans = new int[queries.length]; + for (int i = 0; i < queries.length; i++) { + long[] q = queries[i]; + long er = sumE(q[1] + 1); + long el = sumE(q[0]); + ans[i] = pow(2, er - el, q[2]); + } + return ans; + } + + private long sumE(long k) { + long res = 0; + long n = 0; + long cnt1 = 0; + long sumI = 0; + for (long i = 63L - Long.numberOfLeadingZeros(k + 1); i > 0; i--) { + long c = (cnt1 << i) + (i << (i - 1)); + if (c <= k) { + k -= c; + res += (sumI << i) + ((i * (i - 1) / 2) << (i - 1)); + sumI += i; + cnt1++; + n |= 1L << i; + } + } + if (cnt1 <= k) { + k -= cnt1; + res += sumI; + n++; + } + while (k-- > 0) { + res += Long.numberOfTrailingZeros(n); + n &= n - 1; + } + return res; + } + + private int pow(long x, long n, long mod) { + long res = 1 % mod; + for (; n > 0; n /= 2) { + if (n % 2 == 1) { + res = (res * x) % mod; + } + x = (x * x) % mod; + } + return (int) res; + } +} diff --git a/src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md b/src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md new file mode 100644 index 000000000..eaaf97bad --- /dev/null +++ b/src/main/java/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md @@ -0,0 +1,44 @@ +3145\. Find Products of Elements of Big Array + +Hard + +A **powerful array** for an integer `x` is the shortest sorted array of powers of two that sum up to `x`. For example, the powerful array for 11 is `[1, 2, 8]`. + +The array `big_nums` is created by concatenating the **powerful** arrays for every positive integer `i` in ascending order: 1, 2, 3, and so forth. Thus, `big_nums` starts as [1, 2, 1, 2, 4, 1, 4, 2, 4, 1, 2, 4, 8, ...]. + +You are given a 2D integer matrix `queries`, where for queries[i] = [fromi, toi, modi] you should calculate (big_nums[fromi] * big_nums[fromi + 1] * ... * big_nums[toi]) % modi. + +Return an integer array `answer` such that `answer[i]` is the answer to the ith query. + +**Example 1:** + +**Input:** queries = [[1,3,7]] + +**Output:** [4] + +**Explanation:** + +There is one query. + +`big_nums[1..3] = [2,1,2]`. The product of them is 4. The remainder of 4 under 7 is 4. + +**Example 2:** + +**Input:** queries = [[2,5,3],[7,7,4]] + +**Output:** [2,2] + +**Explanation:** + +There are two queries. + +First query: `big_nums[2..5] = [1,2,4,1]`. The product of them is 8. The remainder of 8 under 3 is 2. + +Second query: `big_nums[7] = 2`. The remainder of 2 under 4 is 2. + +**Constraints:** + +* `1 <= queries.length <= 500` +* `queries[i].length == 3` +* 0 <= queries[i][0] <= queries[i][1] <= 1015 +* 1 <= queries[i][2] <= 105 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.java b/src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.java new file mode 100644 index 000000000..cacdae14e --- /dev/null +++ b/src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.java @@ -0,0 +1,20 @@ +package g3101_3200.s3146_permutation_difference_between_two_strings; + +// #Easy #String #Hash_Table #2024_05_15_Time_1_ms_(100.00%)_Space_42.4_MB_(84.38%) + +import java.util.Arrays; + +public class Solution { + public int findPermutationDifference(String s, String t) { + int[] res = new int[26]; + Arrays.fill(res, -1); + int sum = 0; + for (int i = 0; i < s.length(); ++i) { + res[s.charAt(i) - 'a'] = i; + } + for (int i = 0; i < t.length(); ++i) { + sum += Math.abs(res[t.charAt(i) - 'a'] - i); + } + return sum; + } +} diff --git a/src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/readme.md b/src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/readme.md new file mode 100644 index 000000000..fbda75338 --- /dev/null +++ b/src/main/java/g3101_3200/s3146_permutation_difference_between_two_strings/readme.md @@ -0,0 +1,40 @@ +3146\. Permutation Difference between Two Strings + +Easy + +You are given two strings `s` and `t` such that every character occurs at most once in `s` and `t` is a permutation of `s`. + +The **permutation difference** between `s` and `t` is defined as the **sum** of the absolute difference between the index of the occurrence of each character in `s` and the index of the occurrence of the same character in `t`. + +Return the **permutation difference** between `s` and `t`. + +**Example 1:** + +**Input:** s = "abc", t = "bac" + +**Output:** 2 + +**Explanation:** + +For `s = "abc"` and `t = "bac"`, the permutation difference of `s` and `t` is equal to the sum of: + +* The absolute difference between the index of the occurrence of `"a"` in `s` and the index of the occurrence of `"a"` in `t`. +* The absolute difference between the index of the occurrence of `"b"` in `s` and the index of the occurrence of `"b"` in `t`. +* The absolute difference between the index of the occurrence of `"c"` in `s` and the index of the occurrence of `"c"` in `t`. + +That is, the permutation difference between `s` and `t` is equal to `|0 - 1| + |2 - 2| + |1 - 0| = 2`. + +**Example 2:** + +**Input:** s = "abcde", t = "edbac" + +**Output:** 12 + +**Explanation:** The permutation difference between `s` and `t` is equal to `|0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12`. + +**Constraints:** + +* `1 <= s.length <= 26` +* Each character occurs at most once in `s`. +* `t` is a permutation of `s`. +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/Solution.java b/src/main/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/Solution.java new file mode 100644 index 000000000..7dd991c7d --- /dev/null +++ b/src/main/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/Solution.java @@ -0,0 +1,18 @@ +package g3101_3200.s3147_taking_maximum_energy_from_the_mystic_dungeon; + +// #Medium #Array #Prefix_Sum #2024_05_15_Time_2_ms_(97.58%)_Space_59.8_MB_(75.38%) + +public class Solution { + public int maximumEnergy(int[] energy, int k) { + int max = Integer.MIN_VALUE; + int n = energy.length; + for (int i = n - 1; i >= n - k; i--) { + int en = 0; + for (int j = i; j >= 0; j -= k) { + en += energy[j]; + max = Math.max(en, max); + } + } + return max; + } +} diff --git a/src/main/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/readme.md b/src/main/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/readme.md new file mode 100644 index 000000000..ff311bf56 --- /dev/null +++ b/src/main/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/readme.md @@ -0,0 +1,33 @@ +3147\. Taking Maximum Energy From the Mystic Dungeon + +Medium + +In a mystic dungeon, `n` magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you. + +You have been cursed in such a way that after absorbing energy from magician `i`, you will be instantly transported to magician `(i + k)`. This process will be repeated until you reach the magician where `(i + k)` does not exist. + +In other words, you will choose a starting point and then teleport with `k` jumps until you reach the end of the magicians' sequence, **absorbing all the energy** during the journey. + +You are given an array `energy` and an integer `k`. Return the **maximum** possible energy you can gain. + +**Example 1:** + +**Input:** energy = [5,2,-10,-5,1], k = 3 + +**Output:** 3 + +**Explanation:** We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3. + +**Example 2:** + +**Input:** energy = [-2,-3,-1], k = 2 + +**Output:** -1 + +**Explanation:** We can gain a total energy of -1 by starting from magician 2. + +**Constraints:** + +* 1 <= energy.length <= 105 +* `-1000 <= energy[i] <= 1000` +* `1 <= k <= energy.length - 1` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/Solution.java b/src/main/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/Solution.java new file mode 100644 index 000000000..00903146c --- /dev/null +++ b/src/main/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/Solution.java @@ -0,0 +1,32 @@ +package g3101_3200.s3148_maximum_difference_score_in_a_grid; + +// #Medium #Array #Dynamic_Programming #Matrix #2024_05_15_Time_5_ms_(100.00%)_Space_67.4_MB_(5.12%) + +import java.util.List; + +public class Solution { + public int maxScore(List> grid) { + int m = grid.size() - 1; + List row = grid.get(m); + int n = row.size(); + int[] maxRB = new int[n--]; + int mx = maxRB[n] = row.get(n); + int result = Integer.MIN_VALUE; + for (int i = n - 1; i >= 0; i--) { + int x = row.get(i); + result = Math.max(result, mx - x); + maxRB[i] = mx = Math.max(mx, x); + } + for (int i = m - 1; i >= 0; i--) { + row = grid.get(i); + mx = 0; + for (int j = n; j >= 0; j--) { + mx = Math.max(mx, maxRB[j]); + int x = row.get(j); + result = Math.max(result, mx - x); + maxRB[j] = mx = Math.max(mx, x); + } + } + return result; + } +} diff --git a/src/main/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/readme.md b/src/main/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/readme.md new file mode 100644 index 000000000..c2fe847e5 --- /dev/null +++ b/src/main/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/readme.md @@ -0,0 +1,43 @@ +3148\. Maximum Difference Score in a Grid + +Medium + +You are given an `m x n` matrix `grid` consisting of **positive** integers. You can move from a cell in the matrix to **any** other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value `c1` to a cell with the value `c2` is `c2 - c1`. + +You can start at **any** cell, and you have to make **at least** one move. + +Return the **maximum** total score you can achieve. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/03/14/grid1.png) + +**Input:** grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]] + +**Output:** 9 + +**Explanation:** We start at the cell `(0, 1)`, and we perform the following moves: + +- Move from the cell `(0, 1)` to `(2, 1)` with a score of `7 - 5 = 2`. + +- Move from the cell `(2, 1)` to `(2, 2)` with a score of `14 - 7 = 7`. + +The total score is `2 + 7 = 9`. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/04/08/moregridsdrawio-1.png) + +**Input:** grid = [[4,3,2],[3,2,1]] + +**Output:** \-1 + +**Explanation:** We start at the cell `(0, 0)`, and we perform one move: `(0, 0)` to `(0, 1)`. The score is `3 - 4 = -1`. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `2 <= m, n <= 1000` +* 4 <= m * n <= 105 +* 1 <= grid[i][j] <= 105 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/Solution.java b/src/main/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/Solution.java new file mode 100644 index 000000000..75c371f61 --- /dev/null +++ b/src/main/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/Solution.java @@ -0,0 +1,61 @@ +package g3101_3200.s3149_find_the_minimum_cost_array_permutation; + +// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Bitmask +// #2024_05_15_Time_105_ms_(88.11%)_Space_46.5_MB_(64.32%) + +import java.util.Arrays; + +public class Solution { + private int findMinScore(int mask, int prevNum, int[] nums, int[][] dp) { + int n = nums.length; + if (Integer.bitCount(mask) == n) { + dp[mask][prevNum] = Math.abs(prevNum - nums[0]); + return dp[mask][prevNum]; + } + if (dp[mask][prevNum] != -1) { + return dp[mask][prevNum]; + } + int minScore = Integer.MAX_VALUE; + for (int currNum = 0; currNum < n; currNum++) { + if ((mask >> currNum & 1 ^ 1) == 1) { + int currScore = + Math.abs(prevNum - nums[currNum]) + + findMinScore(mask | 1 << currNum, currNum, nums, dp); + minScore = Math.min(minScore, currScore); + } + } + return dp[mask][prevNum] = minScore; + } + + private int[] constructMinScorePermutation(int n, int[] nums, int[][] dp) { + int[] permutation = new int[n]; + int i = 0; + permutation[i++] = 0; + int prevNum = 0; + for (int mask = 1; i < n; mask |= 1 << prevNum) { + for (int currNum = 0; currNum < n; currNum++) { + if ((mask >> currNum & 1 ^ 1) == 1) { + int currScore = + Math.abs(prevNum - nums[currNum]) + dp[mask | 1 << currNum][currNum]; + int minScore = dp[mask][prevNum]; + if (currScore == minScore) { + permutation[i++] = currNum; + prevNum = currNum; + break; + } + } + } + } + return permutation; + } + + public int[] findPermutation(int[] nums) { + int n = nums.length; + int[][] dp = new int[1 << n][n]; + for (int[] row : dp) { + Arrays.fill(row, -1); + } + findMinScore(1, 0, nums, dp); + return constructMinScorePermutation(n, nums, dp); + } +} diff --git a/src/main/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/readme.md b/src/main/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/readme.md new file mode 100644 index 000000000..0a07e6091 --- /dev/null +++ b/src/main/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/readme.md @@ -0,0 +1,38 @@ +3149\. Find the Minimum Cost Array Permutation + +Hard + +You are given an array `nums` which is a permutation of `[0, 1, 2, ..., n - 1]`. The **score** of any permutation of `[0, 1, 2, ..., n - 1]` named `perm` is defined as: + +`score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n - 1] - nums[perm[0]]|` + +Return the permutation `perm` which has the **minimum** possible score. If _multiple_ permutations exist with this score, return the one that is lexicographically smallest among them. + +**Example 1:** + +**Input:** nums = [1,0,2] + +**Output:** [0,1,2] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/04/example0gif.gif)** + +The lexicographically smallest permutation with minimum cost is `[0,1,2]`. The cost of this permutation is `|0 - 0| + |1 - 2| + |2 - 1| = 2`. + +**Example 2:** + +**Input:** nums = [0,2,1] + +**Output:** [0,2,1] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/04/example1gif.gif)** + +The lexicographically smallest permutation with minimum cost is `[0,2,1]`. The cost of this permutation is `|0 - 1| + |2 - 2| + |1 - 0| = 2`. + +**Constraints:** + +* `2 <= n == nums.length <= 14` +* `nums` is a permutation of `[0, 1, 2, ..., n - 1]`. \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/SolutionTest.java b/src/test/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/SolutionTest.java new file mode 100644 index 000000000..3a81d33d1 --- /dev/null +++ b/src/test/java/g3101_3200/s3142_check_if_grid_satisfies_conditions/SolutionTest.java @@ -0,0 +1,22 @@ +package g3101_3200.s3142_check_if_grid_satisfies_conditions; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void satisfiesConditions() { + assertThat( + new Solution().satisfiesConditions(new int[][] {{1, 0, 2}, {1, 0, 2}}), + equalTo(true)); + } + + @Test + void satisfiesConditions2() { + assertThat( + new Solution().satisfiesConditions(new int[][] {{1, 1, 1}, {0, 0, 0}}), + equalTo(false)); + } +} diff --git a/src/test/java/g3101_3200/s3143_maximum_points_inside_the_square/SolutionTest.java b/src/test/java/g3101_3200/s3143_maximum_points_inside_the_square/SolutionTest.java new file mode 100644 index 000000000..6dfd675de --- /dev/null +++ b/src/test/java/g3101_3200/s3143_maximum_points_inside_the_square/SolutionTest.java @@ -0,0 +1,25 @@ +package g3101_3200.s3143_maximum_points_inside_the_square; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxPointsInsideSquare() { + assertThat( + new Solution() + .maxPointsInsideSquare( + new int[][] {{2, 2}, {-1, -2}, {-4, 4}, {-3, 1}, {3, -3}}, "abdca"), + equalTo(2)); + } + + @Test + void maxPointsInsideSquare2() { + assertThat( + new Solution() + .maxPointsInsideSquare(new int[][] {{1, 1}, {-2, -2}, {-2, 2}}, "abb"), + equalTo(1)); + } +} diff --git a/src/test/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/SolutionTest.java b/src/test/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/SolutionTest.java new file mode 100644 index 000000000..052bc82b7 --- /dev/null +++ b/src/test/java/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3144_minimum_substring_partition_of_equal_character_frequency; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumSubstringsInPartition() { + assertThat(new Solution().minimumSubstringsInPartition("fabccddg"), equalTo(3)); + } + + @Test + void minimumSubstringsInPartition2() { + assertThat(new Solution().minimumSubstringsInPartition("abababaccddb"), equalTo(2)); + } +} diff --git a/src/test/java/g3101_3200/s3145_find_products_of_elements_of_big_array/SolutionTest.java b/src/test/java/g3101_3200/s3145_find_products_of_elements_of_big_array/SolutionTest.java new file mode 100644 index 000000000..5d4b230fc --- /dev/null +++ b/src/test/java/g3101_3200/s3145_find_products_of_elements_of_big_array/SolutionTest.java @@ -0,0 +1,22 @@ +package g3101_3200.s3145_find_products_of_elements_of_big_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findProductsOfElements() { + assertThat( + new Solution().findProductsOfElements(new long[][] {{1, 3, 7}}), + equalTo(new int[] {4})); + } + + @Test + void findProductsOfElements2() { + assertThat( + new Solution().findProductsOfElements(new long[][] {{2, 5, 3}, {7, 7, 4}}), + equalTo(new int[] {2, 2})); + } +} diff --git a/src/test/java/g3101_3200/s3146_permutation_difference_between_two_strings/SolutionTest.java b/src/test/java/g3101_3200/s3146_permutation_difference_between_two_strings/SolutionTest.java new file mode 100644 index 000000000..0387cf0de --- /dev/null +++ b/src/test/java/g3101_3200/s3146_permutation_difference_between_two_strings/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3146_permutation_difference_between_two_strings; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findPermutationDifference() { + assertThat(new Solution().findPermutationDifference("abc", "bac"), equalTo(2)); + } + + @Test + void findPermutationDifference2() { + assertThat(new Solution().findPermutationDifference("abcde", "edbac"), equalTo(12)); + } +} diff --git a/src/test/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/SolutionTest.java b/src/test/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/SolutionTest.java new file mode 100644 index 000000000..8cfd9af44 --- /dev/null +++ b/src/test/java/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3147_taking_maximum_energy_from_the_mystic_dungeon; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumEnergy() { + assertThat(new Solution().maximumEnergy(new int[] {5, 2, -10, -5, 1}, 3), equalTo(3)); + } + + @Test + void maximumEnergy2() { + assertThat(new Solution().maximumEnergy(new int[] {-2, -3, -1}, 2), equalTo(-1)); + } +} diff --git a/src/test/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/SolutionTest.java b/src/test/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/SolutionTest.java new file mode 100644 index 000000000..ab6df4539 --- /dev/null +++ b/src/test/java/g3101_3200/s3148_maximum_difference_score_in_a_grid/SolutionTest.java @@ -0,0 +1,28 @@ +package g3101_3200.s3148_maximum_difference_score_in_a_grid; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import com_github_leetcode.ArrayUtils; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxScore() { + assertThat( + new Solution() + .maxScore( + ArrayUtils.getLists( + new int[][] { + {9, 5, 7, 3}, {8, 9, 6, 1}, {6, 7, 14, 3}, {2, 5, 3, 1} + })), + equalTo(9)); + } + + @Test + void maxScore2() { + assertThat( + new Solution().maxScore(ArrayUtils.getLists(new int[][] {{4, 3, 2}, {3, 2, 1}})), + equalTo(-1)); + } +} diff --git a/src/test/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/SolutionTest.java b/src/test/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/SolutionTest.java new file mode 100644 index 000000000..399e8a5d4 --- /dev/null +++ b/src/test/java/g3101_3200/s3149_find_the_minimum_cost_array_permutation/SolutionTest.java @@ -0,0 +1,20 @@ +package g3101_3200.s3149_find_the_minimum_cost_array_permutation; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findPermutation() { + assertThat( + new Solution().findPermutation(new int[] {1, 0, 2}), equalTo(new int[] {0, 1, 2})); + } + + @Test + void findPermutation2() { + assertThat( + new Solution().findPermutation(new int[] {0, 2, 1}), equalTo(new int[] {0, 2, 1})); + } +}