Leetcode / 2974. Minimum Number Game
Pick a programming language:
Here is the source code for the solution to this problem.
import java.util.Arrays;
// import java.util.PriorityQueue;
class Solution {
// without new array, modifying in place
public int[] numberGame(int[] nums) {
Arrays.sort(nums);
for (int i = 1; i < nums.length; i += 2) {
int temp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = temp;
}
return nums;
}
// public int[] numberGame(int[] nums) {
// // Minheap with capacity passed to constructor
// PriorityQueue<Integer> pq = new PriorityQueue<Integer>(nums.length);
// int[] arr = new int[nums.length];
// for (int i = 0; i < nums.length; i++) {
// pq.add(nums[i]);
// }
// for (int i = 1; i < arr.length; i += 2) {
// int first = pq.poll();
// int second = pq.poll();
// arr[i - 1] = second;
// arr[i] = first;
// }
// return arr;
// }
}
Did you like the lesson? 😆👍
Consider a donation to support our work: