返回介绍

solution / 2900-2999 / 2974.Minimum Number Game / README_EN

发布于 2024-06-17 01:02:58 字数 7042 浏览 0 评论 0 收藏 0

2974. Minimum Number Game

中文文档

Description

You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:

  • Every round, first Alice will remove the minimum element from nums, and then Bob does the same.
  • Now, first Bob will append the removed element in the array arr, and then Alice does the same.
  • The game continues until nums becomes empty.

Return _the resulting array _arr.

 

Example 1:

Input: nums = [5,4,2,3]
Output: [3,2,5,4]
Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].

Example 2:

Input: nums = [2,5]
Output: [5,2]
Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • nums.length % 2 == 0

Solutions

Solution 1: Simulation + Priority Queue (Min Heap)

We can put the elements in the array $nums$ into a min heap one by one, and each time take out two elements $a$ and $b$ from the min heap, then put $b$ and $a$ into the answer array in turn, until the min heap is empty.

Time complexity is $O(n \times \log n)$, and space complexity is $O(n)$. Where $n$ is the length of the array $nums$.

class Solution:
  def numberGame(self, nums: List[int]) -> List[int]:
    heapify(nums)
    ans = []
    while nums:
      a, b = heappop(nums), heappop(nums)
      ans.append(b)
      ans.append(a)
    return ans
class Solution {
  public int[] numberGame(int[] nums) {
    PriorityQueue<Integer> pq = new PriorityQueue<>();
    for (int x : nums) {
      pq.offer(x);
    }
    int[] ans = new int[nums.length];
    int i = 0;
    while (!pq.isEmpty()) {
      int a = pq.poll();
      ans[i++] = pq.poll();
      ans[i++] = a;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> numberGame(vector<int>& nums) {
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int x : nums) {
      pq.push(x);
    }
    vector<int> ans;
    while (pq.size()) {
      int a = pq.top();
      pq.pop();
      int b = pq.top();
      pq.pop();
      ans.push_back(b);
      ans.push_back(a);
    }
    return ans;
  }
};
func numberGame(nums []int) (ans []int) {
  pq := &hp{nums}
  heap.Init(pq)
  for pq.Len() > 0 {
    a := heap.Pop(pq).(int)
    b := heap.Pop(pq).(int)
    ans = append(ans, b)
    ans = append(ans, a)
  }
  return
}

type hp struct{ sort.IntSlice }

func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Pop() interface{} {
  old := h.IntSlice
  n := len(old)
  x := old[n-1]
  h.IntSlice = old[0 : n-1]
  return x
}
func (h *hp) Push(x interface{}) {
  h.IntSlice = append(h.IntSlice, x.(int))
}
function numberGame(nums: number[]): number[] {
  const pq = new MinPriorityQueue();
  for (const x of nums) {
    pq.enqueue(x);
  }
  const ans: number[] = [];
  while (pq.size()) {
    const a = pq.dequeue().element;
    const b = pq.dequeue().element;
    ans.push(b, a);
  }
  return ans;
}
use std::collections::BinaryHeap;
use std::cmp::Reverse;

impl Solution {
  pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
    let mut pq = BinaryHeap::new();

    for &x in &nums {
      pq.push(Reverse(x));
    }

    let mut ans = Vec::new();

    while let Some(Reverse(a)) = pq.pop() {
      if let Some(Reverse(b)) = pq.pop() {
        ans.push(b);
        ans.push(a);
      }
    }

    ans
  }
}

Solution 2: Sorting + Swapping

We can sort the array $nums$, and then swap the positions of every two adjacent elements in sequence to get the answer array.

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

class Solution:
  def numberGame(self, nums: List[int]) -> List[int]:
    nums.sort()
    for i in range(0, len(nums), 2):
      nums[i], nums[i + 1] = nums[i + 1], nums[i]
    return nums
class Solution {
  public int[] numberGame(int[] nums) {
    Arrays.sort(nums);
    for (int i = 0; i < nums.length; i += 2) {
      int t = nums[i];
      nums[i] = nums[i + 1];
      nums[i + 1] = t;
    }
    return nums;
  }
}
class Solution {
public:
  vector<int> numberGame(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int n = nums.size();
    for (int i = 0; i < n; i += 2) {
      swap(nums[i], nums[i + 1]);
    }
    return nums;
  }
};
func numberGame(nums []int) []int {
  sort.Ints(nums)
  for i := 0; i < len(nums); i += 2 {
    nums[i], nums[i+1] = nums[i+1], nums[i]
  }
  return nums
}
function numberGame(nums: number[]): number[] {
  nums.sort((a, b) => a - b);
  for (let i = 0; i < nums.length; i += 2) {
    [nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
  }
  return nums;
}
impl Solution {
  pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
    let mut nums = nums;
    nums.sort_unstable();
    for i in (0..nums.len()).step_by(2) {
      nums.swap(i, i + 1);
    }
    nums
  }
}

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

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

发布评论

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