Skip to content

Commit

Permalink
Improved tasks 3392-3405
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jan 6, 2025
1 parent 482d125 commit 7fe8bd1
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition;

// #Easy #Array #Sliding_Window #2024_12_22_Time_1_ms_(100.00%)_Space_45.5_MB_(100.00%)
// #Easy #Array #2025_01_06_Time_1_(100.00%)_Space_44.87_(93.12%)

public class Solution {
public int countSubarrays(int[] nums) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g3301_3400.s3393_count_paths_with_the_given_xor_value;

// #Medium #Dynamic_Programming #Math #Bit_Manipulation
// #2024_12_22_Time_83_ms_(100.00%)_Space_57_MB_(100.00%)
// #Medium #Array #Dynamic_Programming #Math #Matrix #Bit_Manipulation
// #2025_01_06_Time_76_(62.72%)_Space_56.92_(68.41%)

import java.util.Arrays;

Expand Down
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;
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3301_3400.s3396_minimum_number_of_operations_to_make_elements_in_array_distinct;

// #Easy #Array #Greedy #Simulation #Distinct #Elements #Operations
// #2024_12_22_Time_4_ms_(100.00%)_Space_45_MB_(100.00%)
// #Easy #Array #Greedy #Simulation #2025_01_06_Time_3_(60.47%)_Space_44.61_(50.65%)

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3301_3400.s3397_maximum_number_of_distinct_elements_after_operations;

// #Medium #Array #Greedy #Distinct #Elements #Mathematics #Operations #Optimization
// #2024_12_22_Time_19_ms_(100.00%)_Space_57.8_MB_(100.00%)
// #Medium #Array #Sorting #Greedy #2025_01_06_Time_19_(84.32%)_Space_57.84_(93.53%)

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3301_3400.s3398_smallest_substring_with_identical_characters_i;

// #Hard #String #Greedy #Bit_Manipulation #Sliding_Window #Optimization
// #2024_12_24_Time_1_ms_(100.00%)_Space_42.9_MB_(39.83%)
// #Hard #Array #Binary_Search #Enumeration #2025_01_06_Time_1_(100.00%)_Space_42.72_(39.94%)

public class Solution {
public int minLength(String s, int ops) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3301_3400.s3399_smallest_substring_with_identical_characters_ii;

// #Hard #String #Greedy #Bit_Manipulation #Sliding_Window #Optimization
// #2024_12_24_Time_15_ms_(99.39%)_Space_45.9_MB_(43.03%)
// #Hard #Bit_Manipulation #Sliding_Window #2025_01_06_Time_15_(98.93%)_Space_45.62_(57.38%)

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing;

// #Easy #Array #Dynamic_Programming #Greedy #Matrix #Simulation
// #2024_12_29_Time_1_(100.00%)_Space_44.99_(100.00%)
// #Easy #Matrix #Simulation #2025_01_06_Time_1_(99.95%)_Space_45.24_(62.37%)

public class Solution {
public int minimumOperations(int[][] grid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i;

// #Medium #String #Sorting #Greedy #Backtracking #Combinatorics
// #2024_12_29_Time_5_(100.00%)_Space_45.20_(100.00%)
// #Medium #String #Two_Pointers #Enumeration #2025_01_06_Time_5_(89.70%)_Space_45.38_(80.39%)

public class Solution {
public String answerString(String word, int numFriends) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3401_3500.s3404_count_special_subsequences;

// #Medium #Array #Combinatorics #Dynamic #Mathematics #Programming
// #2024_12_29_Time_331_(100.00%)_Space_55.49_(100.00%)
// #Medium #Array #Hash_Table #Math #Enumeration #2025_01_06_Time_342_(73.58%)_Space_55.00_(79.58%)

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements;

// #Hard #Dynamic_Programming #Combinatorics #Arrays #Mathematics
// #2024_12_29_Time_57_(100.00%)_Space_44.55_(100.00%)
// #Hard #Math #Combinatorics #2025_01_06_Time_55_(52.53%)_Space_44.95_(37.58%)

public class Solution {
private static final int MOD = (int) (1e9 + 7);
Expand Down

0 comments on commit 7fe8bd1

Please sign in to comment.