In Java Using TreeMap
 # Intuition  \n\n\n\n\n\n# Complexity\n- Time complexity: O(n log n)\n\n\n- Space complexity: O(n)\n\n\n# Code\n\nclass Solution {\n    public int maximizeGrea
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n log n)$$\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 int maximizeGreatness(int[] nums) {\n        TreeMap<Integer, Integer> numsCount = new TreeMap<>();\n        for (int num : nums) {\n            numsCount.put(num, numsCount.getOrDefault(num, 0) + 1);\n        }\n        int res = 0;\n        for (int num : nums) {\n            Integer higherKey = numsCount.higherKey(num);\n            if (higherKey != null) {\n                numsCount.put(higherKey, numsCount.get(higherKey) - 1);\n                if (numsCount.get(higherKey) == 0) {\n                    numsCount.remove(higherKey);\n                }\n                res++;\n            }\n        }\n        return res;\n    }\n}\n```
['Heap (Priority Queue)', 'Java']