LeetCode in Kotlin

870. Advantage Shuffle

Medium

You are given two integer arrays nums1 and nums2 both of the same length. The advantage of nums1 with respect to nums2 is the number of indices i for which nums1[i] > nums2[i].

Return any permutation of nums1 that maximizes its advantage with respect to nums2.

Example 1:

Input: nums1 = [2,7,11,15], nums2 = [1,10,4,11]

Output: [2,11,7,15]

Example 2:

Input: nums1 = [12,24,8,32], nums2 = [13,25,32,11]

Output: [24,32,8,12]

Constraints:

Solution

import java.util.PriorityQueue

class Solution {
    fun advantageCount(nums1: IntArray, nums2: IntArray): IntArray {
        val n = nums1.size
        nums1.sort()
        val maxpq = PriorityQueue { pair1: IntArray, pair2: IntArray ->
            pair2[1] - pair1[1]
        }
        for (i in 0 until n) {
            maxpq.offer(intArrayOf(i, nums2[i]))
        }
        var left = 0
        var right = n - 1
        val res = IntArray(n)
        while (maxpq.isNotEmpty()) {
            val pair = maxpq.poll()
            val i = pair[0]
            val `val` = pair[1]
            if (nums1[right] > `val`) {
                res[i] = nums1[right]
                right--
            } else {
                res[i] = nums1[left]
                left++
            }
        }
        return res
    }
}