Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahliu committed Dec 13, 2024
1 parent ba8caa3 commit 5b0b69e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/kotlin/p32xx/Problem3266.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package p32xx

import util.expect
import java.util.*

fun main() {
class Solution {
fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray {
if (multiplier == 1) {
return nums
}

val m = 1000000007L
var max = 0
val queue = PriorityQueue(compareBy<Pair<Long, Int>> { it.first }.thenBy { it.second });

for (i in nums.indices) {
max = maxOf(max, nums[i])
queue.offer(nums[i].toLong() to i)
}

var tk = k
while (queue.peek().first < max && tk > 0) {
queue.poll().also { (num, index) ->
queue.offer(num * multiplier to index)
}
tk--
}

for (numIndex in nums.indices) {
queue.poll().also { (x, y) ->
nums[y] = ((x % m) * multiplier.toBigInteger().modPow((tk / nums.size + (if (numIndex < tk % nums.size) 1 else 0)).toBigInteger(), m.toBigInteger())
.toLong() % m).toInt()
}
}

return nums
}
}

expect {
Solution().getFinalState(
intArrayOf(), 1, 1
)
}
}

0 comments on commit 5b0b69e

Please sign in to comment.