C# | List | BinarySearch api | O(n log n ) solution
I could not crack this solution during contest, I did not think about sorting query set would solve this problem in log (n) search, though it\'s not a suitable 
I could not crack this solution during contest, I did not think about sorting query set would solve this problem in log (n) search, though it\'s not a suitable solution during the realtime practical problems as we will not have queries in-hand upfront. \n\nI feel implementation of this solution in C# is a worth giving time, took the idea from @votrubac [here](https://leetcode.com/problems/closest-room/discuss/1186023/C%2B%2BJava-Sort-Queries)\n\nidea summary:\n - Sort rooms and queries by size decending order - O(n logn + qlogq)\n - For each query maintain rooms qualified sofar into sorted list - (q times)\n - Binary search on the rooms sofar to find the minimum perferred room for that query. - (log r)\n \n time: `O(r log r + qlog q + q log r)`\n space: `O(r)`\n \n **Note: I maintained list in sorted order using BinerySearch api and then did a binary search to find out the minimized preferred room.**\n\n```\npublic class Solution {\n    public int[] ClosestRoom(int[][] rooms, int[][] queries) {\n        Array.Sort(rooms, (a, b) => b[1] - a[1]);\n        var sortedQueries = queries\n                 .Select((x, indx) => new []{x[0] , x[1], indx})\n                 .OrderByDescending(x=> x[1])\n                 .ToArray();\n        \n        int i=0, n = rooms.Length, q = queries.Length;\n        List<int> roomsSoFar = new List<int>();\n        int[] result = new int[q];\n        \n        foreach(var query in sortedQueries){\n            int perferredId = query[0], minSize = query[1], queryIndx = query[2];\n            \n            while(i < n && rooms[i][1] >= minSize){\n                int index = roomsSoFar.BinarySearch(rooms[i][0]);\n\n                if(index < 0)\n                    index = ~index;\n                roomsSoFar.Insert(index, rooms[i][0]);\n                i++;\n            }\n            \n            int ans = GetPreferedRoom(roomsSoFar, perferredId);\n            result[queryIndx] = ans;\n        }\n        return result;\n    }\n    \n    private int GetPreferedRoom(List<int> rooms, int id)\n    {\n        if(rooms.Count == 0)\n            return -1;\n        \n        int lowerbound = rooms.LowerBound(id);\n        int upperbound = rooms.UpperBound(id);\n        \n        if(lowerbound >= 0 && upperbound >= 0)\n            return id - rooms[lowerbound] <= rooms[upperbound] - id ? rooms[lowerbound] : rooms[upperbound];\n        else if(lowerbound >= 0)\n            return rooms[lowerbound];\n        else  \n            return rooms[upperbound];\n    }\n}\npublic static class ListExtentions\n{\n    public static int LowerBound(this List<int> list, int id){\n        int left = 0, right = list.Count - 1;\n        int lowerbound = -1;\n\n        while(left <= right){\n            int mid = left + (right - left) / 2;\n            if(list[mid] <= id){\n                lowerbound = mid;\n                left = mid + 1;\n            }\n            else\n                right = mid - 1;\n        }\n        return lowerbound;\n    }\n\n    public static int UpperBound(this List<int> list, int id){\n        int left = 0, right = list.Count - 1;\n        int upperbound = -1;\n\n        left = 0; right = list.Count -1;\n        while(left <= right){\n            int mid = left + (right - left) / 2;\n            if(list[mid] >= id){\n                upperbound = mid;\n                right = mid - 1;\n            }\n            else\n                left = mid + 1;\n        }\n        return upperbound;\n    }\n}\n```
[]