c# sort + replace repeated category item one by one
Intuition and Approach\n To get the result , we need try to select item with larger profit. The best situation is items with first largest profile are with diff
# Intuition and Approach\n To get the result , we need try to select item with larger profit. The best situation is items with first largest profile are with different profit.\nThen sort the items according to profit.\nWe start from first k elements, and record all items with repeated category with a Stack. The element with smallest profit will be on the top of the stack, in the later steps, we will replace these items with repeated category one by one with new category in the scan.\n\nfor each step, we calculate the elegance, and record the max.\n \n\n \n# Complexity\n- Time complexity:\n Sort: O(NlogN)\n \n\n- Space complexity:\n we use a stack and hashset , O(N)\n\n# Code\n```\npublic class Solution {\n    public long FindMaximumElegance(int[][] items, int k) {\n       var sortedItems = items.OrderBy(item=>-item[0]).ToArray();\n        HashSet<int> C = new ();         \n        Stack<int> S = new (); /*index of repeated category*/\n        long P = 0; \n        for(int i=0; i<k; i++)\n        {\n            if(C.Contains(sortedItems[i][1]))\n            {\n               S.Push(i); \n            }\n            else\n            {\n                C.Add(sortedItems[i][1]); \n            }\n            P += sortedItems[i][0];\n        }\n        \n        long E = P + (long)C.Count * C.Count;\n        //Console.WriteLine($"{C.Count},{P},{E}");\n        if(C.Count==k || k== items.Length)\n            return E;\n        \n        int p = k;\n        while(S.Count>0 && p<sortedItems.Length)\n        {\n            if(C.Contains(sortedItems[p][1]))\n            {\n                p++;\n                continue;\n            }\n            else\n            {              \n                C.Add(sortedItems[p][1]); \n                int idx = S.Pop();\n                P  = P - sortedItems[idx][0] + sortedItems[p][0];\n                E = Math.Max(E,P + (long)C.Count * C.Count); \n                p++;\n            }\n        }\n        return E;                              \n    }\n}\n```
['C#']