返回介绍

solution / 2000-2099 / 2007.Find Original Array From Doubled Array / README_EN

发布于 2024-06-17 01:03:12 字数 6253 浏览 0 评论 0 收藏 0

2007. Find Original Array From Doubled Array

中文文档

Description

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_.

 

Example 1:

Input: changed = [1,3,4,2,6,8]
Output: [1,3,4]
Explanation: One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].

Example 2:

Input: changed = [6,3,0,1]
Output: []
Explanation: changed is not a doubled array.

Example 3:

Input: changed = [1]
Output: []
Explanation: changed is not a doubled array.

 

Constraints:

  • 1 <= changed.length <= 105
  • 0 <= changed[i] <= 105

Solutions

Solution 1: Sorting + Counting + Traversal

First, we check if the length $n$ of the array changed is odd. If it is, we directly return an empty array.

Then, we sort the array changed, and use a hash table or array cnt to count the occurrence of each element in changed.

Next, we traverse the array changed. For each element $x$ in changed, we first check if $x$ exists in the hash table cnt. If it does not exist, we directly skip this element. Otherwise, we check if $x \times 2$ exists in cnt. If it does not exist, we directly return an empty array. Otherwise, we add $x$ to the answer array ans, and decrease the occurrence counts of $x$ and $x \times 2$ in cnt by $1$ each.

After the traversal, we check if the length of the answer array ans is $\frac{n}{2}$. If it is, we return ans, otherwise we return an empty array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array changed.

class Solution:
  def findOriginalArray(self, changed: List[int]) -> List[int]:
    n = len(changed)
    if n & 1:
      return []
    cnt = Counter(changed)
    changed.sort()
    ans = []
    for x in changed:
      if cnt[x] == 0:
        continue
      if cnt[x * 2] <= 0:
        return []
      ans.append(x)
      cnt[x] -= 1
      cnt[x * 2] -= 1
    return ans if len(ans) == n // 2 else []
class Solution {
  public int[] findOriginalArray(int[] changed) {
    int n = changed.length;
    if (n % 2 == 1) {
      return new int[] {};
    }
    Arrays.sort(changed);
    int[] cnt = new int[changed[n - 1] + 1];
    for (int x : changed) {
      ++cnt[x];
    }
    int[] ans = new int[n / 2];
    int i = 0;
    for (int x : changed) {
      if (cnt[x] == 0) {
        continue;
      }
      if (x * 2 >= cnt.length || cnt[x * 2] <= 0) {
        return new int[] {};
      }
      ans[i++] = x;
      cnt[x]--;
      cnt[x * 2]--;
    }
    return i == n / 2 ? ans : new int[] {};
  }
}
class Solution {
public:
  vector<int> findOriginalArray(vector<int>& changed) {
    int n = changed.size();
    if (n & 1) {
      return {};
    }
    sort(changed.begin(), changed.end());
    vector<int> cnt(changed.back() + 1);
    for (int& x : changed) {
      ++cnt[x];
    }
    vector<int> ans;
    for (int& x : changed) {
      if (cnt[x] == 0) {
        continue;
      }
      if (x * 2 >= cnt.size() || cnt[x * 2] <= 0) {
        return {};
      }
      ans.push_back(x);
      --cnt[x];
      --cnt[x * 2];
    }
    return ans.size() == n / 2 ? ans : vector<int>();
  }
};
func findOriginalArray(changed []int) []int {
  n := len(changed)
  ans := []int{}
  if n&1 == 1 {
    return ans
  }
  sort.Ints(changed)
  cnt := make([]int, changed[n-1]+1)
  for _, x := range changed {
    cnt[x]++
  }
  for _, x := range changed {
    if cnt[x] == 0 {
      continue
    }
    if x*2 >= len(cnt) || cnt[x*2] <= 0 {
      return []int{}
    }
    ans = append(ans, x)
    cnt[x]--
    cnt[x*2]--
  }
  if len(ans) != n/2 {
    return []int{}
  }
  return ans
}
function findOriginalArray(changed: number[]): number[] {
  const n = changed.length;
  if (n & 1) {
    return [];
  }
  const cnt = new Map<number, number>();
  for (const x of changed) {
    cnt.set(x, (cnt.get(x) || 0) + 1);
  }
  changed.sort((a, b) => a - b);
  const ans: number[] = [];
  for (const x of changed) {
    if (cnt.get(x) == 0) {
      continue;
    }
    if ((cnt.get(x * 2) || 0) <= 0) {
      return [];
    }
    ans.push(x);
    cnt.set(x, (cnt.get(x) || 0) - 1);
    cnt.set(x * 2, (cnt.get(x * 2) || 0) - 1);
  }
  return ans.length == n / 2 ? ans : [];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文