-
Notifications
You must be signed in to change notification settings - Fork 72
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
12 changed files
with
87 additions
and
176 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/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
4 changes: 2 additions & 2 deletions
4
src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value/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
59 changes: 25 additions & 34 deletions
59
src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/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 |
---|---|---|
@@ -1,50 +1,41 @@ | ||
package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections; | ||
|
||
// #Medium #Sorting #Greedy #Simulation #Geometry #Line_Sweep #Grid | ||
// #2024_12_22_Time_136_ms_(100.00%)_Space_128.7_MB_(100.00%) | ||
// #Medium #Geometry #Line_Sweep #2025_01_06_Time_35_(99.66%)_Space_117.96_(80.52%) | ||
|
||
import java.util.Arrays; | ||
|
||
@SuppressWarnings({"unused", "java:S1172"}) | ||
public class Solution { | ||
public boolean checkValidCuts(int n, int[][] rectangles) { | ||
int m = rectangles.length; | ||
int[][] xAxis = new int[m][2]; | ||
int[][] yAxis = new int[m][2]; | ||
int ind = 0; | ||
for (int[] axis : rectangles) { | ||
int startX = axis[0]; | ||
int startY = axis[1]; | ||
int endX = axis[2]; | ||
int endY = axis[3]; | ||
xAxis[ind] = new int[] {startX, endX}; | ||
yAxis[ind] = new int[] {startY, endY}; | ||
ind++; | ||
private static final int MASK = (1 << 30) - 1; | ||
|
||
public boolean checkValidCuts(int m, int[][] rectangles) { | ||
int n = rectangles.length; | ||
long[] start = new long[n]; | ||
for (int i = 0; i < n; i++) { | ||
start[i] = ((long) rectangles[i][1] << 32) + rectangles[i][3]; | ||
} | ||
Arrays.sort(xAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); | ||
Arrays.sort(yAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); | ||
int verticalCuts = findSections(xAxis); | ||
if (verticalCuts > 2) { | ||
Arrays.sort(start); | ||
if (validate(start)) { | ||
return true; | ||
} | ||
int horizontalCuts = findSections(yAxis); | ||
return horizontalCuts > 2; | ||
for (int i = 0; i < n; i++) { | ||
start[i] = ((long) rectangles[i][0] << 32) + rectangles[i][2]; | ||
} | ||
Arrays.sort(start); | ||
return validate(start); | ||
} | ||
|
||
private int findSections(int[][] axis) { | ||
int end = axis[0][1]; | ||
int sections = 1; | ||
for (int i = 1; i < axis.length; i++) { | ||
if (end > axis[i][0]) { | ||
end = Math.max(end, axis[i][1]); | ||
} else { | ||
sections++; | ||
end = axis[i][1]; | ||
} | ||
if (sections > 2) { | ||
return sections; | ||
private boolean validate(long[] arr) { | ||
int cut = 0; | ||
int n = arr.length; | ||
int max = (int) arr[0] & MASK; | ||
for (int i = 0; i < n; i++) { | ||
int start = (int) (arr[i] >> 32); | ||
if (start >= max && ++cut == 2) { | ||
return true; | ||
} | ||
max = Math.max(max, (int) (arr[i] & MASK)); | ||
} | ||
return sections; | ||
return false; | ||
} | ||
} |
174 changes: 51 additions & 123 deletions
174
src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/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 |
---|---|---|
@@ -1,141 +1,69 @@ | ||
package g3301_3400.s3395_subsequences_with_a_unique_middle_mode_i; | ||
|
||
// #Hard #Dynamic_Programming #Sliding_Window #Combinatorics #Subsequence | ||
// #2024_12_22_Time_1115_ms_(100.00%)_Space_45.2_MB_(100.00%) | ||
// #Hard #Array #Hash_Table #Math #Combinatorics #2025_01_06_Time_27_(99.29%)_Space_45.15_(97.87%) | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Solution { | ||
private static final int MOD = 1000000007; | ||
private static final int MOD = (int) 1e9 + 7; | ||
private long[] c2 = new long[1001]; | ||
|
||
public int subsequencesWithMiddleMode(int[] a) { | ||
int n = a.length; | ||
// Create a dictionary to store indices of each number | ||
Map<Integer, List<Integer>> dict = new HashMap<>(); | ||
for (int i = 0; i < n; i++) { | ||
dict.computeIfAbsent(a[i], k -> new ArrayList<>()).add(i); | ||
} | ||
long ans = 0L; | ||
// Iterate over each unique number and its indices | ||
for (Map.Entry<Integer, List<Integer>> entry : dict.entrySet()) { | ||
List<Integer> b = entry.getValue(); | ||
int m = b.size(); | ||
for (int k = 0; k < m; k++) { | ||
int i = b.get(k); | ||
int r = m - 1 - k; | ||
int u = i - k; | ||
int v = (n - 1 - i) - r; | ||
// Case 2: Frequency of occurrence is 2 times | ||
ans = (ans + convert(k, 1) * convert(u, 1) % MOD * convert(v, 2) % MOD) % MOD; | ||
ans = (ans + convert(r, 1) * convert(u, 2) % MOD * convert(v, 1) % MOD) % MOD; | ||
// Case 3: Frequency of occurrence is 3 times | ||
ans = (ans + convert(k, 2) * convert(v, 2) % MOD) % MOD; | ||
ans = (ans + convert(r, 2) * convert(u, 2) % MOD) % MOD; | ||
ans = | ||
(ans | ||
+ convert(k, 1) | ||
* convert(r, 1) | ||
% MOD | ||
* convert(u, 1) | ||
% MOD | ||
* convert(v, 1) | ||
% MOD) | ||
% MOD; | ||
|
||
// Case 4: Frequency of occurrence is 4 times | ||
ans = (ans + convert(k, 2) * convert(r, 1) % MOD * convert(v, 1) % MOD) % MOD; | ||
ans = (ans + convert(k, 1) * convert(r, 2) % MOD * convert(u, 1) % MOD) % MOD; | ||
|
||
// Case 5: Frequency of occurrence is 5 times | ||
ans = (ans + convert(k, 2) * convert(r, 2) % MOD) % MOD; | ||
public int subsequencesWithMiddleMode(int[] nums) { | ||
if (c2[2] == 0) { | ||
c2[0] = c2[1] = 0; | ||
c2[2] = 1; | ||
for (int i = 3; i < c2.length; ++i) { | ||
c2[i] = i * (i - 1) / 2; | ||
} | ||
} | ||
long dif = 0; | ||
// Principle of inclusion-exclusion | ||
for (Map.Entry<Integer, List<Integer>> midEntry : dict.entrySet()) { | ||
List<Integer> b = midEntry.getValue(); | ||
int m = b.size(); | ||
for (Map.Entry<Integer, List<Integer>> tmpEntry : dict.entrySet()) { | ||
if (!midEntry.getKey().equals(tmpEntry.getKey())) { | ||
List<Integer> c = tmpEntry.getValue(); | ||
int size = c.size(); | ||
int k = 0; | ||
int j = 0; | ||
while (k < m) { | ||
int i = b.get(k); | ||
int r = m - 1 - k; | ||
int u = i - k; | ||
int v = (n - 1 - i) - r; | ||
while (j < size && c.get(j) < i) { | ||
j++; | ||
} | ||
int x = j; | ||
int y = size - x; | ||
dif = | ||
(dif | ||
+ convert(k, 1) | ||
* convert(x, 1) | ||
% MOD | ||
* convert(y, 1) | ||
% MOD | ||
* convert(v - y, 1) | ||
% MOD) | ||
% MOD; | ||
dif = | ||
(dif | ||
+ convert(k, 1) | ||
* convert(y, 2) | ||
% MOD | ||
* convert(u - x, 1) | ||
% MOD) | ||
% MOD; | ||
dif = | ||
(dif + convert(k, 1) * convert(x, 1) % MOD * convert(y, 2) % MOD) | ||
% MOD; | ||
|
||
dif = | ||
(dif | ||
+ convert(r, 1) | ||
* convert(x, 1) | ||
% MOD | ||
* convert(y, 1) | ||
% MOD | ||
* convert(u - x, 1) | ||
% MOD) | ||
% MOD; | ||
dif = | ||
(dif | ||
+ convert(r, 1) | ||
* convert(x, 2) | ||
% MOD | ||
* convert(v - y, 1) | ||
% MOD) | ||
% MOD; | ||
dif = | ||
(dif + convert(r, 1) * convert(x, 2) % MOD * convert(y, 1) % MOD) | ||
% MOD; | ||
k++; | ||
} | ||
} | ||
int n = nums.length; | ||
int[] newNums = new int[n]; | ||
Map<Integer, Integer> map = new HashMap<>(n); | ||
int m = 0; | ||
int index = 0; | ||
for (int x : nums) { | ||
Integer id = map.get(x); | ||
if (id == null) { | ||
id = m++; | ||
map.put(x, id); | ||
} | ||
newNums[index++] = id; | ||
} | ||
return (int) ((ans - dif + MOD) % MOD); | ||
} | ||
|
||
private long convert(int n, int k) { | ||
if (k > n) { | ||
if (m == n) { | ||
return 0; | ||
} | ||
if (k == 0 || k == n) { | ||
return 1; | ||
int[] rightCount = new int[m]; | ||
for (int x : newNums) { | ||
rightCount[x]++; | ||
} | ||
long res = 1; | ||
for (int i = 0; i < k; i++) { | ||
res = res * (n - i) / (i + 1); | ||
int[] leftCount = new int[m]; | ||
long ans = (long) n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120; | ||
for (int left = 0; left < n - 2; left++) { | ||
int x = newNums[left]; | ||
rightCount[x]--; | ||
if (left >= 2) { | ||
int right = n - (left + 1); | ||
int leftX = leftCount[x]; | ||
int rightX = rightCount[x]; | ||
ans -= c2[left - leftX] * c2[right - rightX]; | ||
for (int y = 0; y < m; ++y) { | ||
if (y == x) { | ||
continue; | ||
} | ||
int rightY = rightCount[y]; | ||
int leftY = leftCount[y]; | ||
ans -= c2[leftY] * rightX * (right - rightX); | ||
ans -= c2[rightY] * leftX * (left - leftX); | ||
ans -= | ||
leftY | ||
* rightY | ||
* (leftX * (right - rightX - rightY) | ||
+ rightX * (left - leftX - leftY)); | ||
} | ||
} | ||
leftCount[x]++; | ||
} | ||
return res % MOD; | ||
return (int) (ans % MOD); | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
..._3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/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
3 changes: 1 addition & 2 deletions
3
.../java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/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
3 changes: 1 addition & 2 deletions
3
src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i/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
3 changes: 1 addition & 2 deletions
3
src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/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
3 changes: 1 addition & 2 deletions
3
...ava/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/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
3 changes: 1 addition & 2 deletions
3
...a/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/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
3 changes: 1 addition & 2 deletions
3
src/main/java/g3401_3500/s3404_count_special_subsequences/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
3 changes: 1 addition & 2 deletions
3
...401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/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