NVIDIA 面试题 #2 —— 从倍增打乱数组还原原数组

1次阅读
没有评论

An integer array "original" is transformed into a doubled array "changed" by appending twice the value of every element in "original", and then randomly shuffling the resulting array.
Given an array "changed", return "original" if "changed" is a doubled array. If "changed" is not a doubled array, return an empty array. The elements in "original" may be returned in any order.

总结(思路)
计数 + 从小到大配对:

  1. changed 长度为奇数直接空数组;
  2. 统计频次(或排序后双指针 / 哈希)。
  3. 按值从小到大遍历 x:需要用 count[x] 去匹配 2xcount[2x](特别注意 x==0 要偶数个)。
  4. 每匹配一次,将 x 放入结果并减少对应频次;若不足则失败。
    时间 O(n log n)(排序版),空间 O(n)。

The VOprep team has long accompanied candidates through various major company OAs and VOs, including NVIDIA, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Stripe or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

正文完
 0