Skip to content

Commit 08faaee

Browse files
committed
Added tasks 3483-3490
1 parent dc35b29 commit 08faaee

File tree

13 files changed

+1083
-452
lines changed
  • src/main/java
    • g3101_3200/s3161_block_placement_queries
    • g3201_3300/s3245_alternating_groups_iii
    • g3301_3400/s3327_check_if_dfs_strings_are_palindromes
    • g3401_3500

13 files changed

+1083
-452
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3490 |[Count Beautiful Numbers](src/main/java/g3401_3500/s3490_count_beautiful_numbers)| Hard | Dynamic_Programming | 523 | 100.00
2092+
| 3489 |[Zero Array Transformation IV](src/main/java/g3401_3500/s3489_zero_array_transformation_iv)| Medium | Array, Dynamic_Programming | 142 | 100.00
2093+
| 3488 |[Closest Equal Element Queries](src/main/java/g3401_3500/s3488_closest_equal_element_queries)| Medium | Array, Hash_Table, Binary_Search | 50 | 100.00
2094+
| 3487 |[Maximum Unique Subarray Sum After Deletion](src/main/java/g3401_3500/s3487_maximum_unique_subarray_sum_after_deletion)| Easy | Array, Hash_Table, Greedy | 2 | 100.00
2095+
| 3486 |[Longest Special Path II](src/main/java/g3401_3500/s3486_longest_special_path_ii)| Hard | Array, Hash_Table, Tree, Prefix_Sum, Depth_First_Search | 166 | 166 ms ( %)
2096+
| 3485 |[Longest Common Prefix of K Strings After Removal](src/main/java/g3401_3500/s3485_longest_common_prefix_of_k_strings_after_removal)| Hard | Array, String, Trie | 333 | 100.00
2097+
| 3484 |[Design Spreadsheet](src/main/java/g3401_3500/s3484_design_spreadsheet)| Medium | Array, String, Hash_Table, Matrix, Design | 117 | 100.00
2098+
| 3483 |[Unique 3-Digit Even Numbers](src/main/java/g3401_3500/s3483_unique_3_digit_even_numbers)| Easy | Array, Hash_Table, Recursion, Enumeration | 5 | 100.00
20912099
| 3482 |[Analyze Organization Hierarchy](src/main/java/g3401_3500/s3482_analyze_organization_hierarchy)| Hard | Database | 712 | 100.00
20922100
| 3480 |[Maximize Subarrays After Removing One Conflicting Pair](src/main/java/g3401_3500/s3480_maximize_subarrays_after_removing_one_conflicting_pair)| Hard | Array, Prefix_Sum, Enumeration, Segment_Tree | 20 | 98.86
20932101
| 3479 |[Fruits Into Baskets III](src/main/java/g3401_3500/s3479_fruits_into_baskets_iii)| Medium | Array, Binary_Search, Ordered_Set, Segment_Tree | 38 | 97.76
@@ -2124,7 +2132,7 @@
21242132
| 3444 |[Minimum Increments for Target Multiples in an Array](src/main/java/g3401_3500/s3444_minimum_increments_for_target_multiples_in_an_array)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask, Number_Theory | 47 | 95.85
21252133
| 3443 |[Maximum Manhattan Distance After K Changes](src/main/java/g3401_3500/s3443_maximum_manhattan_distance_after_k_changes)| Medium | String, Hash_Table, Math, Counting | 50 | 96.94
21262134
| 3442 |[Maximum Difference Between Even and Odd Frequency I](src/main/java/g3401_3500/s3442_maximum_difference_between_even_and_odd_frequency_i)| Easy | String, Hash_Table, Counting | 1 | 100.00
2127-
| 3441 |[Minimum Cost Good Caption](src/main/java/g3401_3500/s3441_minimum_cost_good_caption)| Hard | String, Dynamic_Programming | 48 | 96.00
2135+
| 3441 |[Minimum Cost Good Caption](src/main/java/g3401_3500/s3441_minimum_cost_good_caption)| Hard | String, Dynamic_Programming | 20 | 100.00
21282136
| 3440 |[Reschedule Meetings for Maximum Free Time II](src/main/java/g3401_3500/s3440_reschedule_meetings_for_maximum_free_time_ii)| Medium | Array, Greedy, Enumeration | 3 | 100.00
21292137
| 3439 |[Reschedule Meetings for Maximum Free Time I](src/main/java/g3401_3500/s3439_reschedule_meetings_for_maximum_free_time_i)| Medium | Array, Greedy, Sliding_Window | 2 | 83.15
21302138
| 3438 |[Find Valid Pair of Adjacent Digits in String](src/main/java/g3401_3500/s3438_find_valid_pair_of_adjacent_digits_in_string)| Easy | String, Hash_Table, Counting | 1 | 100.00
@@ -2215,7 +2223,7 @@
22152223
| 3332 |[Maximum Points Tourist Can Earn](src/main/java/g3301_3400/s3332_maximum_points_tourist_can_earn)| Medium | Array, Dynamic_Programming, Matrix | 53 | 100.00
22162224
| 3331 |[Find Subtree Sizes After Changes](src/main/java/g3301_3400/s3331_find_subtree_sizes_after_changes)| Medium | Array, String, Hash_Table, Depth_First_Search, Tree | 166 | 52.73
22172225
| 3330 |[Find the Original Typed String I](src/main/java/g3301_3400/s3330_find_the_original_typed_string_i)| Easy | String | 1 | 96.13
2218-
| 3327 |[Check if DFS Strings Are Palindromes](src/main/java/g3301_3400/s3327_check_if_dfs_strings_are_palindromes)| Hard | Array, String, Hash_Table, Depth_First_Search, Tree, Hash_Function | 159 | 90.40
2226+
| 3327 |[Check if DFS Strings Are Palindromes](src/main/java/g3301_3400/s3327_check_if_dfs_strings_are_palindromes)| Hard | Array, String, Hash_Table, Depth_First_Search, Tree, Hash_Function | 70 | 100.00
22192227
| 3326 |[Minimum Division Operations to Make Array Non Decreasing](src/main/java/g3301_3400/s3326_minimum_division_operations_to_make_array_non_decreasing)| Medium | Array, Math, Greedy, Number_Theory | 20 | 97.34
22202228
| 3325 |[Count Substrings With K-Frequency Characters I](src/main/java/g3301_3400/s3325_count_substrings_with_k_frequency_characters_i)| Medium | String, Hash_Table, Sliding_Window | 1 | 100.00
22212229
| 3324 |[Find the Sequence of Strings Appeared on the Screen](src/main/java/g3301_3400/s3324_find_the_sequence_of_strings_appeared_on_the_screen)| Medium | String, Simulation | 6 | 92.04
@@ -2279,7 +2287,7 @@
22792287
| 3250 |[Find the Count of Monotonic Pairs I](src/main/java/g3201_3300/s3250_find_the_count_of_monotonic_pairs_i)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 3 | 100.00
22802288
| 3249 |[Count the Number of Good Nodes](src/main/java/g3201_3300/s3249_count_the_number_of_good_nodes)| Medium | Depth_First_Search, Tree | 34 | 100.00
22812289
| 3248 |[Snake in Matrix](src/main/java/g3201_3300/s3248_snake_in_matrix)| Easy | Array, String, Simulation | 2 | 98.05
2282-
| 3245 |[Alternating Groups III](src/main/java/g3201_3300/s3245_alternating_groups_iii)| Hard | Array, Binary_Indexed_Tree | 135 | 86.36
2290+
| 3245 |[Alternating Groups III](src/main/java/g3201_3300/s3245_alternating_groups_iii)| Hard | Array, Binary_Indexed_Tree | 38 | 91.84
22832291
| 3244 |[Shortest Distance After Road Addition Queries II](src/main/java/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii)| Hard | Array, Greedy, Graph, Ordered_Set | 5 | 97.43
22842292
| 3243 |[Shortest Distance After Road Addition Queries I](src/main/java/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i)| Medium | Array, Breadth_First_Search, Graph | 6 | 100.00
22852293
| 3242 |[Design Neighbor Sum Service](src/main/java/g3201_3300/s3242_design_neighbor_sum_service)| Easy | Array, Hash_Table, Matrix, Design, Simulation | 14 | 100.00
@@ -2344,7 +2352,7 @@
23442352
| 3164 |[Find the Number of Good Pairs II](src/main/java/g3101_3200/s3164_find_the_number_of_good_pairs_ii)| Medium | Array, Hash_Table | 407 | 75.28
23452353
| 3163 |[String Compression III](src/main/java/g3101_3200/s3163_string_compression_iii)| Medium | String | 17 | 88.10
23462354
| 3162 |[Find the Number of Good Pairs I](src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i)| Easy | Array, Hash_Table | 1 | 99.96
2347-
| 3161 |[Block Placement Queries](src/main/java/g3101_3200/s3161_block_placement_queries)| Hard | Array, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 137 | 99.38
2355+
| 3161 |[Block Placement Queries](src/main/java/g3101_3200/s3161_block_placement_queries)| Hard | Array, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 47 | 100.00
23482356
| 3160 |[Find the Number of Distinct Colors Among the Balls](src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls)| Medium | Array, Hash_Table, Simulation | 36 | 98.82
23492357
| 3159 |[Find Occurrences of an Element in an Array](src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array)| Medium | Array, Hash_Table | 4 | 96.74
23502358
| 3158 |[Find the XOR of Numbers Which Appear Twice](src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice)| Easy | Array, Hash_Table, Bit_Manipulation | 1 | 99.87

