leetcode/0870.Advantage-Shuffle/README.md
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indices i for which A[i] > B[i].
Return any permutation of A that maximizes its advantage with respect to B.
Example 1:
Input:A = [2,7,11,15], B = [1,10,4,11]
Output:[2,11,7,15]
Example 2:
Input:A = [12,24,8,32], B = [13,25,32,11]
Output:[24,32,8,12]
Note:
1 <= A.length = B.length <= 100000 <= A[i] <= 10^90 <= B[i] <= 10^9给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。返回 A 的任意排列,使其相对于 B 的优势最大化。
package leetcode
import "sort"
func advantageCount1(A []int, B []int) []int {
n := len(A)
sort.Ints(A)
sortedB := make([]int, n)
for i := range sortedB {
sortedB[i] = i
}
sort.Slice(sortedB, func(i, j int) bool {
return B[sortedB[i]] < B[sortedB[j]]
})
useless, i, res := make([]int, 0), 0, make([]int, n)
for _, index := range sortedB {
b := B[index]
for i < n && A[i] <= b {
useless = append(useless, A[i])
i++
}
if i < n {
res[index] = A[i]
i++
} else {
res[index] = useless[0]
useless = useless[1:]
}
}
return res
}