Skip to content

Commit afd9e22

Browse files
committed
Added taskw 3407-3414
1 parent d50542e commit afd9e22

File tree

11 files changed

+833
-166
lines changed
  • src/main/java
    • g3301_3400
      • s3394_check_if_grid_can_be_cut_into_sections
      • s3395_subsequences_with_a_unique_middle_mode_i
    • g3401_3500
      • s3407_substring_matching_pattern
      • s3408_design_task_manager
      • s3409_longest_subsequence_with_decreasing_adjacent_difference
      • s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element
      • s3411_maximum_subarray_with_equal_products
      • s3412_find_mirror_score_of_a_string
      • s3413_maximum_coins_from_k_consecutive_bags
      • s3414_maximum_score_of_non_overlapping_intervals

11 files changed

+833
-166
lines changed

README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,18 +1816,26 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819-
| 3405 |[Count the Number of Arrays with K Matching Adjacent Elements](src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements)| Hard || 57 | 100.00
1820-
| 3404 |[Count Special Subsequences](src/main/java/g3401_3500/s3404_count_special_subsequences)| Medium || 331 | 100.00
1821-
| 3403 |[Find the Lexicographically Largest String From the Box I](src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i)| Medium || 5 | 100.00
1822-
| 3402 |[Minimum Operations to Make Columns Strictly Increasing](src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing)| Easy || 1 | 100.00
1823-
| 3399 |[Smallest Substring With Identical Characters II](src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii)| Hard || 15 | 99.39
1824-
| 3398 |[Smallest Substring With Identical Characters I](src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i)| Hard || 1 | 100.00
1825-
| 3397 |[Maximum Number of Distinct Elements After Operations](src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations)| Medium || 19 | 100.00
1826-
| 3396 |[Minimum Number of Operations to Make Elements in Array Distinct](src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct)| Easy || 4 | 100.00
1827-
| 3395 |[Subsequences with a Unique Middle Mode I](src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i)| Hard || 1115 | 100.00
1828-
| 3394 |[Check if Grid can be Cut into Sections](src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections)| Medium || 136 | 100.00
1829-
| 3393 |[Count Paths With the Given XOR Value](src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value)| Medium || 83 | 100.00
1830-
| 3392 |[Count Subarrays of Length Three With a Condition](src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition)| Easy || 1 | 100.00
1819+
| 3414 |[Maximum Score of Non-overlapping Intervals](src/main/java/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals)| Hard | Array, Dynamic_Programming, Sorting, Binary_Search | 64 | 95.65
1820+
| 3413 |[Maximum Coins From K Consecutive Bags](src/main/java/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags)| Medium | Array, Sorting, Greedy, Binary_Search, Prefix_Sum, Sliding_Window | 82 | 92.23
1821+
| 3412 |[Find Mirror Score of a String](src/main/java/g3401_3500/s3412_find_mirror_score_of_a_string)| Medium | String, Hash_Table, Stack, Simulation | 12 | 99.54
1822+
| 3411 |[Maximum Subarray With Equal Products](src/main/java/g3401_3500/s3411_maximum_subarray_with_equal_products)| Easy | Array, Math, Sliding_Window, Enumeration, Number_Theory | 3 | 91.26
1823+
| 3410 |[Maximize Subarray Sum After Removing All Occurrences of One Element](src/main/java/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element)| Hard | Array, Dynamic_Programming, Segment_Tree | 51 | 97.96
1824+
| 3409 |[Longest Subsequence With Decreasing Adjacent Difference](src/main/java/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference)| Medium | Array, Dynamic_Programming | 68 | 99.55
1825+
| 3408 |[Design Task Manager](src/main/java/g3401_3500/s3408_design_task_manager)| Medium | Hash_Table, Design, Heap_Priority_Queue, Ordered_Set | 349 | 100.00
1826+
| 3407 |[Substring Matching Pattern](src/main/java/g3401_3500/s3407_substring_matching_pattern)| Easy | String, String_Matching | 1 | 100.00
1827+
| 3405 |[Count the Number of Arrays with K Matching Adjacent Elements](src/main/java/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements)| Hard | Math, Combinatorics | 55 | 52.53
1828+
| 3404 |[Count Special Subsequences](src/main/java/g3401_3500/s3404_count_special_subsequences)| Medium | Array, Hash_Table, Math, Enumeration | 342 | 73.58
1829+
| 3403 |[Find the Lexicographically Largest String From the Box I](src/main/java/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i)| Medium | String, Two_Pointers, Enumeration | 5 | 89.70
1830+
| 3402 |[Minimum Operations to Make Columns Strictly Increasing](src/main/java/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing)| Easy | Matrix, Simulation | 1 | 99.95
1831+
| 3399 |[Smallest Substring With Identical Characters II](src/main/java/g3301_3400/s3399_smallest_substring_with_identical_characters_ii)| Hard | Bit_Manipulation, Sliding_Window | 15 | 98.93
1832+
| 3398 |[Smallest Substring With Identical Characters I](src/main/java/g3301_3400/s3398_smallest_substring_with_identical_characters_i)| Hard | Array, Binary_Search, Enumeration | 1 | 100.00
1833+
| 3397 |[Maximum Number of Distinct Elements After Operations](src/main/java/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations)| Medium | Array, Sorting, Greedy | 19 | 84.32
1834+
| 3396 |[Minimum Number of Operations to Make Elements in Array Distinct](src/main/java/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct)| Easy | Array, Greedy, Simulation | 3 | 60.47
1835+
| 3395 |[Subsequences with a Unique Middle Mode I](src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i)| Hard | Array, Hash_Table, Math, Combinatorics | 27 | 99.29
1836+
| 3394 |[Check if Grid can be Cut into Sections](src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections)| Medium | Geometry, Line_Sweep | 35 | 99.66
1837+
| 3393 |[Count Paths With the Given XOR Value](src/main/java/g3301_3400/s3393_count_paths_with_the_given_xor_value)| Medium | Array, Dynamic_Programming, Math, Matrix, Bit_Manipulation | 76 | 62.72
1838+
| 3392 |[Count Subarrays of Length Three With a Condition](src/main/java/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition)| Easy | Array | 1 | 100.00
18311839
| 3389 |[Minimum Operations to Make Character Frequencies Equal](src/main/java/g3301_3400/s3389_minimum_operations_to_make_character_frequencies_equal)| Hard | String, Hash_Table, Dynamic_Programming, Counting, Enumeration | 4 | 100.00
18321840
| 3388 |[Count Beautiful Splits in an Array](src/main/java/g3301_3400/s3388_count_beautiful_splits_in_an_array)| Medium | Array, Dynamic_Programming | 167 | 70.49
18331841
| 3387 |[Maximize Amount After Two Days of Conversions](src/main/java/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions)| Medium | Array, String, Depth_First_Search, Breadth_First_Search, Graph | 7 | 87.88

