1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public int magicTower(int[] nums) { long sum = 0; for (int num : nums) { sum += num; } if (sum < 0) { return -1; } int ans = 0; long hp = 1; PriorityQueue<Integer> q = new PriorityQueue<>(); for (int num : nums) { if (num < 0) { q.offer(num); } hp += num; if (hp < 1) { hp -= q.poll(); ++ans; } } return ans; } }
|