This is a homework question, and I have written the code but wasn't sure if I had picked the right data structure for this job and minimised time complexity. Can anyone give me some feedback, anything about space complexity or potential bugs that can occur is appreciated as well.
Problem Description
MagicTheGathering
Aim: deal with a series of command (SUMMON, KILL, HEAL). Each SUMMON command will create a monster and each monster has a initial HP value.
Each KILL command will kill the monster with the lowest HP. And each HEAL command applies to every monster in your list.
Input: N, the number of commands. Followed by N lines of commands.
Output: Each KILL command requires you to print the HP of the monster that was killed. At the end of the series of commands, print the HP of the remaining. monsters.
Input example:
8 SUMMON 30 SUMMON 20 SUMMON 50 HEAL 5 KILL SUMMON 7 KILL HEAL 10
Output example:
25 7 45 65
Attempt
import java.util.*;
import java.util.stream.*;
class Yugioh {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
PriorityQueue<Integer> queue = new PriorityQueue(Comparator.naturalOrder());
for (int i =0;i<count;i++) {
String cmd = sc.next();
if (cmd.equals("SUMMON")) {
int health = sc.nextInt();
queue.add(health);
} else if (cmd.equals("HEAL")) {
int health = sc.nextInt();
PriorityQueue<Integer> newQueue = new PriorityQueue<>(Comparator.naturalOrder());
List<Integer> data = queue.stream().map(x -> x + health).collect(Collectors.toList());
newQueue.addAll(data);
queue = newQueue;
} else if (cmd.equals("KILL")) {
System.out.println(queue.poll());
}
}
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}