Skip to content

Commit 18ad0e9

Browse files
committed
Improved task 3389
1 parent 9767f39 commit 18ad0e9

File tree

4 files changed

+34
-34
lines changed
  • src/main/kotlin/g3301_3400
    • s3386_button_with_longest_push_time
    • s3387_maximize_amount_after_two_days_of_conversions
    • s3388_count_beautiful_splits_in_an_array
    • s3389_minimum_operations_to_make_character_frequencies_equal

4 files changed

+34
-34
lines changed

src/main/kotlin/g3301_3400/s3386_button_with_longest_push_time/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3301_3400.s3386_button_with_longest_push_time
22

3-
// #Easy #Array #2024_12_15_Time_1_ms_(100.00%)_Space_40.6_MB_(100.00%)
3+
// #Easy #Array #2024_12_18_Time_1_ms_(100.00%)_Space_41.1_MB_(91.89%)
44

55
import kotlin.math.min
66

src/main/kotlin/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g3301_3400.s3387_maximize_amount_after_two_days_of_conversions
22

33
// #Medium #Array #String #Depth_First_Search #Breadth_First_Search #Graph
4-
// #2024_12_15_Time_13_ms_(100.00%)_Space_43.7_MB_(100.00%)
4+
// #2024_12_18_Time_13_ms_(88.46%)_Space_47.8_MB_(30.77%)
55

66
import kotlin.math.max
77

src/main/kotlin/g3301_3400/s3388_count_beautiful_splits_in_an_array/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3301_3400.s3388_count_beautiful_splits_in_an_array
22

3-
// #Medium #Array #Dynamic_Programming #2024_12_15_Time_162_ms_(100.00%)_Space_235.2_MB_(100.00%)
3+
// #Medium #Array #Dynamic_Programming #2024_12_18_Time_155_ms_(100.00%)_Space_227.9_MB_(26.67%)
44

55
import kotlin.math.min
66

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
package g3301_3400.s3389_minimum_operations_to_make_character_frequencies_equal
22

33
// #Hard #String #Hash_Table #Dynamic_Programming #Counting #Enumeration
4-
// #2024_12_15_Time_88_ms_(100.00%)_Space_40.7_MB_(100.00%)
5-
6-
import kotlin.math.max
7-
import kotlin.math.min
4+
// #2024_12_18_Time_9_ms_(78.95%)_Space_39.3_MB_(18.42%)
85

96
class Solution {
107
fun makeStringGood(s: String): Int {
118
val n = s.length
12-
val a = IntArray(110)
13-
for (ch in s.toCharArray()) {
14-
a[ch.code - 'a'.code + 1]++
15-
}
16-
val f: Array<IntArray> = Array<IntArray>(30) { IntArray(3) }
17-
val inf = Int.Companion.MAX_VALUE / 2
18-
var ans = inf
19-
for (avg in 1..n) {
20-
for (row in f) {
21-
row.fill(inf)
9+
val cnt = IntArray(26)
10+
for (c in s) cnt[c - 'a']++
11+
var mn = n
12+
var mx = 0
13+
for (c in cnt)
14+
if (c != 0) {
15+
mn = Math.min(mn, c)
16+
mx = Math.max(mx, c)
2217
}
23-
f[0][0] = 0
24-
for (i in 1..26) {
25-
f[i][0] = min(min(f[i - 1][0], f[i - 1][1]), f[i - 1][2]) + a[i]
26-
if (a[i] <= avg) {
27-
f[i][1] = min(
28-
min(
29-
(f[i - 1][0] + max(avg - a[i - 1] - a[i], 0)),
30-
(f[i - 1][1] + (avg - a[i])),
31-
),
32-
(f[i - 1][2] + max(avg - (a[i - 1] - avg) - a[i], 0)),
33-
).toInt()
34-
}
35-
if (a[i] >= avg) {
36-
f[i][2] =
37-
min(min(f[i - 1][0], f[i - 1][1]), f[i - 1][2]) + (a[i] - avg)
18+
if (mn == mx) return 0
19+
var dp0 = 0
20+
var dp1 = 0
21+
var tmp0 = 0
22+
var tmp1 = 0
23+
var ans = n - 1
24+
for (i in mn..mx) {
25+
dp0 = cnt[0]
26+
dp1 = Math.abs(i - cnt[0])
27+
for (j in 1 until 26) {
28+
tmp0 = dp0
29+
tmp1 = dp1
30+
dp0 = Math.min(tmp0, tmp1) + cnt[j]
31+
if (cnt[j] >= i) {
32+
dp1 = Math.min(tmp0, tmp1) + (cnt[j] - i)
33+
} else {
34+
dp1 = Math.min(
35+
tmp0 + i - Math.min(i, cnt[j] + cnt[j - 1]),
36+
tmp1 + i - Math.min(i, cnt[j] + Math.max(0, cnt[j - 1] - i)),
37+
)
3838
}
3939
}
40-
ans = min(ans, min(f[26][0], min(f[26][1], f[26][2])))
40+
ans = Math.min(ans, minOf(dp0, dp1))
4141
}
42-
return if (ans == inf) -1 else ans
42+
return ans
4343
}
4444
}

0 commit comments

Comments
 (0)