Skip to content

Commit f3451c0

Browse files
committed
Updated tags
1 parent 666ef69 commit f3451c0

File tree

9 files changed

+179
-152
lines changed
  • src
    • main/kotlin/g3501_3600
      • s3572_maximize_ysum_by_picking_a_triplet_of_distinct_xvalues
      • s3573_best_time_to_buy_and_sell_stock_v
      • s3574_maximize_subarray_gcd_score
      • s3575_maximum_good_subtree_score
      • s3576_transform_array_to_all_equal_elements
      • s3577_count_the_number_of_computer_unlocking_permutations
      • s3578_count_partitions_with_max_min_difference_at_most_k
      • s3579_minimum_steps_to_convert_string_with_operations
    • test/kotlin/g3501_3600/s3576_transform_array_to_all_equal_elements

9 files changed

+179
-152
lines changed
Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
package g3501_3600.s3572_maximize_ysum_by_picking_a_triplet_of_distinct_xvalues
22

3-
// #Medium #2025_06_08_Time_72_ms_(100.00%)_Space_77.69_MB_(100.00%)
4-
5-
import java.util.Collections
6-
import java.util.PriorityQueue
7-
import kotlin.math.max
3+
// #Medium #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue
4+
// #2025_06_10_Time_5_ms_(100.00%)_Space_82.11_MB_(56.00%)
85

