Skip to content

Commit ed00724

Browse files
committed
Added tasks 3483-3490
1 parent 2d5cac3 commit ed00724

File tree

12 files changed

+1021
-312
lines changed
  • src/main/kotlin
    • g3101_3200/s3161_block_placement_queries
    • g3201_3300/s3245_alternating_groups_iii
    • g3301_3400/s3327_check_if_dfs_strings_are_palindromes
    • g3401_3500

12 files changed

+1021
-312
lines changed

README.md

Lines changed: 11 additions & 3 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/kotlin/g3401_3500/s3490_count_beautiful_numbers)| Hard | Dynamic_Programming | 246 | 100.00
2092+
| 3489 |[Zero Array Transformation IV](src/main/kotlin/g3401_3500/s3489_zero_array_transformation_iv)| Medium | Array, Dynamic_Programming | 104 | 100.00
2093+
| 3488 |[Closest Equal Element Queries](src/main/kotlin/g3401_3500/s3488_closest_equal_element_queries)| Medium | Array, Hash_Table, Binary_Search | 93 | 100.00
2094+
| 3487 |[Maximum Unique Subarray Sum After Deletion](src/main/kotlin/g3401_3500/s3487_maximum_unique_subarray_sum_after_deletion)| Easy | Array, Hash_Table, Greedy | 4 | 100.00
2095+
| 3486 |[Longest Special Path II](src/main/kotlin/g3401_3500/s3486_longest_special_path_ii)| Hard | Array, Hash_Table, Depth_First_Search, Tree, Prefix_Sum | 255 | 100.00
2096+
| 3485 |[Longest Common Prefix of K Strings After Removal](src/main/kotlin/g3401_3500/s3485_longest_common_prefix_of_k_strings_after_removal)| Hard | Array, String, Trie | 251 | 100.00
2097+
| 3484 |[Design Spreadsheet](src/main/kotlin/g3401_3500/s3484_design_spreadsheet)| Medium | Array, String, Hash_Table, Matrix, Design | 176 | 100.00
2098+
| 3483 |[Unique 3-Digit Even Numbers](src/main/kotlin/g3401_3500/s3483_unique_3_digit_even_numbers)| Easy | Array, Hash_Table, Recursion, Enumeration | 6 | 100.00
20912099
| 3482 |[Analyze Organization Hierarchy](src/main/kotlin/g3401_3500/s3482_analyze_organization_hierarchy)| Hard | Database | 712 | 100.00
20922100
| 3480 |[Maximize Subarrays After Removing One Conflicting Pair](src/main/kotlin/g3401_3500/s3480_maximize_subarrays_after_removing_one_conflicting_pair)| Hard | Array, Prefix_Sum, Enumeration, Segment_Tree | 48 | 100.00
20932101
| 3479 |[Fruits Into Baskets III](src/main/kotlin/g3401_3500/s3479_fruits_into_baskets_iii)| Medium | Array, Binary_Search, Ordered_Set, Segment_Tree | 53 | 92.86
@@ -2215,7 +2223,7 @@
22152223
| 3332 |[Maximum Points Tourist Can Earn](src/main/kotlin/g3301_3400/s3332_maximum_points_tourist_can_earn)| Medium | Array, Dynamic_Programming, Matrix | 216 | 100.00
22162224
| 3331 |[Find Subtree Sizes After Changes](src/main/kotlin/g3301_3400/s3331_find_subtree_sizes_after_changes)| Medium | Array, String, Hash_Table, Depth_First_Search, Tree | 139 | 95.24
22172225
| 3330 |[Find the Original Typed String I](src/main/kotlin/g3301_3400/s3330_find_the_original_typed_string_i)| Easy | String | 142 | 88.24
2218-
| 3327 |[Check if DFS Strings Are Palindromes](src/main/kotlin/g3301_3400/s3327_check_if_dfs_strings_are_palindromes)| Hard | Array, String, Hash_Table, Depth_First_Search, Tree, Hash_Function | 165 | 100.00
2226+
| 3327 |[Check if DFS Strings Are Palindromes](src/main/kotlin/g3301_3400/s3327_check_if_dfs_strings_are_palindromes)| Hard | Array, String, Hash_Table, Depth_First_Search, Tree, Hash_Function | 86 | 100.00
22192227
| 3326 |[Minimum Division Operations to Make Array Non Decreasing](src/main/kotlin/g3301_3400/s3326_minimum_division_operations_to_make_array_non_decreasing)| Medium | Array, Math, Greedy, Number_Theory | 24 | 94.12
22202228
| 3325 |[Count Substrings With K-Frequency Characters I](src/main/kotlin/g3301_3400/s3325_count_substrings_with_k_frequency_characters_i)| Medium | String, Hash_Table, Sliding_Window | 3 | 88.00
22212229
| 3324 |[Find the Sequence of Strings Appeared on the Screen](src/main/kotlin/g3301_3400/s3324_find_the_sequence_of_strings_appeared_on_the_screen)| Medium | String, Simulation | 8 | 100.00
@@ -2279,7 +2287,7 @@
22792287
| 3250 |[Find the Count of Monotonic Pairs I](src/main/kotlin/g3201_3300/s3250_find_the_count_of_monotonic_pairs_i)| Hard | Array, Dynamic_Programming, Math, Prefix_Sum, Combinatorics | 241 | 100.00
22802288
| 3249 |[Count the Number of Good Nodes](src/main/kotlin/g3201_3300/s3249_count_the_number_of_good_nodes)| Medium | Depth_First_Search, Tree | 1190 | 100.00
22812289
| 3248 |[Snake in Matrix](src/main/kotlin/g3201_3300/s3248_snake_in_matrix)| Easy | Array, String, Simulation | 174 | 90.91
2282-
| 3245 |[Alternating Groups III](src/main/kotlin/g3201_3300/s3245_alternating_groups_iii)| Hard | Array, Binary_Indexed_Tree | 188 | 100.00
2290+
| 3245 |[Alternating Groups III](src/main/kotlin/g3201_3300/s3245_alternating_groups_iii)| Hard | Array, Binary_Indexed_Tree | 70 | 100.00
22832291
| 3244 |[Shortest Distance After Road Addition Queries II](src/main/kotlin/g3201_3300/s3244_shortest_distance_after_road_addition_queries_ii)| Hard | Array, Greedy, Graph, Ordered_Set | 794 | 92.31
22842292
| 3243 |[Shortest Distance After Road Addition Queries I](src/main/kotlin/g3201_3300/s3243_shortest_distance_after_road_addition_queries_i)| Medium | Array, Breadth_First_Search, Graph | 313 | 97.06
22852293
| 3242 |[Design Neighbor Sum Service](src/main/kotlin/g3201_3300/s3242_design_neighbor_sum_service)| Easy | Array, Hash_Table, Matrix, Design, Simulation | 333 | 75.00
@@ -2344,7 +2352,7 @@
23442352
| 3164 |[Find the Number of Good Pairs II](src/main/kotlin/g3101_3200/s3164_find_the_number_of_good_pairs_ii)| Medium | Array, Hash_Table | 1175 | 90.00
23452353
| 3163 |[String Compression III](src/main/kotlin/g3101_3200/s3163_string_compression_iii)| Medium | String | 331 | 66.13
23462354
| 3162 |[Find the Number of Good Pairs I](src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i)| Easy | Array, Hash_Table | 182 | 54.41
2347-
| 3161 |[Block Placement Queries](src/main/kotlin/g3101_3200/s3161_block_placement_queries)| Hard | Array, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 1701 | 100.00
2355+
| 3161 |[Block Placement Queries](src/main/kotlin/g3101_3200/s3161_block_placement_queries)| Hard | Array, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 100 | 100.00
23482356
| 3160 |[Find the Number of Distinct Colors Among the Balls](src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls)| Medium | Array, Hash_Table, Simulation | 1055 | 58.82
23492357
| 3159 |[Find Occurrences of an Element in an Array](src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array)| Medium | Array, Hash_Table | 810 | 98.28
23502358
| 3158 |[Find the XOR of Numbers Which Appear Twice](src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice)| Easy | Array, Hash_Table, Bit_Manipulation | 166 | 92.21

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

