Java TreeMap (n log n)
This is not better than the two pointer solution. I came up with this one in an interview.\n\nTime = O(n log n)\nSpace = O(n)\n\nclass Solution {\n    public in
This is not better than the two pointer solution. I came up with this one in an interview.\n\nTime = O(n log n)\nSpace = O(n)\n```\nclass Solution {\n    public int maximizeGreatness(int[] nums) {\n        TreeMap<Integer, Integer> freq = new TreeMap<>();\n\n        for (int num : nums) {\n            freq.put(num, freq.getOrDefault(num, 0)+1);\n        }\n\n        int greatness = 0;\n\n        for (int num : nums) {\n            Integer nextLargestKey = freq.higherKey(num);\n\n            if (nextLargestKey == null) {\n                continue;\n            }\n\n            greatness++;\n            int curr = freq.get(nextLargestKey);\n\n            if (curr == 1) {\n                freq.remove(nextLargestKey);\n            } else {\n                freq.put(nextLargestKey, curr-1);\n            }\n        }\n\n        return greatness;\n    }\n}\n```
['Java']