728x90
반응형
✨ 문제
Minimum Number Game
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.
https://leetcode.com/problems/minimum-number-game/description/
✨ 개념
PriorityQueue
-
✨ 최종코드
public int[] numberGame(int[] nums) {
int[] answer = new int[nums.length];
Arrays.sort(nums);
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < nums.length - 1; i += 2) {
stack.push(nums[i]);
stack.push(nums[i + 1]);
answer[i] = stack.pop();
answer[i + 1] = stack.pop();
}
return answer;
}
728x90
반응형
'알고리즘 > 99클럽' 카테고리의 다른 글
99클럽 코테 스터디 6일차 TIL + Heap, PriorityQueue 우선순위 큐 (0) | 2024.05.26 |
---|---|
99클럽 코테 스터디 5일차 TIL + PriorityQueue, Heap (0) | 2024.05.25 |
99클럽 코테 스터디 3일차 TIL + Stack (0) | 2024.05.23 |
99클럽 코테 스터디 2일차 TIL + Queue, LinkedList (0) | 2024.05.22 |
99클럽 코테 스터디 1일차 TIL + HashMap (0) | 2024.05.21 |