Lines changed: 87 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -54,100 +54,114 @@ For query 0, place an obstacle at `x = 2`. A block of size at most 2 can be plac
5454
import kotlin.math.max
5555

5656
class Solution {
57-
private class Seg private constructor(private val start: Int, private val end: Int) {
58-
private var min = 0
59-
private var max = 0
60-
private var len = 0
61-
private var obstacle = false
62-
private lateinit var left: Seg
63-
private lateinit var right: Seg
64-
65-
init {
66-
if (start < end) {
67-
val mid = start + ((end - start) shr 1)
68-
left = Seg(start, mid)
69-
right = Seg(mid + 1, end)
70-
refresh()
57+
fun getResults(queries: Array<IntArray>): List<Boolean> {
58+
val m = queries.size
59+
val pos = IntArray(m + 1)
60+
var size = 0
61+
pos[size++] = 0
62+
var max = 0
63+
for (q in queries) {
64+
max = max(max.toDouble(), q[1].toDouble()).toInt()
65+
if (q[0] == 1) {
66+
pos[size++] = q[1]
7167
}
7268
}
69+
pos.sort(0, size)
70+
max++
71+
val left = UnionFind(max + 1)
72+
val right = UnionFind(max + 1)
73+
val bit = BIT(max)
74+
initializePositions(size, pos, bit, left, right, max)
75+
return listOf<Boolean>(*getBooleans(queries, m, size, left, right, bit))
76+
}
7377

74-
fun set(i: Int) {
75-
if (i < start || i > end) {
76-
return
77-
} else if (i == start && i == end) {
78-
obstacle = true
79-
max = start
80-
min = max
81-
return
78+
private fun initializePositions(
79+
size: Int,
80+
pos: IntArray,
81+
bit: BIT,
82+
left: UnionFind,
83+
right: UnionFind,
84+
max: Int,
85+
) {
86+
for (i in 1..<size) {
87+
val pre = pos[i - 1]
88+
val cur = pos[i]
89+
bit.update(cur, cur - pre)
90+
for (j in pre + 1..<cur) {
91+
left.parent[j] = pre
92+
right.parent[j] = cur
8293
}
83-
left.set(i)
84-
right.set(i)
85-
refresh()
8694
}
95+
for (j in pos[size - 1] + 1..<max) {
96+
left.parent[j] = pos[size - 1]
97+
right.parent[j] = max
98+
}
99+
}
87100

88-
private fun refresh() {
89-
if (left.obstacle) {
90-
min = left.min
91-
if (right.obstacle) {
92-
max = right.max
93-
len = max((right.min - left.max), max(left.len, right.len))
94-
} else {
95-
max = left.max
96-
len = max(left.len, (right.end - left.max))
97-
}
98-
obstacle = true
99-
} else if (right.obstacle) {
100-
min = right.min
101-
max = right.max
102-
len = max(right.len, (right.min - left.start))
103-
obstacle = true
101+
private fun getBooleans(
102+
queries: Array<IntArray>,
103+
m: Int,
104+
size: Int,
105+
left: UnionFind,
106+
right: UnionFind,
107+
bit: BIT,
108+
): Array<Boolean> {
109+
val ans = Array<Boolean>(m - size + 1) { false }
110+
var index = ans.size - 1
111+
for (i in m - 1 downTo 0) {
112+
val q = queries[i]
113+
val x = q[1]
114+
val pre = left.find(x - 1)
115+
if (q[0] == 1) {
116+
val next = right.find(x + 1)
117+
left.parent[x] = pre
118+
right.parent[x] = next
119+
bit.update(next, next - pre)
104120
} else {
105-
len = end - start
121+
val maxGap = max(bit.query(pre).toDouble(), (x - pre).toDouble()).toInt()
122+
ans[index--] = maxGap >= q[2]
106123
}
107124
}
125+
return ans
126+
}
108127

109-
fun max(n: Int, t: IntArray) {
110-
if (end <= n) {
111-
t[0] = max(t[0], len)
112-
if (obstacle) {
113-
t[1] = max
114-
}
115-
return
116-
}
117-
left.max(n, t)
118-
if (!right.obstacle || right.min >= n) {
119-
return
128+
private class BIT(var n: Int) {
129+
var tree: IntArray = IntArray(n)
130+
131+
fun update(i: Int, v: Int) {
132+
var i = i
133+
while (i < n) {
134+
tree[i] = max(tree[i].toDouble(), v.toDouble()).toInt()
135+
i += i and -i
120136
}
121-
t[0] = max(t[0], (right.min - t[1]))
122-
right.max(n, t)
123137
}
124138

125-
companion object {
126-
fun init(n: Int): Seg {
127-
return Seg(0, n)
139+
fun query(i: Int): Int {
140+
var i = i
141+
var result = 0
142+
while (i > 0) {
143+
result = max(result.toDouble(), tree[i].toDouble()).toInt()
144+
i = i and i - 1
128145
}
146+
return result
129147
}
130148
}
131149

132-
fun getResults(queries: Array<IntArray>): List<Boolean> {
133-
var max = 0
134-
for (i in queries) {
135-
max = max(max, i[1])
150+
private class UnionFind(n: Int) {
151+
val parent: IntArray = IntArray(n)
152+
153+
init {
154+
for (i in 1..<n) {
155+
parent[i] = i
156+
}
136157
}
137-
val root = Seg.init(max)
138-
root.set(0)
139158

140-
val res: MutableList<Boolean> = ArrayList(queries.size)
141-
for (i in queries) {
142-
if (i[0] == 1) {
143-
root.set(i[1])
144-
} else {
145-
val t = IntArray(2)
146-
root.max(i[1], t)
147-
res.add(max(t[0], (i[1] - t[1])) >= i[2])
159+
fun find(x: Int): Int {
160+
if (parent[x] != x) {
161+
parent[x] = find(parent[x])
148162
}
163+
return parent[x]
149164
}
150-
return res
151165
}
152166
}
153167
```

0 commit comments

Comments
 (0)