Skip to content

Commit 0733051

Browse files
committed
Added tasks 3407-3435
1 parent ed25757 commit 0733051

File tree

28 files changed

+3250
-581
lines changed
  • src/main/kotlin
    • 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
      • s3417_zigzag_grid_traversal_with_skip
      • s3418_maximum_amount_of_money_robot_can_earn
      • s3419_minimize_the_maximum_edge_weight_of_graph
      • s3420_count_non_decreasing_subarrays_after_k_operations
      • s3421_find_students_who_improved
      • s3423_maximum_difference_between_adjacent_elements_in_a_circular_array
      • s3424_minimum_cost_to_make_arrays_identical
      • s3425_longest_special_path
      • s3426_manhattan_distances_of_all_arrangements_of_pieces
      • s3427_sum_of_variable_length_subarrays
      • s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences
      • s3429_paint_house_iv
      • s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays
      • s3432_count_partitions_with_even_sum_difference
      • s3433_count_mentions_per_user
      • s3434_maximum_frequency_after_subarray_operation
      • s3435_frequencies_of_shortest_supersequences

28 files changed

+3250
-581
lines changed

README.md

Lines changed: 664 additions & 371 deletions
Large diffs are not rendered by default.

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

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,39 @@ import kotlin.math.max
6666