src/main/java/g3101_3200/s3161_block_placement_queries/readme.md

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -51,106 +51,111 @@ For query 0, place an obstacle at `x = 2`. A block of size at most 2 can be plac
5151
## Solution
5252

5353
```java
54-
import java.util.ArrayList;
54+
import java.util.Arrays;
5555
import java.util.List;
5656

5757
public class Solution {
58-
private static class Seg {
59-
private final int start;
60-
private final int end;
61-
private int min;
62-
private int max;
63-
private int len;
64-
private boolean obstacle;
65-
private Seg left;
66-
private Seg right;
67-
68-
public static Seg init(int n) {
69-
return new Seg(0, n);
70-
}
71-
72-
private Seg(int start, int end) {
73-
this.start = start;
74-
this.end = end;
75-
if (start >= end) {
76-
return;
58+
public List<Boolean> getResults(int[][] queries) {
59+
int m = queries.length;
60+
int[] pos = new int[m + 1];
61+
int size = 0;
62+
pos[size++] = 0;
63+
int max = 0;
64+
for (int[] q : queries) {
65+
max = Math.max(max, q[1]);
66+
if (q[0] == 1) {
67+
pos[size++] = q[1];
7768
}
78-
int mid = start + ((end - start) >> 1);
79-
left = new Seg(start, mid);
80-
right = new Seg(mid + 1, end);
81-
refresh();
8269
}
70+
Arrays.sort(pos, 0, size);
71+
max++;
72+
UnionFind left = new UnionFind(max + 1);
73+
UnionFind right = new UnionFind(max + 1);
74+
BIT bit = new BIT(max);
75+
initializePositions(size, pos, bit, left, right, max);
76+
return List.of(getBooleans(queries, m, size, left, right, bit));
77+
}
8378

84-
public void set(int i) {
85-
if (i < start || i > end) {
86-
return;
87-
} else if (i == start && i == end) {
88-
obstacle = true;
89-
min = max = start;
90-
return;
79+
private void initializePositions(
80+
int size, int[] pos, BIT bit, UnionFind left, UnionFind right, int max) {
81+
for (int i = 1; i < size; i++) {
82+
int pre = pos[i - 1];
83+
int cur = pos[i];
84+
bit.update(cur, cur - pre);
85+
for (int j = pre + 1; j < cur; j++) {
86+
left.parent[j] = pre;
87+
right.parent[j] = cur;
9188
}
92-
left.set(i);
93-
right.set(i);
94-
refresh();
9589
}
90+
for (int j = pos[size - 1] + 1; j < max; j++) {
91+
left.parent[j] = pos[size - 1];
92+
right.parent[j] = max;
93+
}
94+
}
9695

97-
private void refresh() {
98-
if (left.obstacle) {
99-
min = left.min;
100-
if (right.obstacle) {
101-
max = right.max;
102-
len = Math.max(right.min - left.max, Math.max(left.len, right.len));
103-
} else {
104-
max = left.max;
105-
len = Math.max(left.len, right.end - left.max);
106-
}
107-
obstacle = true;
108-
} else if (right.obstacle) {
109-
min = right.min;
110-
max = right.max;
111-
len = Math.max(right.len, right.min - left.start);
112-
obstacle = true;
96+
private Boolean[] getBooleans(
97+
int[][] queries, int m, int size, UnionFind left, UnionFind right, BIT bit) {
98+
Boolean[] ans = new Boolean[m - size + 1];
99+
int index = ans.length - 1;
100+
for (int i = m - 1; i >= 0; i--) {
101+
int[] q = queries[i];
102+
int x = q[1];
103+
int pre = left.find(x - 1);
104+
if (q[0] == 1) {
105+
int next = right.find(x + 1);
106+
left.parent[x] = pre;
107+
right.parent[x] = next;
108+
bit.update(next, next - pre);
113109
} else {
114-
len = end - start;
110+
int maxGap = Math.max(bit.query(pre), x - pre);
111+
ans[index--] = maxGap >= q[2];
115112
}
116113
}
114+
return ans;
115+
}
116+
117+
private static final class BIT {
118+
int n;
119+
int[] tree;
120+
121+
public BIT(int n) {
122+
this.n = n;
123+
tree = new int[n];
124+
}
117125

118-
public void max(int n, int[] t) {
119-
if (end <= n) {
120-
t[0] = Math.max(t[0], len);
121-
if (obstacle) {
122-
t[1] = max;
123-
}
124-
return;
126+
public void update(int i, int v) {
127+
while (i < n) {
128+
tree[i] = Math.max(tree[i], v);
129+
i += i & -i;
125130
}
126-
left.max(n, t);
127-
if (!right.obstacle || right.min >= n) {
128-
return;
131+
}
132+
133+
public int query(int i) {
134+
int result = 0;
135+
while (i > 0) {
136+
result = Math.max(result, tree[i]);
137+
i &= i - 1;
129138
}
130-
t[0] = Math.max(t[0], right.min - t[1]);
131-
right.max(n, t);
139+
return result;
132140
}
133141
}
134142

135-
public List<Boolean> getResults(int[][] queries) {
136-
int max = 0;
137-
for (int[] i : queries) {
138-
max = Math.max(max, i[1]);
143+
private static final class UnionFind {
144+
private final int[] parent;
145+
146+
public UnionFind(int n) {
147+
parent = new int[n];
148+
for (int i = 1; i < n; i++) {
149+
parent[i] = i;
150+
}
139151
}
140-
Seg root = Seg.init(max);
141-
root.set(0);
142152

143-
List<Boolean> res = new ArrayList<>(queries.length);
144-
for (int[] i : queries) {
145-
if (i[0] == 1) {
146-
root.set(i[1]);
147-
} else {
148-
int[] t = new int[2];
149-
root.max(i[1], t);
150-
res.add(Math.max(t[0], i[1] - t[1]) >= i[2]);
153+
public int find(int x) {
154+
if (parent[x] != x) {
155+
parent[x] = find(parent[x]);
151156
}
157+
return parent[x];
152158
}
153-
return res;
154159
}
155160
}
156161
```

0 commit comments

Comments
 (0)