File size: 4,239 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10048", "name": "LeetCode 0542: 01 Matrix"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: 01 Matrix (Difficulty: Medium)\nTags: Array, Dynamic Programming, Breadth-First Search, Matrix\n\nGiven an m x n binary matrix mat, return the distance of the nearest 0 for each cell.\nThe distance between two cells sharing a common edge is 1.\nNote: This question is the same as 1765: https://leetcode.com/problems/map-of-highest-peak/\n\nExample 1:\nInput: mat = [[0,0,0],[0,1,0],[0,0,0]]\nOutput: [[0,0,0],[0,1,0],[0,0,0]]\n\nExample 2:\nInput: mat = [[0,0,0],[0,1,0],[1,1,1]]\nOutput: [[0,0,0],[0,1,0],[1,2,1]]\n\nConstraints:\n- m == mat.length\n- n == mat[i].length\n- 1 <= m, n <= 10^4\n- 1 <= m * n <= 10^4\n- mat[i][j] is either 0 or 1.\n- There is at least one 0 in mat."}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(m * n)\n# Space: O(1)\n\n# dp solution\nclass Solution(object):\n    def updateMatrix(self, matrix):\n        \"\"\"\n        :type matrix: List[List[int]]\n        :rtype: List[List[int]]\n        \"\"\"\n        for i in range(len(matrix)):\n            for j in range(len(matrix[i])):\n                if not matrix[i][j]:\n                    continue\n                matrix[i][j] = float(\"inf\")\n                if i > 0:\n                    matrix[i][j] = min(matrix[i][j], matrix[i-1][j]+1)\n                if j > 0:\n                    matrix[i][j] = min(matrix[i][j], matrix[i][j-1]+1)\n        for i in reversed(range(len(matrix))):\n            for j in reversed(range(len(matrix[i]))):\n                if not matrix[i][j]:\n                    continue\n                if i < len(matrix)-1:\n                    matrix[i][j] = min(matrix[i][j], matrix[i+1][j]+1)\n                if j < len(matrix[i])-1:\n                    matrix[i][j] = min(matrix[i][j], matrix[i][j+1]+1)\n        return matrix\n\n\n# Time:  O(m * n)\n# Space: O(m * n)\n# dp solution\nclass Solution2(object):\n    def updateMatrix(self, matrix):\n        \"\"\"\n        :type matrix: List[List[int]]\n        :rtype: List[List[int]]\n        \"\"\"\n        dp = [[float(\"inf\")]*len(matrix[0]) for _ in range(len(matrix))]\n        for i in range(len(matrix)):\n            for j in range(len(matrix[i])):\n                if matrix[i][j] == 0:\n                    dp[i][j] = 0\n                else:\n                    if i > 0:\n                        dp[i][j] = min(dp[i][j], dp[i-1][j]+1)\n                    if j > 0:\n                        dp[i][j] = min(dp[i][j], dp[i][j-1]+1)\n        for i in reversed(range(len(matrix))):\n            for j in reversed(range(len(matrix[i]))):\n                if matrix[i][j] == 0:\n                    dp[i][j] = 0\n                else:\n                    if i < len(matrix)-1:\n                        dp[i][j] = min(dp[i][j], dp[i+1][j]+1)\n                    if j < len(matrix[i])-1:\n                        dp[i][j] = min(dp[i][j], dp[i][j+1]+1)\n        return dp\n\n\n# Time:  O(m * n)\n# Space: O(m * n)\nimport collections\n\n\nclass Solution3(object):\n    def updateMatrix(self, matrix):\n        \"\"\"\n        :type matrix: List[List[int]]\n        :rtype: List[List[int]]\n        \"\"\"\n        queue = collections.deque()\n        for i in range(len(matrix)):\n            for j in range(len(matrix[0])):\n                if matrix[i][j] == 0:\n                    queue.append((i, j))\n                else:\n                    matrix[i][j] = float(\"inf\")\n\n        dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n        while queue:\n            cell = queue.popleft()\n            for dir in dirs:\n                i, j = cell[0]+dir[0], cell[1]+dir[1]\n                if not (0 <= i < len(matrix) and\n                        0 <= j < len(matrix[0]) and\n                        matrix[i][j] > matrix[cell[0]][cell[1]]+1):\n                    continue\n                queue.append((i, j))\n                matrix[i][j] = matrix[cell[0]][cell[1]]+1\n\n        return matrix\n"}}