Java | O(NLogN) Solution Using Priority Queue & Set 🔥🔥
Intuition: Priority Queue + Set\n Describe your first thoughts on how to solve this problem. \n\n# Complexity\n- Time complexity: O(nlogn)\n Add your time compl
# Intuition: Priority Queue + Set\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public long[] unmarkedSumArray(int[] nums, int[][] queries) {\n        int n = nums.length;\n        int m = queries.length;\n\n        PriorityQueue<Pair> pq = new PriorityQueue<>(n, new Comparator<Pair>(){\n            \n            @Override\n            public int compare(Pair a, Pair b){\n                if(a.val == b.val){\n                    return a.index - b.index;\n                }\n\n                return a.val - b.val;\n            }\n        });\n\n        long sum = 0;\n        for(int i = 0; i < n; i++){\n            sum += nums[i];\n            pq.offer(new Pair(nums[i], i));\n        }\n\n        Set<Integer> set = new HashSet<>();\n\n        long ans[] = new long[m];\n        for(int i = 0; i < m; i++){\n            int index = queries[i][0];\n            int removeTopKElements = queries[i][1];\n\n            if(!set.contains(index)){\n                sum -= nums[index];\n                set.add(index);\n            }\n\n            while(!pq.isEmpty() && removeTopKElements > 0){\n                Pair top = pq.remove();\n                if(!set.contains(top.index)){\n                    sum -= top.val;\n                    set.add(top.index);\n                    removeTopKElements--;\n                }\n            }\n\n            ans[i] = sum;\n        }\n\n        return ans;\n    }\n\n    class Pair{\n        int val;\n        int index;\n\n        public Pair(int val, int index){\n            this.val = val;\n            this.index = index;\n        }\n    }\n}\n```
['Array', 'Hash Table', 'Sorting', 'Heap (Priority Queue)', 'Simulation', 'Java']