6767
@Suppress("unused")
6868
class Solution {
69-
fun checkValidCuts(n: Int, rectangles: Array<IntArray>): Boolean {
70-
val m = rectangles.size
71-
val xAxis = Array<IntArray>(m) { IntArray(2) }
72-
val yAxis = Array<IntArray>(m) { IntArray(2) }
73-
var ind = 0
74-
for (axis in rectangles) {
75-
val startX = axis[0]
76-
val startY = axis[1]
77-
val endX = axis[2]
78-
val endY = axis[3]
79-
xAxis[ind] = intArrayOf(startX, endX)
80-
yAxis[ind] = intArrayOf(startY, endY)
81-
ind++
69+
fun checkValidCuts(m: Int, rectangles: Array<IntArray>): Boolean {
70+
val n = rectangles.size
71+
val start = LongArray(n)
72+
for (i in 0..<n) {
73+
start[i] = (rectangles[i][1].toLong() shl 32) + rectangles[i][3]
8274
}
83-
84-
xAxis.sortWith<IntArray>(
85-
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
86-
)
87-
88-
yAxis.sortWith<IntArray>(
89-
Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] },
90-
)
91-
val verticalCuts = findSections(xAxis)
92-
if (verticalCuts > 2) {
75+
start.sort()
76+
if (validate(start)) {
9377
return true
9478
}
95-
val horizontalCuts = findSections(yAxis)
96-
return horizontalCuts > 2
79+
for (i in 0..<n) {
80+
start[i] = (rectangles[i][0].toLong() shl 32) + rectangles[i][2]
81+
}
82+
start.sort()
83+
return validate(start)
9784
}
9885

99-
private fun findSections(axis: Array<IntArray>): Int {
100-
var end = axis[0][1]
101-
var sections = 1
102-
for (i in 1..<axis.size) {
103-
if (end > axis[i][0]) {
104-
end = max(end, axis[i][1])
105-
} else {
106-
sections++
107-
end = axis[i][1]
108-
}
109-
if (sections > 2) {
110-
return sections
86+
private fun validate(arr: LongArray): Boolean {
87+
var cut = 0
88+
val n = arr.size
89+
var max = arr[0].toInt() and MASK
90+
for (i in 0..<n) {
91+
val start = (arr[i] shr 32).toInt()
92+
if (start >= max && ++cut == 2) {
93+
return true
11194
}
95+
max = max(max.toDouble(), (arr[i] and MASK.toLong()).toInt().toDouble()).toInt()
11296
}
113-
return sections
97+
return false
98+
}
99+
100+
companion object {
101+
private val MASK = (1 shl 30) - 1
114102
}
115103
}
116104
```

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

Lines changed: 56 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -56,189 +56,73 @@ There is no subsequence of length 5 with a unique middle mode.
5656

5757
```kotlin
5858
class Solution {
59-
fun subsequencesWithMiddleMode(a: IntArray): Int {
60-
val n = a.size
61-
// Create a dictionary to store indices of each number
62-
val dict: MutableMap<Int, MutableList<Int>> = HashMap()
63-
for (i in 0..<n) {
64-
dict.computeIfAbsent(a[i]) { _: Int -> ArrayList<Int>() }.add(i)
65-
}
66-
var ans = 0L
67-
// Iterate over each unique number and its indices
68-
for (entry in dict.entries) {
69-
val b: MutableList<Int> = entry.value
70-
val m = b.size
71-
for (k in 0..<m) {
72-
val i: Int = b[k]
73-
val r = m - 1 - k
74-
val u = i - k
75-
val v = (n - 1 - i) - r
76-
// Case 2: Frequency of occurrence is 2 times
77-
ans = (
78-
ans + convert(k, 1) * convert(u, 1) % MOD * convert(
79-
v,
80-
2,
81-
) % MOD
82-
) % MOD
83-
ans = (
84-
ans + convert(r, 1) * convert(u, 2) % MOD * convert(
85-
v,
86-
1,
87-
) % MOD
88-
) % MOD
89-
// Case 3: Frequency of occurrence is 3 times
90-
ans = (ans + convert(k, 2) * convert(v, 2) % MOD) % MOD
91-
ans = (ans + convert(r, 2) * convert(u, 2) % MOD) % MOD
92-
ans =
93-
(
94-
(
95-
ans +
96-
convert(k, 1) *
97-
convert(r, 1) %
98-
MOD
99-
* convert(u, 1) %
100-
MOD
101-
* convert(v, 1) %
102-
MOD
103-
) %
104-
MOD
105-
)
106-
// Case 4: Frequency of occurrence is 4 times
107-
ans = (
108-
ans + convert(k, 2) * convert(r, 1) % MOD * convert(
109-
v,
110-
1,
111-
) % MOD
112-
) % MOD
113-
ans = (
114-
ans + convert(k, 1) * convert(r, 2) % MOD * convert(
115-
u,
116-
1,
117-
) % MOD
118-
) % MOD
119-
120-
// Case 5: Frequency of occurrence is 5 times
121-
ans = (ans + convert(k, 2) * convert(r, 2) % MOD) % MOD
59+
private val c2 = LongArray(1001)
60+
61+
fun subsequencesWithMiddleMode(nums: IntArray): Int {
62+
if (c2[2] == 0L) {
63+
c2[1] = 0
64+
c2[0] = c2[1]
65+
c2[2] = 1
66+
for (i in 3..<c2.size) {
67+
c2[i] = (i * (i - 1) / 2).toLong()
12268
}
12369
}
124-
var dif: Long = 0
125-
// Principle of inclusion-exclusion
126-
for (midEntry in dict.entries) {
127-
val b: MutableList<Int> = midEntry.value
128-
val m = b.size
129-
for (tmpEntry in dict.entries) {
130-
if (midEntry.key != tmpEntry.key) {
131-
val c: MutableList<Int> = tmpEntry.value
132-
val size = c.size
133-
var k = 0
134-
var j = 0
135-
while (k < m) {
136-
val i: Int = b[k]
137-
val r = m - 1 - k
138-
val u = i - k
139-
val v = (n - 1 - i) - r
140-
while (j < size && c[j] < i) {
141-
j++
142-
}
143-
val x = j
144-
val y = size - x
145-
dif =
146-
(
147-
(
148-
dif +
149-
convert(k, 1) *
150-
convert(x, 1) %
151-
MOD
152-
* convert(y, 1) %
153-
MOD
154-
* convert(v - y, 1) %
155-
MOD
156-
) %
157-
MOD
158-
)
159-
dif =
160-
(
161-
(
162-
dif +
163-
convert(k, 1) *
164-
convert(y, 2) %
165-
MOD
166-
* convert(u - x, 1) %
167-
MOD
168-
) %
169-
MOD
170-
)
171-
dif =
172-
(
173-
(
174-
dif + convert(k, 1) * convert(x, 1) % MOD * convert(
175-
y,
176-
2,
177-
) % MOD
178-
) %
179-
MOD
180-
)
181-
182-
dif =
183-
(
184-
(
185-
dif +
186-
convert(r, 1) *
187-
convert(x, 1) %
188-
MOD
189-
* convert(y, 1) %
190-
MOD
191-
* convert(u - x, 1) %
192-
MOD
193-
) %
194-
MOD
195-
)
196-
dif =
197-
(
198-
(
199-
dif +
200-
convert(r, 1) *
201-
convert(x, 2) %
202-
MOD
203-
* convert(v - y, 1) %
204-
MOD
205-
) %
206-
MOD
207-
)
208-
dif =
209-
(
210-
(
211-
dif + convert(r, 1) * convert(x, 2) % MOD * convert(
212-
y,
213-
1,
214-
) % MOD
215-
) %
216-
MOD
217-
)
218-
k++
219-
}
220-
}
70+
val n = nums.size
71+
val newNums = IntArray(n)
72+
val map: MutableMap<Int?, Int?> = HashMap<Int?, Int?>(n)
73+
var m = 0
74+
var index = 0
75+
for (x in nums) {
76+
var id = map[x]
77+
if (id == null) {
78+
id = m++
79+
map.put(x, id)
22180
}
81+
newNums[index++] = id
22282
}
223-
return ((ans - dif + MOD) % MOD).toInt()
224-
}
225-
226-
private fun convert(n: Int, k: Int): Long {
227-
if (k > n) {
83+
if (m == n) {
22884
return 0
22985
}
230-
if (k == 0 || k == n) {
231-
return 1
86+
val rightCount = IntArray(m)
87+
for (x in newNums) {
88+
rightCount[x]++
23289
}
233-
var res: Long = 1
234-
for (i in 0..<k) {
235-
res = res * (n - i) / (i + 1)
90+
val leftCount = IntArray(m)
91+
var ans = n.toLong() * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120
92+
for (left in 0..<n - 2) {
93+
val x = newNums[left]
94+
rightCount[x]--
95+
if (left >= 2) {
96+
val right = n - (left + 1)
97+
val leftX = leftCount[x]
98+
val rightX = rightCount[x]
99+
ans -= c2[left - leftX] * c2[right - rightX]
100+
for (y in 0..<m) {
101+
if (y == x) {
102+
continue
103+
}
104+
val rightY = rightCount[y]
105+
val leftY = leftCount[y]
106+
ans -= c2[leftY] * rightX * (right - rightX)
107+
ans -= c2[rightY] * leftX * (left - leftX)
108+
ans -=
109+
(
110+
leftY
111+
* rightY
112+
* (
113+
leftX * (right - rightX - rightY) +
114+
rightX * (left - leftX - leftY)
115+
)
116+
).toLong()
117+
}
118+
}
119+
leftCount[x]++
236120
}
237-
return res % MOD
121+
return (ans % MOD).toInt()
238122
}
239123

240124
companion object {
241-
private const val MOD = 1000000007
125+
private val MOD = 1e9.toInt() + 7
242126
}
243127
}
244128
```

0 commit comments

Comments
 (0)