diff --git a/src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/Solution.java b/src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/Solution.java new file mode 100644 index 000000000..ddcd0e5ac --- /dev/null +++ b/src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/Solution.java @@ -0,0 +1,27 @@ +package g3201_3300.s3254_find_the_power_of_k_size_subarrays_i; + +// #Medium #Array #Sliding_Window #2024_08_20_Time_1_ms_(100.00%)_Space_45.2_MB_(95.96%) + +public class Solution { + public int[] resultsArray(int[] nums, int k) { + int n = nums.length; + int[] arr = new int[n - k + 1]; + int count = 0; + for (int i = 1; i < k; i++) { + if (nums[i] == nums[i - 1] + 1) { + count++; + } + } + arr[0] = (count == k - 1) ? nums[k - 1] : -1; + for (int i = 1; i <= n - k; i++) { + if (nums[i] == nums[i - 1] + 1) { + count--; + } + if (nums[i + k - 1] == nums[i + k - 2] + 1) { + count++; + } + arr[i] = (count == k - 1) ? nums[i + k - 1] : -1; + } + return arr; + } +} diff --git a/src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md b/src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md new file mode 100644 index 000000000..48b663c77 --- /dev/null +++ b/src/main/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/readme.md @@ -0,0 +1,48 @@ +3254\. Find the Power of K-Size Subarrays I + +Medium + +You are given an array of integers `nums` of length `n` and a _positive_ integer `k`. + +The **power** of an array is defined as: + +* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order. +* \-1 otherwise. + +You need to find the **power** of all subarrays of `nums` of size `k`. + +Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,3,2,5], k = 3 + +**Output:** [3,4,-1,-1,-1] + +**Explanation:** + +There are 5 subarrays of `nums` of size 3: + +* `[1, 2, 3]` with the maximum element 3. +* `[2, 3, 4]` with the maximum element 4. +* `[3, 4, 3]` whose elements are **not** consecutive. +* `[4, 3, 2]` whose elements are **not** sorted. +* `[3, 2, 5]` whose elements are **not** consecutive. + +**Example 2:** + +**Input:** nums = [2,2,2,2,2], k = 4 + +**Output:** [-1,-1] + +**Example 3:** + +**Input:** nums = [3,2,3,2,3,2], k = 2 + +**Output:** [-1,3,-1,3,-1] + +**Constraints:** + +* `1 <= n == nums.length <= 500` +* 1 <= nums[i] <= 105 +* `1 <= k <= n` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/Solution.java b/src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/Solution.java new file mode 100644 index 000000000..1f38edfff --- /dev/null +++ b/src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/Solution.java @@ -0,0 +1,28 @@ +package g3201_3300.s3255_find_the_power_of_k_size_subarrays_ii; + +// #Medium #Array #Sliding_Window #2024_08_20_Time_3_ms_(99.24%)_Space_64.1_MB_(67.44%) + +public class Solution { + public int[] resultsArray(int[] nums, int k) { + if (k == 1) { + return nums; + } + int start = 0; + int n = nums.length; + int[] output = new int[n - k + 1]; + for (int i = 1; i < n; i++) { + if (nums[i] != nums[i - 1] + 1) { + start = i; + } + int index = i - k + 1; + if (index >= 0) { + if (start > index) { + output[index] = -1; + } else { + output[index] = nums[i]; + } + } + } + return output; + } +} diff --git a/src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md b/src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md new file mode 100644 index 000000000..3a2c24329 --- /dev/null +++ b/src/main/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/readme.md @@ -0,0 +1,48 @@ +3255\. Find the Power of K-Size Subarrays II + +Medium + +You are given an array of integers `nums` of length `n` and a _positive_ integer `k`. + +The **power** of an array is defined as: + +* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order. +* \-1 otherwise. + +You need to find the **power** of all subarrays of `nums` of size `k`. + +Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,3,2,5], k = 3 + +**Output:** [3,4,-1,-1,-1] + +**Explanation:** + +There are 5 subarrays of `nums` of size 3: + +* `[1, 2, 3]` with the maximum element 3. +* `[2, 3, 4]` with the maximum element 4. +* `[3, 4, 3]` whose elements are **not** consecutive. +* `[4, 3, 2]` whose elements are **not** sorted. +* `[3, 2, 5]` whose elements are **not** consecutive. + +**Example 2:** + +**Input:** nums = [2,2,2,2,2], k = 4 + +**Output:** [-1,-1] + +**Example 3:** + +**Input:** nums = [3,2,3,2,3,2], k = 2 + +**Output:** [-1,3,-1,3,-1] + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 106 +* `1 <= k <= n` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/Solution.java b/src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/Solution.java new file mode 100644 index 000000000..43195d103 --- /dev/null +++ b/src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/Solution.java @@ -0,0 +1,65 @@ +package g3201_3300.s3256_maximum_value_sum_by_placing_three_rooks_i; + +// #Hard #Array #Dynamic_Programming #Matrix #Enumeration +// #2024_08_20_Time_7_ms_(100.00%)_Space_45.1_MB_(90.97%) + +import java.util.Arrays; + +public class Solution { + public long maximumValueSum(int[][] board) { + int n = board.length; + int m = board[0].length; + int[][] tb = new int[n][m]; + tb[0] = Arrays.copyOf(board[0], m); + for (int i = 1; i < n; i++) { + for (int j = 0; j < m; j++) { + tb[i][j] = Math.max(tb[i - 1][j], board[i][j]); + } + } + int[][] bt = new int[n][m]; + bt[n - 1] = Arrays.copyOf(board[n - 1], m); + for (int i = n - 2; i >= 0; i--) { + for (int j = 0; j < m; j++) { + bt[i][j] = Math.max(bt[i + 1][j], board[i][j]); + } + } + long ans = Long.MIN_VALUE; + for (int i = 1; i < n - 1; i++) { + int[][] max3Top = getMax3(tb[i - 1]); + int[][] max3Cur = getMax3(board[i]); + int[][] max3Bottom = getMax3(bt[i + 1]); + for (int[] topCand : max3Top) { + for (int[] curCand : max3Cur) { + for (int[] bottomCand : max3Bottom) { + if (topCand[1] != curCand[1] + && topCand[1] != bottomCand[1] + && curCand[1] != bottomCand[1]) { + long cand = (long) topCand[0] + curCand[0] + bottomCand[0]; + ans = Math.max(ans, cand); + } + } + } + } + } + return ans; + } + + private int[][] getMax3(int[] row) { + int m = row.length; + int[][] ans = new int[3][2]; + Arrays.fill(ans, new int[] {Integer.MIN_VALUE, -1}); + for (int j = 0; j < m; j++) { + if (row[j] >= ans[0][0]) { + ans[2] = ans[1]; + ans[1] = ans[0]; + ans[0] = new int[] {row[j], j}; + } else if (row[j] >= ans[1][0]) { + ans[2] = ans[1]; + ans[1] = new int[] {row[j], j}; + } else if (row[j] > ans[2][0]) { + ans[2] = new int[] {row[j], j}; + } + } + return ans; + } +} diff --git a/src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md b/src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md new file mode 100644 index 000000000..384583c5e --- /dev/null +++ b/src/main/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/readme.md @@ -0,0 +1,47 @@ +3256\. Maximum Value Sum by Placing Three Rooks I + +Hard + +You are given a `m x n` 2D array `board` representing a chessboard, where `board[i][j]` represents the **value** of the cell `(i, j)`. + +Rooks in the **same** row or column **attack** each other. You need to place _three_ rooks on the chessboard such that the rooks **do not** **attack** each other. + +Return the **maximum** sum of the cell **values** on which the rooks are placed. + +**Example 1:** + +**Input:** board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]] + +**Output:** 4 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png) + +We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`. + +**Example 2:** + +**Input:** board = [[1,2,3],[4,5,6],[7,8,9]] + +**Output:** 15 + +**Explanation:** + +We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`. + +**Example 3:** + +**Input:** board = [[1,1,1],[1,1,1],[1,1,1]] + +**Output:** 3 + +**Explanation:** + +We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`. + +**Constraints:** + +* `3 <= m == board.length <= 100` +* `3 <= n == board[i].length <= 100` +* -109 <= board[i][j] <= 109 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/Solution.java b/src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/Solution.java new file mode 100644 index 000000000..7cf418f6c --- /dev/null +++ b/src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/Solution.java @@ -0,0 +1,65 @@ +package g3201_3300.s3257_maximum_value_sum_by_placing_three_rooks_ii; + +// #Hard #Array #Dynamic_Programming #Matrix #Enumeration +// #2024_08_20_Time_18_ms_(99.59%)_Space_74.2_MB_(66.81%) + +import java.util.Arrays; + +public class Solution { + public long maximumValueSum(int[][] board) { + int n = board.length; + int m = board[0].length; + int[][] tb = new int[n][m]; + tb[0] = Arrays.copyOf(board[0], m); + for (int i = 1; i < n; i++) { + for (int j = 0; j < m; j++) { + tb[i][j] = Math.max(tb[i - 1][j], board[i][j]); + } + } + int[][] bt = new int[n][m]; + bt[n - 1] = Arrays.copyOf(board[n - 1], m); + for (int i = n - 2; i >= 0; i--) { + for (int j = 0; j < m; j++) { + bt[i][j] = Math.max(bt[i + 1][j], board[i][j]); + } + } + long ans = Long.MIN_VALUE; + for (int i = 1; i < n - 1; i++) { + int[][] max3Top = getMax3(tb[i - 1]); + int[][] max3Cur = getMax3(board[i]); + int[][] max3Bottom = getMax3(bt[i + 1]); + for (int[] topCand : max3Top) { + for (int[] curCand : max3Cur) { + for (int[] bottomCand : max3Bottom) { + if (topCand[1] != curCand[1] + && topCand[1] != bottomCand[1] + && curCand[1] != bottomCand[1]) { + long cand = (long) topCand[0] + curCand[0] + bottomCand[0]; + ans = Math.max(ans, cand); + } + } + } + } + } + return ans; + } + + private int[][] getMax3(int[] row) { + int m = row.length; + int[][] ans = new int[3][2]; + Arrays.fill(ans, new int[] {Integer.MIN_VALUE, -1}); + for (int j = 0; j < m; j++) { + if (row[j] >= ans[0][0]) { + ans[2] = ans[1]; + ans[1] = ans[0]; + ans[0] = new int[] {row[j], j}; + } else if (row[j] >= ans[1][0]) { + ans[2] = ans[1]; + ans[1] = new int[] {row[j], j}; + } else if (row[j] > ans[2][0]) { + ans[2] = new int[] {row[j], j}; + } + } + return ans; + } +} diff --git a/src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md b/src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md new file mode 100644 index 000000000..6cd67b4ed --- /dev/null +++ b/src/main/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/readme.md @@ -0,0 +1,47 @@ +3257\. Maximum Value Sum by Placing Three Rooks II + +Hard + +You are given a `m x n` 2D array `board` representing a chessboard, where `board[i][j]` represents the **value** of the cell `(i, j)`. + +Rooks in the **same** row or column **attack** each other. You need to place _three_ rooks on the chessboard such that the rooks **do not** **attack** each other. + +Return the **maximum** sum of the cell **values** on which the rooks are placed. + +**Example 1:** + +**Input:** board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]] + +**Output:** 4 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png) + +We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`. + +**Example 2:** + +**Input:** board = [[1,2,3],[4,5,6],[7,8,9]] + +**Output:** 15 + +**Explanation:** + +We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`. + +**Example 3:** + +**Input:** board = [[1,1,1],[1,1,1],[1,1,1]] + +**Output:** 3 + +**Explanation:** + +We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`. + +**Constraints:** + +* `3 <= m == board.length <= 500` +* `3 <= n == board[i].length <= 500` +* -109 <= board[i][j] <= 109 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/Solution.java b/src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/Solution.java new file mode 100644 index 000000000..8fc470d0a --- /dev/null +++ b/src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/Solution.java @@ -0,0 +1,34 @@ +package g3201_3300.s3258_count_substrings_that_satisfy_k_constraint_i; + +// #Easy #String #Sliding_Window #2024_08_20_Time_1_ms_(100.00%)_Space_42_MB_(75.09%) + +public class Solution { + public int countKConstraintSubstrings(String s, int k) { + int n = s.length(); + int sum = n; + int i = 0; + int j = 0; + int one = 0; + int zero = 0; + char ch; + while (j < n) { + ch = s.charAt(j++); + if (ch == '0') { + zero++; + } else { + one++; + } + while (i <= j && one > k && zero > k) { + ch = s.charAt(i++); + if (ch == '0') { + zero--; + } else { + one--; + } + } + int len = (zero + one - 1); + sum += len; + } + return sum; + } +} diff --git a/src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md b/src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md new file mode 100644 index 000000000..3eaf094de --- /dev/null +++ b/src/main/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/readme.md @@ -0,0 +1,48 @@ +3258\. Count Substrings That Satisfy K-Constraint I + +Easy + +You are given a **binary** string `s` and an integer `k`. + +A **binary string** satisfies the **k-constraint** if **either** of the following conditions holds: + +* The number of `0`'s in the string is at most `k`. +* The number of `1`'s in the string is at most `k`. + +Return an integer denoting the number of substrings of `s` that satisfy the **k-constraint**. + +**Example 1:** + +**Input:** s = "10101", k = 1 + +**Output:** 12 + +**Explanation:** + +Every substring of `s` except the substrings `"1010"`, `"10101"`, and `"0101"` satisfies the k-constraint. + +**Example 2:** + +**Input:** s = "1010101", k = 2 + +**Output:** 25 + +**Explanation:** + +Every substring of `s` except the substrings with a length greater than 5 satisfies the k-constraint. + +**Example 3:** + +**Input:** s = "11111", k = 1 + +**Output:** 15 + +**Explanation:** + +All substrings of `s` satisfy the k-constraint. + +**Constraints:** + +* `1 <= s.length <= 50` +* `1 <= k <= s.length` +* `s[i]` is either `'0'` or `'1'`. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/Solution.java b/src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/Solution.java new file mode 100644 index 000000000..2b5de38b6 --- /dev/null +++ b/src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/Solution.java @@ -0,0 +1,20 @@ +package g3201_3300.s3259_maximum_energy_boost_from_two_drinks; + +// #Medium #Array #Dynamic_Programming #2024_08_20_Time_3_ms_(100.00%)_Space_57_MB_(98.29%) + +public class Solution { + public long maxEnergyBoost(int[] energyDrinkA, int[] energyDrinkB) { + long a0 = 0; + long a1 = 0; + long b0 = 0; + long b1 = 0; + long n = energyDrinkA.length; + for (int i = 0; i < n; i++) { + a1 = Math.max(a0 + energyDrinkA[i], b0); + b1 = Math.max(b0 + energyDrinkB[i], a0); + a0 = a1; + b0 = b1; + } + return Math.max(a1, b1); + } +} diff --git a/src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md b/src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md new file mode 100644 index 000000000..f8e105266 --- /dev/null +++ b/src/main/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/readme.md @@ -0,0 +1,41 @@ +3259\. Maximum Energy Boost From Two Drinks + +Medium + +You are given two integer arrays `energyDrinkA` and `energyDrinkB` of the same length `n` by a futuristic sports scientist. These arrays represent the energy boosts per hour provided by two different energy drinks, A and B, respectively. + +You want to _maximize_ your total energy boost by drinking one energy drink _per hour_. However, if you want to switch from consuming one energy drink to the other, you need to wait for _one hour_ to cleanse your system (meaning you won't get any energy boost in that hour). + +Return the **maximum** total energy boost you can gain in the next `n` hours. + +**Note** that you can start consuming _either_ of the two energy drinks. + +**Example 1:** + +**Input:** energyDrinkA = [1,3,1], energyDrinkB = [3,1,1] + +**Output:** 5 + +**Explanation:** + +To gain an energy boost of 5, drink only the energy drink A (or only B). + +**Example 2:** + +**Input:** energyDrinkA = [4,1,1], energyDrinkB = [1,1,3] + +**Output:** 7 + +**Explanation:** + +To gain an energy boost of 7: + +* Drink the energy drink A for the first hour. +* Switch to the energy drink B and we lose the energy boost of the second hour. +* Gain the energy boost of the drink B in the third hour. + +**Constraints:** + +* `n == energyDrinkA.length == energyDrinkB.length` +* 3 <= n <= 105 +* 1 <= energyDrinkA[i], energyDrinkB[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/Solution.java b/src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/Solution.java new file mode 100644 index 000000000..6fe3f123e --- /dev/null +++ b/src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/Solution.java @@ -0,0 +1,111 @@ +package g3201_3300.s3260_find_the_largest_palindrome_divisible_by_k; + +// #Hard #String #Dynamic_Programming #Math #Greedy #Number_Theory +// #2024_08_20_Time_4_ms_(97.21%)_Space_45.1_MB_(73.49%) + +import java.util.Arrays; + +public class Solution { + public String largestPalindrome(int n, int k) { + char[] sc = new char[n]; + if (k == 1 || k == 3 || k == 9) { + Arrays.fill(sc, '9'); + } else if (k == 7) { + if (n == 1) { + return "7"; + } else if (n == 2) { + return "77"; + } + int mod = n % 12; + checkValues(n, mod, sc); + } else if (k == 2) { + Arrays.fill(sc, '9'); + sc[0] = '8'; + sc[n - 1] = '8'; + } else if (k == 4) { + Arrays.fill(sc, '8'); + int i = 2; + int j = n - 3; + while (i <= j) { + sc[i] = '9'; + sc[j] = '9'; + ++i; + --j; + } + } else if (k == 5) { + Arrays.fill(sc, '9'); + sc[0] = '5'; + sc[n - 1] = '5'; + } else if (k == 6) { + String number = getString(n, sc); + if (number != null) { + return number; + } + } else if (k == 8) { + Arrays.fill(sc, '8'); + int i = 3; + int j = n - 4; + while (i <= j) { + sc[i] = '9'; + sc[j] = '9'; + ++i; + --j; + } + } + return new String(sc); + } + + private void checkValues(int n, int mod, char[] sc) { + if (mod == 6 || mod == 0) { + Arrays.fill(sc, '9'); + } else if (mod == 3) { + Arrays.fill(sc, '9'); + sc[n / 2] = '5'; + } else if (mod == 4 || mod == 5 || mod == 1 || mod == 2) { + Arrays.fill(sc, '7'); + int i = 0; + int j = n - 1; + while (i + 1 < j) { + sc[i] = '9'; + sc[j] = '9'; + ++i; + --j; + } + } else if (mod == 7 || mod == 8 || mod == 10 || mod == 11) { + Arrays.fill(sc, '4'); + int i = 0; + int j = n - 1; + while (i + 1 < j) { + sc[i] = '9'; + sc[j] = '9'; + ++i; + --j; + } + } else if (mod == 9) { + Arrays.fill(sc, '9'); + sc[n / 2] = '6'; + } + } + + private String getString(int n, char[] sc) { + if (n == 1) { + return "6"; + } else if (n == 2) { + return "66"; + } else { + if (n % 2 == 1) { + Arrays.fill(sc, '9'); + sc[0] = '8'; + sc[n - 1] = '8'; + sc[n / 2] = '8'; + } else { + Arrays.fill(sc, '9'); + sc[0] = '8'; + sc[n - 1] = '8'; + sc[n / 2] = '7'; + sc[n / 2 - 1] = '7'; + } + } + return null; + } +} diff --git a/src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md b/src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md new file mode 100644 index 000000000..a4cfb138e --- /dev/null +++ b/src/main/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/readme.md @@ -0,0 +1,45 @@ +3260\. Find the Largest Palindrome Divisible by K + +Hard + +You are given two **positive** integers `n` and `k`. + +An integer `x` is called **k-palindromic** if: + +* `x` is a palindrome. +* `x` is divisible by `k`. + +Return the **largest** integer having `n` digits (as a string) that is **k-palindromic**. + +**Note** that the integer must **not** have leading zeros. + +**Example 1:** + +**Input:** n = 3, k = 5 + +**Output:** "595" + +**Explanation:** + +595 is the largest k-palindromic integer with 3 digits. + +**Example 2:** + +**Input:** n = 1, k = 4 + +**Output:** "8" + +**Explanation:** + +4 and 8 are the only k-palindromic integers with 1 digit. + +**Example 3:** + +**Input:** n = 5, k = 6 + +**Output:** "89898" + +**Constraints:** + +* 1 <= n <= 105 +* `1 <= k <= 9` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/Solution.java b/src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/Solution.java new file mode 100644 index 000000000..699d5dff0 --- /dev/null +++ b/src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/Solution.java @@ -0,0 +1,54 @@ +package g3201_3300.s3261_count_substrings_that_satisfy_k_constraint_ii; + +// #Hard #Array #String #Binary_Search #Prefix_Sum #Sliding_Window +// #2024_08_20_Time_8_ms_(100.00%)_Space_104.9_MB_(71.64%) + +public class Solution { + public long[] countKConstraintSubstrings(String s, int k, int[][] queries) { + char[] current = s.toCharArray(); + int n = current.length; + long[] prefix = new long[n]; + int[] index = new int[n]; + int i = 0; + int count = 0; + int count1 = 0; + int count0 = 0; + for (int j = 0; j < n; j++) { + if (current[j] == '0') { + count0++; + } + if (current[j] == '1') { + count1++; + } + while (count0 > k && count1 > k) { + if (current[i] == '0') { + count0--; + } + if (current[i] == '1') { + count1--; + } + i++; + index[i] = j - 1; + } + count += j - i + 1; + index[i] = j; + prefix[j] = count; + } + while (i < n) { + index[i++] = n - 1; + } + long[] result = new long[queries.length]; + for (i = 0; i < queries.length; i++) { + int indexFirst = index[queries[i][0]]; + if (indexFirst > queries[i][1]) { + long num = queries[i][1] - queries[i][0] + 1L; + result[i] = ((num) * (num + 1)) / 2; + } else { + result[i] = prefix[queries[i][1]] - prefix[indexFirst]; + long num = indexFirst - queries[i][0] + 1L; + result[i] += ((num) * (num + 1)) / 2; + } + } + return result; + } +} diff --git a/src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md b/src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md new file mode 100644 index 000000000..9c4968aef --- /dev/null +++ b/src/main/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/readme.md @@ -0,0 +1,44 @@ +3261\. Count Substrings That Satisfy K-Constraint II + +Hard + +You are given a **binary** string `s` and an integer `k`. + +You are also given a 2D integer array `queries`, where queries[i] = [li, ri]. + +A **binary string** satisfies the **k-constraint** if **either** of the following conditions holds: + +* The number of `0`'s in the string is at most `k`. +* The number of `1`'s in the string is at most `k`. + +Return an integer array `answer`, where `answer[i]` is the number of substrings of s[li..ri] that satisfy the **k-constraint**. + +**Example 1:** + +**Input:** s = "0001111", k = 2, queries = [[0,6]] + +**Output:** [26] + +**Explanation:** + +For the query `[0, 6]`, all substrings of `s[0..6] = "0001111"` satisfy the k-constraint except for the substrings `s[0..5] = "000111"` and `s[0..6] = "0001111"`. + +**Example 2:** + +**Input:** s = "010101", k = 1, queries = [[0,5],[1,4],[2,3]] + +**Output:** [15,9,3] + +**Explanation:** + +The substrings of `s` with a length greater than 3 do not satisfy the k-constraint. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. +* `1 <= k <= s.length` +* 1 <= queries.length <= 105 +* queries[i] == [li, ri] +* 0 <= li <= ri < s.length +* All queries are distinct. \ No newline at end of file diff --git a/src/test/java/g1101_1200/s1114_print_in_order/FooTest.java b/src/test/java/g1101_1200/s1114_print_in_order/FooTest.java index 1dd99dee4..1fced389e 100644 --- a/src/test/java/g1101_1200/s1114_print_in_order/FooTest.java +++ b/src/test/java/g1101_1200/s1114_print_in_order/FooTest.java @@ -15,7 +15,7 @@ void foo() throws InterruptedException { new Thread(() -> foo.first(() -> fooData[0]++)).start(); new Thread(() -> foo.second(() -> fooData[0]++)).start(); new Thread(() -> foo.third(() -> fooData[0]++)).start(); - TimeUnit.MILLISECONDS.sleep(1200); + TimeUnit.MILLISECONDS.sleep(1400); assertThat(fooData[0], equalTo(3)); } } diff --git a/src/test/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/SolutionTest.java b/src/test/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/SolutionTest.java new file mode 100644 index 000000000..30d92146c --- /dev/null +++ b/src/test/java/g3201_3300/s3254_find_the_power_of_k_size_subarrays_i/SolutionTest.java @@ -0,0 +1,29 @@ +package g3201_3300.s3254_find_the_power_of_k_size_subarrays_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void resultsArray() { + assertThat( + new Solution().resultsArray(new int[] {1, 2, 3, 4, 3, 2, 5}, 3), + equalTo(new int[] {3, 4, -1, -1, -1})); + } + + @Test + void resultsArray2() { + assertThat( + new Solution().resultsArray(new int[] {2, 2, 2, 2, 2}, 4), + equalTo(new int[] {-1, -1})); + } + + @Test + void resultsArray3() { + assertThat( + new Solution().resultsArray(new int[] {3, 2, 3, 2, 3, 2}, 2), + equalTo(new int[] {-1, 3, -1, 3, -1})); + } +} diff --git a/src/test/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/SolutionTest.java b/src/test/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/SolutionTest.java new file mode 100644 index 000000000..6b92d3352 --- /dev/null +++ b/src/test/java/g3201_3300/s3255_find_the_power_of_k_size_subarrays_ii/SolutionTest.java @@ -0,0 +1,34 @@ +package g3201_3300.s3255_find_the_power_of_k_size_subarrays_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void resultsArray() { + assertThat( + new Solution().resultsArray(new int[] {1, 2, 3, 4, 3, 2, 5}, 3), + equalTo(new int[] {3, 4, -1, -1, -1})); + } + + @Test + void resultsArray2() { + assertThat( + new Solution().resultsArray(new int[] {2, 2, 2, 2, 2}, 4), + equalTo(new int[] {-1, -1})); + } + + @Test + void resultsArray3() { + assertThat( + new Solution().resultsArray(new int[] {3, 2, 3, 2, 3, 2}, 2), + equalTo(new int[] {-1, 3, -1, 3, -1})); + } + + @Test + void resultsArray4() { + assertThat(new Solution().resultsArray(new int[] {1}, 1), equalTo(new int[] {1})); + } +} diff --git a/src/test/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/SolutionTest.java b/src/test/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/SolutionTest.java new file mode 100644 index 000000000..84a2eedd6 --- /dev/null +++ b/src/test/java/g3201_3300/s3256_maximum_value_sum_by_placing_three_rooks_i/SolutionTest.java @@ -0,0 +1,31 @@ +package g3201_3300.s3256_maximum_value_sum_by_placing_three_rooks_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumValueSum() { + assertThat( + new Solution() + .maximumValueSum( + new int[][] {{-3, 1, 1, 1}, {-3, 1, -3, 1}, {-3, 2, 1, 1}}), + equalTo(4L)); + } + + @Test + void maximumValueSum2() { + assertThat( + new Solution().maximumValueSum(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}), + equalTo(15L)); + } + + @Test + void maximumValueSum3() { + assertThat( + new Solution().maximumValueSum(new int[][] {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}), + equalTo(3L)); + } +} diff --git a/src/test/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/SolutionTest.java b/src/test/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/SolutionTest.java new file mode 100644 index 000000000..c73156f06 --- /dev/null +++ b/src/test/java/g3201_3300/s3257_maximum_value_sum_by_placing_three_rooks_ii/SolutionTest.java @@ -0,0 +1,31 @@ +package g3201_3300.s3257_maximum_value_sum_by_placing_three_rooks_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumValueSum() { + assertThat( + new Solution() + .maximumValueSum( + new int[][] {{-3, 1, 1, 1}, {-3, 1, -3, 1}, {-3, 2, 1, 1}}), + equalTo(4L)); + } + + @Test + void maximumValueSum2() { + assertThat( + new Solution().maximumValueSum(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}), + equalTo(15L)); + } + + @Test + void maximumValueSum3() { + assertThat( + new Solution().maximumValueSum(new int[][] {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}), + equalTo(3L)); + } +} diff --git a/src/test/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/SolutionTest.java b/src/test/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/SolutionTest.java new file mode 100644 index 000000000..029a389df --- /dev/null +++ b/src/test/java/g3201_3300/s3258_count_substrings_that_satisfy_k_constraint_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3258_count_substrings_that_satisfy_k_constraint_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countKConstraintSubstrings() { + assertThat(new Solution().countKConstraintSubstrings("10101", 1), equalTo(12)); + } + + @Test + void countKConstraintSubstrings2() { + assertThat(new Solution().countKConstraintSubstrings("1010101", 2), equalTo(25)); + } +} diff --git a/src/test/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/SolutionTest.java b/src/test/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/SolutionTest.java new file mode 100644 index 000000000..94e44d6fa --- /dev/null +++ b/src/test/java/g3201_3300/s3259_maximum_energy_boost_from_two_drinks/SolutionTest.java @@ -0,0 +1,22 @@ +package g3201_3300.s3259_maximum_energy_boost_from_two_drinks; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxEnergyBoost() { + assertThat( + new Solution().maxEnergyBoost(new int[] {1, 3, 1}, new int[] {3, 1, 1}), + equalTo(5L)); + } + + @Test + void maxEnergyBoost2() { + assertThat( + new Solution().maxEnergyBoost(new int[] {4, 1, 1}, new int[] {1, 1, 3}), + equalTo(7L)); + } +} diff --git a/src/test/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/SolutionTest.java b/src/test/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/SolutionTest.java new file mode 100644 index 000000000..999015170 --- /dev/null +++ b/src/test/java/g3201_3300/s3260_find_the_largest_palindrome_divisible_by_k/SolutionTest.java @@ -0,0 +1,104 @@ +package g3201_3300.s3260_find_the_largest_palindrome_divisible_by_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void largestPalindrome() { + assertThat(new Solution().largestPalindrome(3, 5), equalTo("595")); + } + + @Test + void largestPalindrome2() { + assertThat(new Solution().largestPalindrome(1, 4), equalTo("8")); + } + + @Test + void largestPalindrome3() { + assertThat(new Solution().largestPalindrome(5, 6), equalTo("89898")); + } + + @Test + void largestPalindrome4() { + Solution solution = new Solution(); + assertEquals("9", solution.largestPalindrome(1, 1)); + assertEquals("99", solution.largestPalindrome(2, 1)); + assertEquals("999", solution.largestPalindrome(3, 1)); + } + + @Test + void largestPalindrome5() { + Solution solution = new Solution(); + assertEquals("8", solution.largestPalindrome(1, 2)); + assertEquals("88", solution.largestPalindrome(2, 2)); + assertEquals("898", solution.largestPalindrome(3, 2)); + assertEquals("8998", solution.largestPalindrome(4, 2)); + } + + @Test + void largestPalindrome6() { + Solution solution = new Solution(); + assertEquals("9", solution.largestPalindrome(1, 3)); + assertEquals("99", solution.largestPalindrome(2, 3)); + assertEquals("999", solution.largestPalindrome(3, 3)); + } + + @Test + void largestPalindrome7() { + Solution solution = new Solution(); + assertEquals("8", solution.largestPalindrome(1, 4)); + assertEquals("88", solution.largestPalindrome(2, 4)); + assertEquals("888", solution.largestPalindrome(3, 4)); + assertEquals("8888", solution.largestPalindrome(4, 4)); + assertEquals("88988", solution.largestPalindrome(5, 4)); + } + + @Test + void largestPalindrome8() { + Solution solution = new Solution(); + assertEquals("5", solution.largestPalindrome(1, 5)); + assertEquals("55", solution.largestPalindrome(2, 5)); + assertEquals("595", solution.largestPalindrome(3, 5)); + } + + @Test + void largestPalindrome9() { + Solution solution = new Solution(); + assertEquals("6", solution.largestPalindrome(1, 6)); + assertEquals("66", solution.largestPalindrome(2, 6)); + assertEquals("8778", solution.largestPalindrome(4, 6)); + } + + @Test + void largestPalindrome10() { + Solution solution = new Solution(); + assertEquals("7", solution.largestPalindrome(1, 7)); + assertEquals("77", solution.largestPalindrome(2, 7)); + assertEquals("959", solution.largestPalindrome(3, 7)); + assertEquals("99799", solution.largestPalindrome(5, 7)); + assertEquals("999999", solution.largestPalindrome(6, 7)); + assertEquals("9994999", solution.largestPalindrome(7, 7)); + } + + @Test + void largestPalindrome11() { + Solution solution = new Solution(); + assertEquals("8", solution.largestPalindrome(1, 8)); + assertEquals("88", solution.largestPalindrome(2, 8)); + assertEquals("888", solution.largestPalindrome(3, 8)); + assertEquals("8888", solution.largestPalindrome(4, 8)); + assertEquals("88888", solution.largestPalindrome(5, 8)); + } + + @Test + void largestPalindrome12() { + Solution solution = new Solution(); + assertEquals("9", solution.largestPalindrome(1, 9)); + assertEquals("99", solution.largestPalindrome(2, 9)); + assertEquals("999", solution.largestPalindrome(3, 9)); + } +} diff --git a/src/test/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/SolutionTest.java b/src/test/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/SolutionTest.java new file mode 100644 index 000000000..9ed986237 --- /dev/null +++ b/src/test/java/g3201_3300/s3261_count_substrings_that_satisfy_k_constraint_ii/SolutionTest.java @@ -0,0 +1,24 @@ +package g3201_3300.s3261_count_substrings_that_satisfy_k_constraint_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countKConstraintSubstrings() { + assertThat( + new Solution().countKConstraintSubstrings("0001111", 2, new int[][] {{0, 6}}), + equalTo(new long[] {26})); + } + + @Test + void countKConstraintSubstrings2() { + assertThat( + new Solution() + .countKConstraintSubstrings( + "010101", 1, new int[][] {{0, 5}, {1, 4}, {2, 3}}), + equalTo(new long[] {15, 9, 3})); + } +}