Skip to content

Commit 8557a2d

Browse files
committed
Improved tasks
1 parent 6ebf2ee commit 8557a2d

File tree

9 files changed

+140
-110
lines changed

9 files changed

+140
-110
lines changed

src/main/kotlin/g3601_3700/s3663_find_the_least_frequent_digit/Solution.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
package g3601_3700.s3663_find_the_least_frequent_digit
22

3-
// #Easy #Biweekly_Contest_164 #2025_08_31_Time_3_ms_(100.00%)_Space_41.13_MB_(100.00%)
4-
5-
import kotlin.math.min
3+
// #Easy #Biweekly_Contest_164 #2025_09_07_Time_1_ms_(96.30%)_Space_40.60_MB_(100.00%)
64

75
class Solution {
86
fun getLeastFrequentDigit(n: Int): Int {
9-
val s = n.toString()
10-
val k = s.length
11-
val freq: MutableMap<Int, Int> = HashMap<Int, Int>()
12-
for (i in 0..<k) {
13-
val digit = s[i].code - '0'.code
14-
freq.put(digit, freq.getOrDefault(digit, 0) + 1)
15-
}
16-
var minfreq = Int.Companion.MAX_VALUE
17-
for (it in freq.entries) {
18-
minfreq = min(minfreq, it.value)
7+
val freq = IntArray(10)
8+
val numStr = n.toString()
9+
for (c in numStr.toCharArray()) {
10+
freq[c.code - '0'.code]++
1911
}
20-
var result = 10
21-
for (it in freq.entries) {
22-
if (it.value == minfreq) {
23-
result = min(result, it.key)
12+
var minFreq = Int.Companion.MAX_VALUE
13+
var result = -1
14+
for (d in 0..9) {
15+
if (freq[d] == 0) {
16+
continue
17+
}
18+
if (freq[d] < minFreq) {
19+
minFreq = freq[d]
20+
result = d
21+
} else if (freq[d] == minFreq && d < result) {
22+
result = d
2423
}
2524
}
2625
return result

src/main/kotlin/g3601_3700/s3664_two_letter_card_game/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3601_3700.s3664_two_letter_card_game
22

3-
// #Medium #Biweekly_Contest_164 #2025_08_31_Time_11_ms_(100.00%)_Space_82.34_MB_(_%)
3+
// #Medium #Biweekly_Contest_164 #2025_09_07_Time_11_ms_(100.00%)_Space_69.41_MB_(100.00%)
44

55
import kotlin.math.min
66

@@ -12,8 +12,8 @@ class Solution {
1212
val right = IntArray(10)
1313
var xx = 0
1414
for (c in cards) {
15-
val a = c[0]
16-
val b = c[1]
15+
val a = c.get(0)
16+
val b = c.get(1)
1717
if (a == x && b == x) {
1818
xx++
1919
} else if (a == x) {
Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
package g3601_3700.s3665_twisted_mirror_path_count
22

3-
// #Medium #Biweekly_Contest_164 #2025_08_31_Time_114_ms_(100.00%)_Space_120.02_MB_(100.00%)
3+
// #Medium #Biweekly_Contest_164 #2025_09_07_Time_33_ms_(100.00%)_Space_113.52_MB_(72.73%)
44

55
class Solution {
66
fun uniquePaths(grid: Array<IntArray>): Int {
7+
// 0 right, 1 down
78
val n = grid.size
89
val m = grid[0].size
9-
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(m) { IntArray(2) } }
10-
for (i in 0..<n) {
11-
for (j in 0..<m) {
12-
dp[i][j].fill(-1)
10+
val mod = 1000000007
11+
var dp = IntArray(m)
12+
dp[0] = 1
13+
for (j in 1..<m) {
14+
if (grid[0][j - 1] == 0) {
15+
dp[j] = dp[j - 1]
1316
}
1417
}
15-
return f(0, 0, 0, grid, n, m, dp)
16-
}
17-
18-
private fun f(i: Int, j: Int, dir: Int, grid: Array<IntArray>, n: Int, m: Int, dp: Array<Array<IntArray>>): Int {
19-
if (i == n - 1 && j == m - 1) {
20-
return 1
21-
}
22-
if (i >= n || j >= m) {
23-
return 0
24-
}
25-
if (dp[i][j][dir] != -1) {
26-
return dp[i][j][dir]
27-
}
28-
var ways: Long = 0
29-
if (grid[i][j] == 1) {
30-
ways = if (dir == 0) {
31-
f(i + 1, j, 1, grid, n, m, dp).toLong()
32-
} else {
33-
f(i, j + 1, 0, grid, n, m, dp).toLong()
18+
for (i in 1..<n) {
19+
val next = IntArray(m)
20+
if (grid[i - 1][0] == 0 && grid[i][0] == 0) {
21+
next[0] = dp[0]
22+
}
23+
for (j in 1..<m) {
24+
if (grid[i][j] == 0) {
25+
next[j] = (next[j] + dp[j]) % mod
26+
}
27+
if (grid[i][j - 1] == 0) {
28+
next[j] = (next[j] + next[j - 1]) % mod
29+
} else {
30+
next[j] = (next[j] + dp[j - 1]) % mod
31+
}
3432
}
35-
} else {
36-
ways += f(i + 1, j, 1, grid, n, m, dp).toLong()
37-
ways += f(i, j + 1, 0, grid, n, m, dp).toLong()
33+
dp = next
3834
}
39-
dp[i][j][dir] = ways.toInt() % MOD
40-
return dp[i][j][dir]
41-
}
42-
43-
companion object {
44-
private const val MOD = 1000000007
35+
return dp[m - 1]
4536
}
4637
}
Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,46 @@
11
package g3601_3700.s3666_minimum_operations_to_equalize_binary_string
22

3-
// #Hard #Biweekly_Contest_164 #2025_08_31_Time_29_ms_(100.00%)_Space_48.47_MB_(100.00%)
3+
// #Hard #Biweekly_Contest_164 #2025_09_07_Time_8_ms_(100.00%)_Space_46.70_MB_(100.00%)
44

5-
import java.util.ArrayDeque
6-
import java.util.Queue
75
import kotlin.math.max
8-
import kotlin.math.min
96

107
class Solution {
118
fun minOperations(s: String, k: Int): Int {
12-
val zeros = s.chars().map { x: Int -> if (x == '0'.code) 1 else 0 }.sum()
13-
if ((zeros % k) == 0) {
14-
return zeros / k
15-
}
169
val n = s.length
17-
val q: Queue<Int> = ArrayDeque<Int>()
18-
q.add(zeros)
19-
var res = 1
20-
// use bounds for optimization
21-
val bounds = Array<IntArray>(2) { IntArray(2) }
22-
bounds[zeros and 1][1] = zeros
23-
bounds[zeros and 1][0] = bounds[zeros and 1][1]
24-
bounds[1 - (zeros and 1)][0] = Int.Companion.MAX_VALUE
25-
bounds[1 - (zeros and 1)][1] = Int.Companion.MIN_VALUE
26-
while (q.isNotEmpty()) {
27-
// find min number of zeros and max number of zeros in this round
28-
var minv = Int.Companion.MAX_VALUE
29-
var maxv = Int.Companion.MIN_VALUE
30-
for (len in q.size downTo 1) {
31-
val h: Int = q.poll()
32-
val t = n - h
33-
var x = min(h, k)
34-
if (t >= k - x) {
35-
val fst = h - x + (k - x)
36-
minv = min(minv, fst)
37-
maxv = max(maxv, fst)
38-
}
39-
x = min(t, k)
40-
if (h >= k - x) {
41-
val snd = h - (k - x) + x
42-
minv = min(minv, snd)
43-
maxv = max(maxv, snd)
44-
}
10+
var cnt0 = 0
11+
for (c in s.toCharArray()) {
12+
if (c == '0') {
13+
cnt0++
14+
}
15+
}
16+
if (cnt0 == 0) {
17+
return 0
18+
}
19+
if (k == n) {
20+
return if (cnt0 == n) 1 else -1
21+
}
22+
val kP = k and 1
23+
val needP = cnt0 and 1
24+
var best = Long.Companion.MAX_VALUE
25+
for (p in 0..1) {
26+
if ((p * kP) % 2 != needP) {
27+
continue
28+
}
29+
val mismatch = (if (p == 0) cnt0 else (n - cnt0)).toLong()
30+
val b1 = (cnt0 + k - 1L) / k
31+
val b2: Long
32+
b2 = (mismatch + (n - k) - 1L) / (n - k)
33+
var lb = max(b1, b2)
34+
if (lb < 1) {
35+
lb = 1
36+
}
37+
if ((lb and 1L) != p.toLong()) {
38+
lb++
4539
}
46-
// possible children are sequence of equal difference 2
47-
val ind = minv and 1
48-
var temp = minv
49-
while (temp <= maxv) {
50-
if ((temp % k) == 0) {
51-
return res + temp / k
52-
}
53-
if (temp < bounds[ind][0] || temp > bounds[ind][1]) {
54-
q.add(temp)
55-
temp += 2
56-
} else {
57-
temp = bounds[ind][1] + 2
58-
}
40+
if (lb < best) {
41+
best = lb
5942
}
60-
bounds[ind][0] = min(bounds[ind][0], minv)
61-
bounds[ind][1] = max(bounds[ind][1], maxv)
62-
res++
6343
}
64-
return -1
44+
return if (best == Long.Companion.MAX_VALUE) -1 else best.toInt()
6545
}
6646
}

src/main/kotlin/g3601_3700/s3668_restore_finishing_order/Solution.kt

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

3-
// #Easy #Weekly_Contest_465 #2025_08_31_Time_2_ms_(100.00%)_Space_49.00_MB_(93.75%)
3+
// #Easy #Weekly_Contest_465 #2025_09_07_Time_2_ms_(94.29%)_Space_49.08_MB_(72.86%)
44

55
class Solution {
66
fun recoverOrder(order: IntArray, friends: IntArray): IntArray {

src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition/Solution.kt

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

3-
// #Medium #Weekly_Contest_465 #2025_08_31_Time_33_ms_(100.00%)_Space_58.00_MB_(40.00%)
3+
// #Medium #Weekly_Contest_465 #2025_09_07_Time_30_ms_(85.71%)_Space_56.41_MB_(28.57%)
44

55
import kotlin.math.max
66
import kotlin.math.min

src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits/Solution.kt

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

3-
// #Medium #Weekly_Contest_465 #2025_08_31_Time_103_ms_(100.00%)_Space_82.76_MB_(33.33%)
3+
// #Medium #Weekly_Contest_465 #2025_09_07_Time_113_ms_(88.89%)_Space_78.00_MB_(100.00%)
44

55
class Solution {
66
fun maxProduct(nums: IntArray): Long {

src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences/Solution.kt

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

3-
// #Hard #Weekly_Contest_465 #2025_08_31_Time_270_ms_(100.00%)_Space_79.84_MB_(100.00%)
3+
// #Hard #Weekly_Contest_465 #2025_09_07_Time_225_ms_(100.00%)_Space_75.96_MB_(100.00%)
44

55
import kotlin.math.sqrt
66

src/test/kotlin/g3601_3700/s3666_minimum_operations_to_equalize_binary_string/SolutionTest.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,64 @@ internal class SolutionTest {
1919
fun minOperations3() {
2020
assertThat<Int>(Solution().minOperations("101", 2), equalTo<Int>(-1))
2121
}
22+
23+
@Test
24+
fun minOperations4() {
25+
val k = 3
26+
assertThat<Int>(Solution().minOperations("111111", k), equalTo<Int>(0))
27+
}
28+
29+
@Test
30+
fun minOperations5() {
31+
val k = 6
32+
assertThat<Int>(Solution().minOperations("000000", k), equalTo<Int>(1))
33+
}
34+
35+
@Test
36+
fun minOperations6() {
37+
val k = 6
38+
assertThat<Int>(Solution().minOperations("000111", k), equalTo<Int>(-1))
39+
}
40+
41+
@Test
42+
fun minOperations7() {
43+
val k = 3
44+
assertThat<Int>(Solution().minOperations("0011", k), equalTo<Int>(2))
45+
}
46+
47+
@Test
48+
fun minOperations8() {
49+
val k = 4
50+
assertThat<Int>(Solution().minOperations("000011", k), equalTo<Int>(1))
51+
}
52+
53+
@Test
54+
fun minOperations9() {
55+
val k = 2
56+
assertThat<Int>(Solution().minOperations("000111", k), equalTo<Int>(-1))
57+
}
58+
59+
@Test
60+
fun minOperations10() {
61+
val k = 4
62+
assertThat<Int>(Solution().minOperations("001100", k), equalTo<Int>(1))
63+
}
64+
65+
@Test
66+
fun minOperations11() {
67+
val k = 3
68+
assertThat<Int>(Solution().minOperations("000100", k), equalTo<Int>(3))
69+
}
70+
71+
@Test
72+
fun minOperations12() {
73+
val k = 4
74+
assertThat<Int>(Solution().minOperations("111111", k), equalTo<Int>(0))
75+
}
76+
77+
@Test
78+
fun minOperations13() {
79+
val k = 4
80+
assertThat<Int>(Solution().minOperations("001001", k), equalTo<Int>(1))
81+
}
2282
}

0 commit comments

Comments
 (0)