Skip to content

Commit a83fab1

Browse files
committed
Improved task 3389
1 parent 18ad0e9 commit a83fab1

File tree

1 file changed

+14
-10
lines changed
  • src/main/kotlin/g3301_3400/s3389_minimum_operations_to_make_character_frequencies_equal

1 file changed

+14
-10
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package g3301_3400.s3389_minimum_operations_to_make_character_frequencies_equal
33
// #Hard #String #Hash_Table #Dynamic_Programming #Counting #Enumeration
44
// #2024_12_18_Time_9_ms_(78.95%)_Space_39.3_MB_(18.42%)
55

6+
import kotlin.math.abs
7+
import kotlin.math.max
8+
import kotlin.math.min
9+
610
class Solution {
711
fun makeStringGood(s: String): Int {
812
val n = s.length
@@ -12,8 +16,8 @@ class Solution {
1216
var mx = 0
1317
for (c in cnt)
1418
if (c != 0) {
15-
mn = Math.min(mn, c)
16-
mx = Math.max(mx, c)
19+
mn = min(mn, c)
20+
mx = max(mx, c)
1721
}
1822
if (mn == mx) return 0
1923
var dp0 = 0
@@ -23,21 +27,21 @@ class Solution {
2327
var ans = n - 1
2428
for (i in mn..mx) {
2529
dp0 = cnt[0]
26-
dp1 = Math.abs(i - cnt[0])
30+
dp1 = abs(i - cnt[0])
2731
for (j in 1 until 26) {
2832
tmp0 = dp0
2933
tmp1 = dp1
30-
dp0 = Math.min(tmp0, tmp1) + cnt[j]
31-
if (cnt[j] >= i) {
32-
dp1 = Math.min(tmp0, tmp1) + (cnt[j] - i)
34+
dp0 = min(tmp0, tmp1) + cnt[j]
35+
dp1 = if (cnt[j] >= i) {
36+
min(tmp0, tmp1) + (cnt[j] - i)
3337
} 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)),
38+
min(
39+
tmp0 + i - min(i, cnt[j] + cnt[j - 1]),
40+
tmp1 + i - min(i, cnt[j] + max(0, cnt[j - 1] - i)),
3741
)
3842
}
3943
}
40-
ans = Math.min(ans, minOf(dp0, dp1))
44+
ans = min(ans, minOf(dp0, dp1))
4145
}
4246
return ans
4347
}

0 commit comments

Comments
 (0)