Skip to content

Commit fbd898a

Browse files
committed
Updated tags
1 parent b615180 commit fbd898a

File tree

8 files changed

+130
-93
lines changed
  • src/main/kotlin/g3601_3700

8 files changed

+130
-93
lines changed

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

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

3-
// #Medium #2025_07_20_Time_3_ms_(100.00%)_Space_62.52_MB_(100.00%)
3+
// #Medium #Biweekly_Contest_161 #2025_07_22_Time_6_ms_(100.00%)_Space_78.20_MB_(81.48%)
44

55
import kotlin.math.abs
66

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

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

3-
// #Medium #2025_07_20_Time_16_ms_(96.67%)_Space_70.90_MB_(100.00%)
3+
// #Medium #Biweekly_Contest_161 #2025_07_22_Time_14_ms_(100.00%)_Space_86.20_MB_(47.83%)
44

55
class Solution {
66
private var m = 0
Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,100 @@
11
package g3601_3700.s3620_network_recovery_pathways
22

3-
// #Hard #2025_07_20_Time_18_ms_(100.00%)_Space_120.24_MB_(100.00%)
3+
// #Hard #Biweekly_Contest_161 #2025_07_22_Time_212_ms_(66.67%)_Space_150.09_MB_(13.33%)
44

55
import kotlin.math.max
6-
import kotlin.math.min
76

87
class Solution {
9-
private var ans = -1
10-
private var d = 0
11-
private var k: Long = 0
12-
13-
fun findMaxPathScore(edges: Array<IntArray>, online: BooleanArray, k: Long): Int {
14-
val n = online.size
15-
this.k = k
16-
this.d = n - 1
17-
val g: Array<ArrayList<P>> = Array(n) { ArrayList() }
18-
for (i in edges) {
19-
if (online[i[0]] && online[i[1]]) {
20-
g[i[0]].add(P(i[1], i[2]))
8+
private fun topologicalSort(n: Int, g: List<List<Int>>): List<Int> {
9+
val indeg = IntArray(n)
10+
for (i in 0 until n) {
11+
for (adjNode in g[i]) {
12+
indeg[adjNode]++
2113
}
2214
}
23-
dfs(g, 0, 0L, Int.Companion.MAX_VALUE)
24-
return ans
25-
}
26-
27-
private fun dfs(g: Array<ArrayList<P>>, s: Int, tc: Long, max: Int) {
28-
if (s == d) {
29-
if (ans == -1) {
30-
ans = max
31-
} else {
32-
ans = max(ans, max)
15+
val q: Queue<Int> = LinkedList()
16+
val ts = ArrayList<Int>()
17+
for (i in 0 until n) {
18+
if (indeg[i] == 0) {
19+
q.offer(i)
3320
}
34-
return
3521
}
36-
for (i in g[s]) {
37-
val cost = tc + i.c
38-
if (ans != -1 && ans >= max) {
39-
return
22+
while (!q.isEmpty()) {
23+
val u = q.poll()
24+
ts.add(u)
25+
for (v in g[u]) {
26+
indeg[v]--
27+
if (indeg[v] == 0) {
28+
q.offer(v)
29+
}
4030
}
41-
if (cost <= k) {
42-
dfs(g, i.d, cost, min(max, i.c))
31+
}
32+
return ts
33+
}
34+
35+
private fun check(
36+
x: Int,
37+
n: Int,
38+
adj: List<List<IntArray>>,
39+
ts: List<Int>,
40+
online: BooleanArray,
41+
k: Long,
42+
): Boolean {
43+
val d = LongArray(n)
44+
Arrays.fill(d, Long.MAX_VALUE)
45+
d[0] = 0
46+
for (u in ts) {
47+
if (d[u] != Long.MAX_VALUE) {
48+
for (p in adj[u]) {
49+
val v = p[0]
50+
val c = p[1]
51+
if (c < x || !online[v]) {
52+
continue
53+
}
54+
if (d[u] + c < d[v]) {
55+
d[v] = d[u] + c
56+
}
57+
}
4358
}
4459
}
60+
return d[n - 1] <= k
4561
}
4662

47-
private class P(var d: Int, var c: Int)
63+
fun findMaxPathScore(edges: Array<IntArray>, online: BooleanArray, k: Long): Int {
64+
val n = online.size
65+
// Adjacency list for graph with edge weights
66+
val adj = ArrayList<ArrayList<IntArray>>()
67+
for (i in 0 until n) {
68+
adj.add(ArrayList())
69+
}
70+
val g = ArrayList<ArrayList<Int>>()
71+
for (i in 0 until n) {
72+
g.add(ArrayList())
73+
}
74+
for (e in edges) {
75+
val u = e[0]
76+
val v = e[1]
77+
val c = e[2]
78+
adj[u].add(intArrayOf(v, c))
79+
g[u].add(v)
80+
}
81+
val ts = topologicalSort(n, g)
82+
if (!check(0, n, adj, ts, online, k)) {
83+
return -1
84+
}
85+
var l = 0
86+
var h = 0
87+
for (e in edges) {
88+
h = max(h, e[2])
89+
}
90+
while (l < h) {
91+
val md = l + (h - l + 1) / 2
92+
if (check(md, n, adj, ts, online, k)) {
93+
l = md
94+
} else {
95+
h = md - 1
96+
}
97+
}
98+
return l
99+
}
48100
}
Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,56 @@
11
package g3601_3700.s3621_number_of_integers_with_popcount_depth_equal_to_k_i
22

3-
// #Hard #2025_07_20_Time_1_ms_(100.00%)_Space_41.10_MB_(100.00%)
3+
// #Hard #Biweekly_Contest_161 #2025_07_22_Time_2_ms_(100.00%)_Space_41.19_MB_(100.00%)
44

55
class Solution {
6-
private val slct = Array<LongArray>(MX_LN) { LongArray(MX_LN) }
7-
private val popHeight = IntArray(MX_LN)
6+
companion object {
7+
private val comb = Array(61) { LongArray(61) }
8+
private val depth = IntArray(61)
89

9-
init {
10-
for (i in 0..<MX_LN) {
11-
slct[i][i] = 1
12-
slct[i][0] = slct[i][i]
13-
for (j in 1..<i) {
14-
slct[i][j] = slct[i - 1][j - 1] + slct[i - 1][j]
10+
init {
11+
for (i in 0..60) {
12+
comb[i][0] = 1L
13+
comb[i][i] = 1L
14+
for (j in 1 until i) {
15+
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j]
16+
}
17+
}
18+
depth[1] = 0
19+
for (i in 2..60) {
20+
depth[i] = depth[i.countOneBits()] + 1
1521
}
16-
}
17-
popHeight[1] = 0
18-
for (v in 2..<MX_LN) {
19-
popHeight[v] = 1 + popHeight[java.lang.Long.bitCount(v.toLong())]
2022
}
2123
}
2224

23-
private fun countNumbers(upperLimit: Long, setBits: Int): Long {
24-
if (setBits == 0) {
25-
return 1
26-
}
27-
var count: Long = 0
28-
var used = 0
29-
var len = 0
30-
var x = upperLimit
31-
while (x > 0) {
32-
len++
33-
x = x shr 1
34-
}
35-
for (pos in len - 1 downTo 0) {
36-
if (((upperLimit shr pos) and 1L) == 1L) {
37-
if (setBits - used <= pos) {
38-
count += slct[pos][setBits - used]
39-
}
40-
used++
41-
if (used > setBits) {
42-
break
25+
fun popcountDepth(n: Long, k: Int): Long {
26+
if (k == 0) { return 1L }
27+
fun countPop(x: Long, c: Int): Long {
28+
var res = 0L
29+
var ones = 0
30+
var bits = 0
31+
var t = x
32+
while (t > 0) {
33+
bits++
34+
t = t shr 1
35+
}
36+
for (i in bits - 1 downTo 0) {
37+
if ((x shr i) and 1L == 1L) {
38+
val rem = c - ones
39+
if (rem in 0..i) {
40+
res += comb[i][rem]
41+
}
42+
ones++
4343
}
4444
}
45+
return if (ones == c) res + 1 else res
4546
}
46-
if (java.lang.Long.bitCount(upperLimit) == setBits) {
47-
count++
48-
}
49-
return count
50-
}
51-
52-
fun popcountDepth(tillNumber: Long, depthQuery: Int): Long {
53-
if (depthQuery == 0) {
54-
return (if (tillNumber >= 1) 1 else 0).toLong()
55-
}
56-
var total: Long = 0
57-
for (ones in 1..<MX_LN) {
58-
if (popHeight[ones] == depthQuery - 1) {
59-
total += countNumbers(tillNumber, ones)
47+
var ans = 0L
48+
for (c in 1..60) {
49+
if (depth[c] == k - 1) {
50+
ans += countPop(n, c)
6051
}
6152
}
62-
if (depthQuery == 1) {
63-
total -= 1
64-
}
65-
return total
66-
}
67-
68-
companion object {
69-
private const val MX_LN = 61
53+
if (k == 1) { ans-- }
54+
return ans
7055
}
7156
}

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

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

3-
// #Easy #2025_07_20_Time_0_ms_(100.00%)_Space_40.53_MB_(_%)
3+
// #Easy #Weekly_Contest_459 #2025_07_22_Time_0_ms_(100.00%)_Space_40.35_MB_(100.00%)
44

55
class Solution {
66
fun checkDivisibility(n: Int): Boolean {

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

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

3-
// #Medium #2025_07_20_Time_30_ms_(99.91%)_Space_100.40_MB_(100.00%)
3+
// #Medium #Weekly_Contest_459 #2025_07_22_Time_58_ms_(68.00%)_Space_139.38_MB_(8.00%)
44

55
class Solution {
66
fun countTrapezoids(points: Array<IntArray>): Int {

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

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

3-
// #Hard #2025_07_20_Time_27_ms_(96.92%)_Space_125.98_MB_(100.00%)
3+
// #Hard #Weekly_Contest_459 #2025_07_22_Time_38_ms_(100.00%)_Space_134.15_MB_(100.00%)
44

55
class Solution {
66
private fun computeDepth(number: Long): Int {

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

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

3-
// #Hard #2025_07_21_Time_354_ms_(100.00%)_Space_131.52_MB_(52.31%)
3+
// #Hard #Weekly_Contest_459 #2025_07_22_Time_377_ms_(100.00%)_Space_162.04_MB_(37.50%)
44

55
import kotlin.math.abs
66

0 commit comments

Comments
 (0)