96
class Solution {
107
fun maxSumDistinctTriplet(x: IntArray, y: IntArray): Int {
11-
val map: MutableMap<Int, Int> = HashMap<Int, Int>()
12-
for (i in x.indices) {
13-
map.put(x[i], max(map.getOrDefault(x[i], 0), y[i]))
8+
var index = -1
9+
var max = -1
10+
var sum = 0
11+
for (i in y.indices) {
12+
if (y[i] > max) {
13+
max = y[i]
14+
index = i
15+
}
16+
}
17+
sum += max
18+
if (max == -1) {
19+
return -1
1420
}
15-
val maxHeap = PriorityQueue<Int>(Collections.reverseOrder<Int>())
16-
for (`val` in map.values) {
17-
maxHeap.add(`val`)
21+
var index2 = -1
22+
max = -1
23+
for (i in y.indices) {
24+
if (y[i] > max && x[i] != x[index]) {
25+
max = y[i]
26+
index2 = i
27+
}
1828
}
19-
if (maxHeap.size < 3) {
29+
sum += max
30+
if (max == -1) {
2031
return -1
2132
}
22-
var sum = 0
23-
for (i in 0..2) {
24-
sum += maxHeap.poll()!!
33+
max = -1
34+
for (i in y.indices) {
35+
if (y[i] > max && x[i] != x[index] && x[i] != x[index2]) {
36+
max = y[i]
37+
}
38+
}
39+
if (max == -1) {
40+
return -1
2541
}
42+
sum += max
2643
return sum
2744
}
2845
}
Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,30 @@
11
package g3501_3600.s3573_best_time_to_buy_and_sell_stock_v
22

3-
// #Medium #2025_06_08_Time_157_ms_(100.00%)_Space_99.08_MB_(100.00%)
3+
// #Medium #Array #Dynamic_Programming #2025_06_10_Time_27_ms_(100.00%)_Space_48.69_MB_(80.00%)
44

55
import kotlin.math.max
66

77
class Solution {
8-
private lateinit var dp: Array<Array<LongArray>>
9-
private lateinit var prices: IntArray
10-
11-
private fun f(i: Int, k: Int, state: Int): Long {
12-
if (i == prices.size) {
13-
return if (state == 0) 0 else MN
14-
}
15-
if (dp[i][k][state] != MN) {
16-
return dp[i][k][state]
17-
}
18-
val p = prices[i].toLong()
19-
var profit: Long = MN
20-
profit = max(profit, f(i + 1, k, state))
21-
if (state == 0) {
22-
profit = max(profit, f(i + 1, k, 1) - p)
23-
profit = max(profit, f(i + 1, k, 2) + p)
24-
} else if (k > 0) {
25-
profit = if (state == 1) {
26-
max(profit, f(i + 1, k - 1, 0) + p)
27-
} else {
28-
max(profit, f(i + 1, k - 1, 0) - p)
29-
}
30-
}
31-
dp[i][k][state] = profit
32-
return profit
33-
}
34-
358
fun maximumProfit(prices: IntArray, k: Int): Long {
36-
this.prices = prices
379
val n = prices.size
38-
dp = Array<Array<LongArray>>(n + 1) { Array<LongArray>(k + 1) { LongArray(3) } }
39-
for (twoD in dp) {
40-
for (oneD in twoD) {
41-
oneD.fill(MN)
10+
var prev = LongArray(n)
11+
var curr = LongArray(n)
12+
for (t in 1..k) {
13+
var bestLong = -prices[0].toLong()
14+
var bestShort = prices[0].toLong()
15+
curr[0] = 0
16+
for (i in 1..<n) {
17+
var res = curr[i - 1]
18+
res = max(res, prices[i] + bestLong)
19+
res = max(res, -prices[i] + bestShort)
20+
curr[i] = res
21+
bestLong = max(bestLong, prev[i - 1] - prices[i])
22+
bestShort = max(bestShort, prev[i - 1] + prices[i])
4223
}
24+
val tmp = prev
25+
prev = curr
26+
curr = tmp
4327
}
44-
return f(0, k, 0)
45-
}
46-
47-
companion object {
48-
private val MN = -1e14.toLong()
28+
return prev[n - 1]
4929
}
5030
}
Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,67 @@
11
package g3501_3600.s3574_maximize_subarray_gcd_score
22

3-
// #Hard #2025_06_08_Time_269_ms_(100.00%)_Space_63.86_MB_(100.00%)
3+
// #Hard #Array #Math #Enumeration #Number_Theory
4+
// #2025_06_10_Time_27_ms_(100.00%)_Space_48.98_MB_(100.00%)
45

56
import kotlin.math.max
67

78
class Solution {
89
fun maxGCDScore(nums: IntArray, k: Int): Long {
10+
var mx = 0
11+
for (x in nums) {
12+
mx = max(mx, x)
13+
}
14+
val width = 32 - Integer.numberOfLeadingZeros(mx)
15+
val lowbitPos: Array<MutableList<Int>> = Array<MutableList<Int>>(width) { i: Int -> ArrayList<Int>() }
16+
val intervals = Array<IntArray?>(width + 1) { IntArray(3) }
17+
var size = 0
918
var ans: Long = 0
10-
val n = nums.size
11-
for (i in 0..<n) {
12-
var countGCD: Long = 0
13-
var oddCount: Long = 0
14-
var ongoingGCD: Long = 0
15-
for (j in i..<n) {
16-
val currentGCD = gcd(ongoingGCD, nums[j].toLong())
17-
if (currentGCD != ongoingGCD) {
18-
ongoingGCD = currentGCD
19-
countGCD = 1
20-
} else if (nums[j].toLong() == ongoingGCD) {
21-
countGCD++
22-
}
23-
if (nums[j] % 2 != 0) {
24-
oddCount++
19+
for (i in nums.indices) {
20+
val x = nums[i]
21+
val tz = Integer.numberOfTrailingZeros(x)
22+
lowbitPos[tz].add(i)
23+
for (j in 0..<size) {
24+
intervals[j]!![0] = gcd(intervals[j]!![0], x)
25+
}
26+
intervals[size]!![0] = x
27+
intervals[size]!![1] = i - 1
28+
intervals[size]!![2] = i
29+
size++
30+
var idx = 1
31+
for (j in 1..<size) {
32+
if (intervals[j]!![0] != intervals[j - 1]!![0]) {
33+
intervals[idx]!![0] = intervals[j]!![0]
34+
intervals[idx]!![1] = intervals[j]!![1]
35+
intervals[idx]!![2] = intervals[j]!![2]
36+
idx++
37+
} else {
38+
intervals[idx - 1]!![2] = intervals[j]!![2]
2539
}
26-
val len = j - i + 1
27-
var res = ongoingGCD * len
28-
if (ongoingGCD % 2 != 0L) {
29-
if (k >= oddCount) {
30-
res *= 2L
31-
}
32-
} else if (k >= countGCD) {
33-
res *= 2L
40+
}
41+
size = idx
42+
for (j in 0..<size) {
43+
val g = intervals[j]!![0]
44+
val l = intervals[j]!![1]
45+
val r = intervals[j]!![2]
46+
ans = max(ans, g.toLong() * (i - l))
47+
val pos = lowbitPos[Integer.numberOfTrailingZeros(g)]
48+
val minL = if (pos.size > k) max(l, pos.get(pos.size - k - 1)!!) else l
49+
if (minL < r) {
50+
ans = max(ans, g.toLong() * 2 * (i - minL))
3451
}
35-
ans = max(ans, res)
3652
}
3753
}
3854
return ans
3955
}
4056

41-
private fun gcd(a: Long, b: Long): Long {
42-
if (a == 0L) {
43-
return b
57+
private fun gcd(a: Int, b: Int): Int {
58+
var a = a
59+
var b = b
60+
while (a != 0) {
61+
val tmp = a
62+
a = b % a
63+
b = tmp
4464
}
45-
return gcd(b % a, a)
65+
return b
4666
}
4767
}
Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,85 @@
11
package g3501_3600.s3575_maximum_good_subtree_score
22

3-
// #Hard #2025_06_08_Time_414_ms_(100.00%)_Space_66.06_MB_(100.00%)
3+
// #Hard #Array #Dynamic_Programming #Depth_First_Search #Tree #Bit_Manipulation #Bitmask
4+
// #2025_06_10_Time_86_ms_(100.00%)_Space_67.73_MB_(33.33%)
45

5-
import kotlin.math.abs
66
import kotlin.math.max
77

88
class Solution {
9-
private var ans: Long = 0
9+
private val digits = 10
10+
private val full = 1 shl digits
11+
private val neg = Long.Companion.MIN_VALUE / 4
12+
private val mod = 1e9.toLong() + 7
13+
private lateinit var tree: Array<ArrayList<Int>>
14+
private lateinit var `val`: IntArray
15+
private lateinit var mask: IntArray
16+
private lateinit var isOk: BooleanArray
17+
private var res: Long = 0
18+
1019
fun goodSubtreeSum(vals: IntArray, par: IntArray): Int {
1120
val n = vals.size
12-
val adj: Array<MutableList<Int>> = Array<MutableList<Int>>(n) { mutableListOf() }
21+
`val` = vals
22+
mask = IntArray(n)
23+
isOk = BooleanArray(n)
24+
for (i in 0..<n) {
25+
var m = 0
26+
var v = vals[i]
27+
var valid = true
28+
while (v > 0) {
29+
val d = v % 10
30+
if (((m shr d) and 1) == 1) {
31+
valid = false
32+
break
33+
}
34+
m = m or (1 shl d)
35+
v /= 10
36+
}
37+
mask[i] = m
38+
isOk[i] = valid
39+
}
40+
tree = Array(n) { initialCapacity: Int -> ArrayList(initialCapacity) }
41+
Arrays.setAll<MutableList<out Any?>?>(tree, IntFunction { initialCapacity: Int -> ArrayList(initialCapacity) })
42+
val root = 0
1343
for (i in 1..<n) {
14-
adj[par[i]].add(i)
44+
tree[par[i]].add(i)
1545
}
16-
this.ans = 0
17-
dfs(0, vals, adj)
18-
return ((this.ans % MOD + MOD) % MOD).toInt()
46+
dfs(root)
47+
return (res % mod).toInt()
1948
}
2049

21-
private fun dfs(u: Int, vals: IntArray, adj: Array<MutableList<Int>>): MutableMap<Int, Int> {
22-
// du: The DP map for the subtree at node u.
23-
// Key: bitmask of digits. Value: max sum for that combination of digits.
24-
val du: MutableMap<Int, Int> = HashMap<Int, Int>()
25-
// Base case: A sum of 0 is possible with an empty set of digits (mask 0).
26-
du.put(0, 0)
27-
// Process the current node's value.
28-
val s = abs(vals[u]).toString()
29-
if (hasUniqueDigits(s)) {
30-
var mask = 0
31-
for (c in s.toCharArray()) {
32-
mask = mask or (1 shl (c.code - '0'.code))
33-
}
34-
du.put(mask, vals[u])
50+
private fun dfs(u: Int): LongArray {
51+
var dp = LongArray(full)
52+
Arrays.fill(dp, neg)
53+
dp[0] = 0
54+
if (isOk[u]) {
55+
dp[mask[u]] = `val`[u].toLong()
3556
}
36-
for (v in adj[u]) {
37-
val dv = dfs(v, vals, adj)
38-
val duSnapshot: MutableMap<Int, Int> = HashMap<Int, Int>(du)
39-
for (entryV in dv.entries) {
40-
val mv: Int = entryV.key
41-
val sv: Int = entryV.value
42-
for (entryU in duSnapshot.entries) {
43-
val mu: Int = entryU.key
44-
val su: Int = entryU.value
45-
// If the digit sets are disjoint (no common bits in masks), we can combine
46-
// them.
47-
if ((mu and mv) == 0) {
48-
val newMask = mu or mv
49-
val newSum = su + sv
50-
// Update `du` with the best possible sum for the new combined mask.
51-
du.put(
52-
newMask,
53-
max(du.getOrDefault(newMask, Int.Companion.MIN_VALUE), newSum),
54-
)
57+
for (v in tree[u]) {
58+
val child = dfs(v)
59+
val newDp = dp.copyOf(full)
60+
for (m1 in 0..<full) {
61+
if (dp[m1] < 0) {
62+
continue
63+
}
64+
val remain = full - 1 - m1
65+
var m2 = remain
66+
while (m2 > 0) {
67+
if (child[m2] < 0) {
68+
m2 = (m2 - 1) and remain
69+
continue
5570
}
71+
val newM = m1 or m2
72+
newDp[newM] = max(newDp[newM], dp[m1] + child[m2])
73+
m2 = (m2 - 1) and remain
5674
}
5775
}
76+
dp = newDp
5877
}
59-
// After processing all children, the max value in `du` is the "good" sum for the subtree at
60-
// u.
61-
// Initialize with a very small number to correctly find the maximum, even if sums are
62-
// negative.
63-
var maxSubtreeSum = Int.Companion.MIN_VALUE
64-
for (sum in du.values) {
65-
maxSubtreeSum = max(maxSubtreeSum, sum)
78+
var best: Long = 0
79+
for (v in dp) {
80+
best = max(best, v)
6681
}
67-
// Add this subtree's best sum to the total answer.
68-
// If du is empty (should not happen due to {0:0}), we add 0.
69-
this.ans += (if (maxSubtreeSum == Int.Companion.MIN_VALUE) 0 else maxSubtreeSum).toLong()
70-
return du
71-
}
72-
73-
private fun hasUniqueDigits(s: String): Boolean {
74-
val digits: MutableSet<Char> = HashSet<Char>()
75-
for (c in s.toCharArray()) {
76-
if (!digits.add(c)) {
77-
return false
78-
}
79-
}
80-
return true
81-
}
82-
83-
companion object {
84-
private const val MOD = 1000000007
82+
res = (res + best) % mod
83+
return dp
8584
}
8685
}

src/main/kotlin/g3501_3600/s3576_transform_array_to_all_equal_elements/Solution.kt

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

3-
// #Medium #2025_06_08_Time_10_ms_(91.67%)_Space_82.55_MB_(58.33%)
3+
// #Medium #Array #Greedy #2025_06_10_Time_11_ms_(92.31%)_Space_84.38_MB_(15.38%)
44

55
class Solution {
66
fun canMakeEqual(nums: IntArray, k: Int): Boolean {

src/main/kotlin/g3501_3600/s3577_count_the_number_of_computer_unlocking_permutations/Solution.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3501_3600.s3577_count_the_number_of_computer_unlocking_permutations
22

3-
// #Medium #2025_06_08_Time_2_ms_(100.00%)_Space_70.32_MB_(14.29%)
3+
// #Medium #Array #Math #Combinatorics #Brainteaser
4+
// #2025_06_10_Time_2_ms_(100.00%)_Space_70.49_MB_(30.00%)
45

56
class Solution {
67
fun countPermutations(complexity: IntArray): Int {

0 commit comments

Comments
 (0)