forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
450 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...g2901_3000/s2962_count_subarrays_where_max_element_appears_at_least_k_times/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package g2901_3000.s2962_count_subarrays_where_max_element_appears_at_least_k_times; | ||
|
||
// #Medium #Array #Sliding_Window #2024_01_16_Time_3_ms_(100.00%)_Space_61.6_MB_(12.43%) | ||
|
||
public class Solution { | ||
public long countSubarrays(int[] n, int k) { | ||
int[] st = new int[n.length + 1]; | ||
int si = 0; | ||
int m = 0; | ||
for (int i = 0; i < n.length; i++) { | ||
if (m < n[i]) { | ||
m = n[i]; | ||
si = 0; | ||
} | ||
if (m == n[i]) { | ||
st[si++] = i; | ||
} | ||
} | ||
if (si < k) { | ||
return 0; | ||
} | ||
long r = 0; | ||
st[si] = n.length; | ||
for (int i = k; i <= si; i++) { | ||
r += (long) (st[i - k] + 1) * (st[i] - st[i - 1]); | ||
} | ||
return r; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...3000/s2962_count_subarrays_where_max_element_appears_at_least_k_times/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
2962\. Count Subarrays Where Max Element Appears at Least K Times | ||
|
||
Medium | ||
|
||
You are given an integer array `nums` and a **positive** integer `k`. | ||
|
||
Return _the number of subarrays where the **maximum** element of_ `nums` _appears **at least**_ `k` _times in that subarray._ | ||
|
||
A **subarray** is a contiguous sequence of elements within an array. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,3,2,3,3], k = 2 | ||
|
||
**Output:** 6 | ||
|
||
**Explanation:** The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,4,2,1], k = 3 | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** No subarray contains the element 4 at least 3 times. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length <= 10<sup>5</sup></code> | ||
* <code>1 <= nums[i] <= 10<sup>6</sup></code> | ||
* <code>1 <= k <= 10<sup>5</sup></code> |
34 changes: 34 additions & 0 deletions
34
src/main/java/g2901_3000/s2963_count_the_number_of_good_partitions/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package g2901_3000.s2963_count_the_number_of_good_partitions; | ||
|
||
// #Hard #Array #Hash_Table #Math #Combinatorics | ||
// #2024_01_16_Time_30_ms_(80.04%)_Space_64.3_MB_(30.54%) | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Solution { | ||
public int numberOfGoodPartitions(int[] nums) { | ||
Map<Integer, Integer> mp = new HashMap<>(); | ||
int n = nums.length; | ||
for (int i = 0; i < n; i++) { | ||
mp.put(nums[i], i); | ||
} | ||
int i = 0; | ||
int j = 0; | ||
int cnt = 0; | ||
while (i < n) { | ||
j = Math.max(j, mp.get(nums[i])); | ||
if (i == j) { | ||
cnt++; | ||
} | ||
i++; | ||
} | ||
int res = 1; | ||
for (int k = 1; k < cnt; k++) { | ||
res = res * 2; | ||
int mod = 1000000007; | ||
res %= mod; | ||
} | ||
return res; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/g2901_3000/s2963_count_the_number_of_good_partitions/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
2963\. Count the Number of Good Partitions | ||
|
||
Hard | ||
|
||
You are given a **0-indexed** array `nums` consisting of **positive** integers. | ||
|
||
A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number. | ||
|
||
Return _the **total number** of good partitions of_ `nums`. | ||
|
||
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,3,4] | ||
|
||
**Output:** 8 | ||
|
||
**Explanation:** The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]). | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,1,1,1] | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** The only possible good partition is: ([1,1,1,1]). | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [1,2,1,3] | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]). | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length <= 10<sup>5</sup></code> | ||
* <code>1 <= nums[i] <= 10<sup>9</sup></code> |
22 changes: 22 additions & 0 deletions
22
src/main/java/g2901_3000/s2965_find_missing_and_repeated_values/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package g2901_3000.s2965_find_missing_and_repeated_values; | ||
|
||
// #Easy #Array #Hash_Table #Math #Matrix #2024_01_16_Time_1_ms_(100.00%)_Space_45.4_MB_(17.99%) | ||
|
||
public class Solution { | ||
public int[] findMissingAndRepeatedValues(int[][] grid) { | ||
int nSquare = grid.length * grid.length; | ||
int sum = nSquare * (nSquare + 1) / 2; | ||
boolean[] found = new boolean[nSquare + 1]; | ||
int repeated = 1; | ||
for (int[] row : grid) { | ||
for (int n : row) { | ||
sum -= n; | ||
if (found[n]) { | ||
repeated = n; | ||
} | ||
found[n] = true; | ||
} | ||
} | ||
return new int[] {repeated, sum + repeated}; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/g2901_3000/s2965_find_missing_and_repeated_values/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
2965\. Find Missing and Repeated Values | ||
|
||
Easy | ||
|
||
You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range <code>[1, n<sup>2</sup>]</code>. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`. | ||
|
||
Return _a **0-indexed** integer array_ `ans` _of size_ `2` _where_ `ans[0]` _equals to_ `a` _and_ `ans[1]` _equals to_ `b`_._ | ||
|
||
**Example 1:** | ||
|
||
**Input:** grid = [[1,3],[2,2]] | ||
|
||
**Output:** [2,4] | ||
|
||
**Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4]. | ||
|
||
**Example 2:** | ||
|
||
**Input:** grid = [[9,1,7],[8,9,2],[3,4,6]] | ||
|
||
**Output:** [9,5] | ||
|
||
**Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5]. | ||
|
||
**Constraints:** | ||
|
||
* `2 <= n == grid.length == grid[i].length <= 50` | ||
* `1 <= grid[i][j] <= n * n` | ||
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members. | ||
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members. | ||
* For all `x` that `1 <= x <= n * n` except two of them there is exatly one pair of `i, j` that `0 <= i, j <= n - 1` and `grid[i][j] == x`. |
23 changes: 23 additions & 0 deletions
23
src/main/java/g2901_3000/s2966_divide_array_into_arrays_with_max_difference/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package g2901_3000.s2966_divide_array_into_arrays_with_max_difference; | ||
|
||
// #Medium #Array #Sorting #Greedy #2024_01_16_Time_20_ms_(99.04%)_Space_59.4_MB_(10.50%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public int[][] divideArray(int[] nums, int k) { | ||
Arrays.sort(nums); | ||
int n = nums.length; | ||
int triplets = n / 3; | ||
int[][] result = new int[triplets][]; | ||
for (int i = 0, j = 0; i < n; i += 3, j++) { | ||
int first = nums[i]; | ||
int third = nums[i + 2]; | ||
if (third - first > k) { | ||
return new int[0][]; | ||
} | ||
result[j] = new int[] {first, nums[i + 1], third}; | ||
} | ||
return result; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...in/java/g2901_3000/s2966_divide_array_into_arrays_with_max_difference/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
2966\. Divide Array Into Arrays With Max Difference | ||
|
||
Medium | ||
|
||
You are given an integer array `nums` of size `n` and a positive integer `k`. | ||
|
||
Divide the array into one or more arrays of size `3` satisfying the following conditions: | ||
|
||
* **Each** element of `nums` should be in **exactly** one array. | ||
* The difference between **any** two elements in one array is less than or equal to `k`. | ||
|
||
Return _a_ **2D** _array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them._ | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2 | ||
|
||
**Output:** [[1,1,3],[3,4,5],[7,8,9]] | ||
|
||
**Explanation:** We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9]. The difference between any two elements in each array is less than or equal to 2. Note that the order of elements is not important. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,3,3,2,7,3], k = 3 | ||
|
||
**Output:** [] | ||
|
||
**Explanation:** It is not possible to divide the array satisfying all the conditions. | ||
|
||
**Constraints:** | ||
|
||
* `n == nums.length` | ||
* <code>1 <= n <= 10<sup>5</sup></code> | ||
* `n` is a multiple of `3`. | ||
* <code>1 <= nums[i] <= 10<sup>5</sup></code> | ||
* <code>1 <= k <= 10<sup>5</sup></code> |
48 changes: 48 additions & 0 deletions
48
src/main/java/g2901_3000/s2967_minimum_cost_to_make_array_equalindromic/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package g2901_3000.s2967_minimum_cost_to_make_array_equalindromic; | ||
|
||
// #Medium #Array #Math #Sorting #Greedy #2024_01_16_Time_15_ms_(97.78%)_Space_56.5_MB_(20.47%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public long minimumCost(int[] nums) { | ||
Arrays.sort(nums); | ||
int len = nums.length; | ||
int m = len % 2 != 0 ? len / 2 : len / 2 - 1; | ||
int previousPalindrome = getPreviousPalindrome(nums[m]); | ||
int nextPalindrome = getNextPalindrome(nums[m]); | ||
long ans1 = 0; | ||
long ans2 = 0; | ||
for (int num : nums) { | ||
ans1 += Math.abs(previousPalindrome - num); | ||
ans2 += Math.abs(nextPalindrome - num); | ||
} | ||
return Math.min(ans1, ans2); | ||
} | ||
|
||
private int getPreviousPalindrome(int num) { | ||
int previousPalindrome = num; | ||
while (!isPalindrome(previousPalindrome)) { | ||
previousPalindrome--; | ||
} | ||
return previousPalindrome; | ||
} | ||
|
||
private int getNextPalindrome(int num) { | ||
int nextPalindrome = num; | ||
while (!isPalindrome(nextPalindrome)) { | ||
nextPalindrome++; | ||
} | ||
return nextPalindrome; | ||
} | ||
|
||
private boolean isPalindrome(int num) { | ||
int copyNum = num; | ||
int reverseNum = 0; | ||
while (num > 0) { | ||
reverseNum = reverseNum * 10 + num % 10; | ||
num /= 10; | ||
} | ||
return copyNum == reverseNum; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/g2901_3000/s2967_minimum_cost_to_make_array_equalindromic/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
2967\. Minimum Cost to Make Array Equalindromic | ||
|
||
Medium | ||
|
||
You are given a **0-indexed** integer array `nums` having length `n`. | ||
|
||
You are allowed to perform a special move **any** number of times (**including zero**) on `nums`. In one **special** **move** you perform the following steps **in order**: | ||
|
||
* Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`. | ||
* Add `|nums[i] - x|` to the total cost. | ||
* Change the value of `nums[i]` to `x`. | ||
|
||
A **palindromic number** is a positive integer that remains the same when its digits are reversed. For example, `121`, `2552` and `65756` are palindromic numbers whereas `24`, `46`, `235` are not palindromic numbers. | ||
|
||
An array is considered **equalindromic** if all the elements in the array are equal to an integer `y`, where `y` is a **palindromic number** less than <code>10<sup>9</sup></code>. | ||
|
||
Return _an integer denoting the **minimum** possible total cost to make_ `nums` _**equalindromic** by performing any number of special moves._ | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,3,4,5] | ||
|
||
**Output:** 6 | ||
|
||
**Explanation:** We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6. It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [10,12,13,14,15] | ||
|
||
**Output:** 11 | ||
|
||
**Explanation:** We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11. It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [22,33,22,33,22] | ||
|
||
**Output:** 22 | ||
|
||
**Explanation:** We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22. It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= n <= 10<sup>5</sup></code> | ||
* <code>1 <= nums[i] <= 10<sup>9</sup></code> |
18 changes: 18 additions & 0 deletions
18
...1_3000/s2962_count_subarrays_where_max_element_appears_at_least_k_times/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package g2901_3000.s2962_count_subarrays_where_max_element_appears_at_least_k_times; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void countSubarrays() { | ||
assertThat(new Solution().countSubarrays(new int[] {1, 3, 2, 3, 3}, 2), equalTo(6L)); | ||
} | ||
|
||
@Test | ||
void countSubarrays2() { | ||
assertThat(new Solution().countSubarrays(new int[] {1, 4, 2, 1}, 3), equalTo(0L)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/g2901_3000/s2963_count_the_number_of_good_partitions/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package g2901_3000.s2963_count_the_number_of_good_partitions; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void numberOfGoodPartitions() { | ||
assertThat(new Solution().numberOfGoodPartitions(new int[] {1, 2, 3, 4}), equalTo(8)); | ||
} | ||
|
||
@Test | ||
void numberOfGoodPartitions2() { | ||
assertThat(new Solution().numberOfGoodPartitions(new int[] {1, 1, 1, 1}), equalTo(1)); | ||
} | ||
|
||
@Test | ||
void numberOfGoodPartitions3() { | ||
assertThat(new Solution().numberOfGoodPartitions(new int[] {1, 2, 1, 3}), equalTo(2)); | ||
} | ||
} |
Oops, something went wrong.