src/main/java/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,37 @@ import java.util.Arrays;
6666

6767
@SuppressWarnings({"unused", "java:S1172"})
6868
public class Solution {
69-
public boolean checkValidCuts(int n, int[][] rectangles) {
70-
int m = rectangles.length;
71-
int[][] xAxis = new int[m][2];
72-
int[][] yAxis = new int[m][2];
73-
int ind = 0;
74-
for (int[] axis : rectangles) {
75-
int startX = axis[0];
76-
int startY = axis[1];
77-
int endX = axis[2];
78-
int endY = axis[3];
79-
xAxis[ind] = new int[] {startX, endX};
80-
yAxis[ind] = new int[] {startY, endY};
81-
ind++;
69+
private static final int MASK = (1 << 30) - 1;
70+
71+
public boolean checkValidCuts(int m, int[][] rectangles) {
72+
int n = rectangles.length;
73+
long[] start = new long[n];
74+
for (int i = 0; i < n; i++) {
75+
start[i] = ((long) rectangles[i][1] << 32) + rectangles[i][3];
8276
}
83-
Arrays.sort(xAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
84-
Arrays.sort(yAxis, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
85-
int verticalCuts = findSections(xAxis);
86-
if (verticalCuts > 2) {
77+
Arrays.sort(start);
78+
if (validate(start)) {
8779
return true;
8880
}
89-
int horizontalCuts = findSections(yAxis);
90-
return horizontalCuts > 2;
81+
for (int i = 0; i < n; i++) {
82+
start[i] = ((long) rectangles[i][0] << 32) + rectangles[i][2];
83+
}
84+
Arrays.sort(start);
85+
return validate(start);
9186
}
9287

93-
private int findSections(int[][] axis) {
94-
int end = axis[0][1];
95-
int sections = 1;
96-
for (int i = 1; i < axis.length; i++) {
97-
if (end > axis[i][0]) {
98-
end = Math.max(end, axis[i][1]);
99-
} else {
100-
sections++;
101-
end = axis[i][1];
102-
}
103-
if (sections > 2) {
104-
return sections;
88+
private boolean validate(long[] arr) {
89+
int cut = 0;
90+
int n = arr.length;
91+
int max = (int) arr[0] & MASK;
92+
for (int i = 0; i < n; i++) {
93+
int start = (int) (arr[i] >> 32);
94+
if (start >= max && ++cut == 2) {
95+
return true;
10596
}
97+
max = Math.max(max, (int) (arr[i] & MASK));
10698
}
107-
return sections;
99+
return false;
108100
}
109101
}
110102
```

src/main/java/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/readme.md

Lines changed: 51 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -55,140 +55,69 @@ There is no subsequence of length 5 with a unique middle mode.
5555
## Solution
5656

5757
```java
58-
import java.util.ArrayList;
5958
import java.util.HashMap;
60-
import java.util.List;
6159
import java.util.Map;
6260

6361
public class Solution {
64-
private static final int MOD = 1000000007;
65-
66-
public int subsequencesWithMiddleMode(int[] a) {
67-
int n = a.length;
68-
// Create a dictionary to store indices of each number
69-
Map<Integer, List<Integer>> dict = new HashMap<>();
70-
for (int i = 0; i < n; i++) {
71-
dict.computeIfAbsent(a[i], k -> new ArrayList<>()).add(i);
72-
}
73-
long ans = 0L;
74-
// Iterate over each unique number and its indices
75-
for (Map.Entry<Integer, List<Integer>> entry : dict.entrySet()) {
76-
List<Integer> b = entry.getValue();
77-
int m = b.size();
78-
for (int k = 0; k < m; k++) {
79-
int i = b.get(k);
80-
int r = m - 1 - k;
81-
int u = i - k;
82-
int v = (n - 1 - i) - r;
83-
// Case 2: Frequency of occurrence is 2 times
84-
ans = (ans + convert(k, 1) * convert(u, 1) % MOD * convert(v, 2) % MOD) % MOD;
85-
ans = (ans + convert(r, 1) * convert(u, 2) % MOD * convert(v, 1) % MOD) % MOD;
86-
// Case 3: Frequency of occurrence is 3 times
87-
ans = (ans + convert(k, 2) * convert(v, 2) % MOD) % MOD;
88-
ans = (ans + convert(r, 2) * convert(u, 2) % MOD) % MOD;
89-
ans =
90-
(ans
91-
+ convert(k, 1)
92-
* convert(r, 1)
93-
% MOD
94-
* convert(u, 1)
95-
% MOD
96-
* convert(v, 1)
97-
% MOD)
98-
% MOD;
99-
100-
// Case 4: Frequency of occurrence is 4 times
101-
ans = (ans + convert(k, 2) * convert(r, 1) % MOD * convert(v, 1) % MOD) % MOD;
102-
ans = (ans + convert(k, 1) * convert(r, 2) % MOD * convert(u, 1) % MOD) % MOD;
103-
104-
// Case 5: Frequency of occurrence is 5 times
105-
ans = (ans + convert(k, 2) * convert(r, 2) % MOD) % MOD;
62+
private static final int MOD = (int) 1e9 + 7;
63+
private long[] c2 = new long[1001];
64+
65+
public int subsequencesWithMiddleMode(int[] nums) {
66+
if (c2[2] == 0) {
67+
c2[0] = c2[1] = 0;
68+
c2[2] = 1;
69+
for (int i = 3; i < c2.length; ++i) {
70+
c2[i] = i * (i - 1) / 2;
10671
}
10772
}
108-
long dif = 0;
109-
// Principle of inclusion-exclusion
110-
for (Map.Entry<Integer, List<Integer>> midEntry : dict.entrySet()) {
111-
List<Integer> b = midEntry.getValue();
112-
int m = b.size();
113-
for (Map.Entry<Integer, List<Integer>> tmpEntry : dict.entrySet()) {
114-
if (!midEntry.getKey().equals(tmpEntry.getKey())) {
115-
List<Integer> c = tmpEntry.getValue();
116-
int size = c.size();
117-
int k = 0;
118-
int j = 0;
119-
while (k < m) {
120-
int i = b.get(k);
121-
int r = m - 1 - k;
122-
int u = i - k;
123-
int v = (n - 1 - i) - r;
124-
while (j < size && c.get(j) < i) {
125-
j++;
126-
}
127-
int x = j;
128-
int y = size - x;
129-
dif =
130-
(dif
131-
+ convert(k, 1)
132-
* convert(x, 1)
133-
% MOD
134-
* convert(y, 1)
135-
% MOD
136-
* convert(v - y, 1)
137-
% MOD)
138-
% MOD;
139-
dif =
140-
(dif
141-
+ convert(k, 1)
142-
* convert(y, 2)
143-
% MOD
144-
* convert(u - x, 1)
145-
% MOD)
146-
% MOD;
147-
dif =
148-
(dif + convert(k, 1) * convert(x, 1) % MOD * convert(y, 2) % MOD)
149-
% MOD;
150-
151-
dif =
152-
(dif
153-
+ convert(r, 1)
154-
* convert(x, 1)
155-
% MOD
156-
* convert(y, 1)
157-
% MOD
158-
* convert(u - x, 1)
159-
% MOD)
160-
% MOD;
161-
dif =
162-
(dif
163-
+ convert(r, 1)
164-
* convert(x, 2)
165-
% MOD
166-
* convert(v - y, 1)
167-
% MOD)
168-
% MOD;
169-
dif =
170-
(dif + convert(r, 1) * convert(x, 2) % MOD * convert(y, 1) % MOD)
171-
% MOD;
172-
k++;
173-
}
174-
}
73+
int n = nums.length;
74+
int[] newNums = new int[n];
75+
Map<Integer, Integer> map = new HashMap<>(n);
76+
int m = 0;
77+
int index = 0;
78+
for (int x : nums) {
79+
Integer id = map.get(x);
80+
if (id == null) {
81+
id = m++;
82+
map.put(x, id);
17583
}
84+
newNums[index++] = id;
17685
}
177-
return (int) ((ans - dif + MOD) % MOD);
178-
}
179-
180-
private long convert(int n, int k) {
181-
if (k > n) {
86+
if (m == n) {
18287
return 0;
18388
}
184-
if (k == 0 || k == n) {
185-
return 1;
89+
int[] rightCount = new int[m];
90+
for (int x : newNums) {
91+
rightCount[x]++;
18692
}
187-
long res = 1;
188-
for (int i = 0; i < k; i++) {
189-
res = res * (n - i) / (i + 1);
93+
int[] leftCount = new int[m];
94+
long ans = (long) n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120;
95+
for (int left = 0; left < n - 2; left++) {
96+
int x = newNums[left];
97+
rightCount[x]--;
98+
if (left >= 2) {
99+
int right = n - (left + 1);
100+
int leftX = leftCount[x];
101+
int rightX = rightCount[x];
102+
ans -= c2[left - leftX] * c2[right - rightX];
103+
for (int y = 0; y < m; ++y) {
104+
if (y == x) {
105+
continue;
106+
}
107+
int rightY = rightCount[y];
108+
int leftY = leftCount[y];
109+
ans -= c2[leftY] * rightX * (right - rightX);
110+
ans -= c2[rightY] * leftX * (left - leftX);
111+
ans -=
112+
leftY
113+
* rightY
114+
* (leftX * (right - rightX - rightY)
115+
+ rightX * (left - leftX - leftY));
116+
}
117+
}
118+
leftCount[x]++;
190119
}
191-
return res % MOD;
120+
return (int) (ans % MOD);
192121
}
193122
}
194123
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3407\. Substring Matching Pattern
5+
6+
Easy
7+
8+
You are given a string `s` and a pattern string `p`, where `p` contains **exactly one** `'*'` character.
9+
10+
The `'*'` in `p` can be replaced with any sequence of zero or more characters.
11+
12+
Return `true` if `p` can be made a substring of `s`, and `false` otherwise.
13+
14+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "leetcode", p = "ee\*e"
19+
20+
**Output:** true
21+
22+
**Explanation:**
23+
24+
By replacing the `'*'` with `"tcod"`, the substring `"eetcode"` matches the pattern.
25+
26+
**Example 2:**
27+
28+
**Input:** s = "car", p = "c\*v"
29+
30+
**Output:** false
31+
32+
**Explanation:**
33+
34+
There is no substring matching the pattern.
35+
36+
**Example 3:**
37+
38+
**Input:** s = "luck", p = "u\*"
39+
40+
**Output:** true
41+
42+
**Explanation:**
43+
44+
The substrings `"u"`, `"uc"`, and `"uck"` match the pattern.
45+
46+
**Constraints:**
47+
48+
* `1 <= s.length <= 50`
49+
* `1 <= p.length <= 50`
50+
* `s` contains only lowercase English letters.
51+
* `p` contains only lowercase English letters and exactly one `'*'`
52+
53+
## Solution
54+
55+
```java
56+
public class Solution {
57+
public boolean hasMatch(String s, String p) {
58+
int index = -1;
59+
for (int i = 0; i < p.length(); i++) {
60+
if (p.charAt(i) == '*') {
61+
index = i;
62+
break;
63+
}
64+
}
65+
int num1 = fun(s, p.substring(0, index));
66+
if (num1 == -1) {
67+
return false;
68+
}
69+
int num2 = fun(s.substring(num1), p.substring(index + 1));
70+
return num2 != -1;
71+
}
72+
73+
private int fun(String s, String k) {
74+
int n = s.length();
75+
int m = k.length();
76+
int j = 0;
77+
for (int i = 0; i <= n - m; i++) {
78+
for (j = 0; j < m; j++) {
79+
char ch1 = s.charAt(j + i);
80+
char ch2 = k.charAt(j);
81+
if (ch1 != ch2) {
82+
break;
83+
}
84+
}
85+
if (j == m) {
86+
return i + j;
87+
}
88+
}
89+
return -1;
90+
}
91+
}
92+
```

0 commit comments

Comments
 (0)