Python || Priority Queue || Follow up || From LC239
My intuition is this is a complexed 2-dim sliding window maximum extended from LC239. When I find the brute force solutions in the discuss, I feel like I am a i
My intuition is this is a complexed 2-dim sliding window maximum extended from LC239. When I find the brute force solutions in the discuss, I feel like I am a idiot... But I still want to post my answer since this can be used when k(the range) is not fixed. Welcome your comments.\nThe main idea is using a piority queue to firstly figure the maximum  for every row/col, and then use the function again for the existing result col/row. I calculated the columns first in avoid of the need of switching rows&cols if the rows are calculated first.\n\n![image](https://assets.leetcode.com/users/images/67e22357-2713-4309-bf7c-b4934916ff10_1660511772.4988132.png)\n\n\nTC & SC: O(n^2). Please correct me if I am wrong:)\n\n```\nclass Solution:\n    def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n        \n        \n        def largest(nums, k):\n            res = []\n            queue = []\n            for i in range(k-1):\n                heapq.heappush(queue, (-nums[i], i))\n                \n            for i in range(k-1, len(nums)):\n                heapq.heappush(queue, (-nums[i], i))\n                while queue[0][1] < i-k+1:\n                    heapq.heappop(queue)\n                res.append(-queue[0][0])\n                \n            return res\n        \n        n = len(grid)\n        res = []\n        for j in range(n):\n            col = [grid[i][j] for i in range(n)]\n            res.append(largest(col, 3))\n            \n        ans = []\n        for j in range(len(res[0])):\n            row = [res[i][j] for i in range(n)]\n            ans.append(largest(row, 3))\n         \n        return ans\n            \n```
['Heap (Priority Queue)', 'Python']