id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int poss(vector<vector<int>> &dis, int val, vector<vector<int>> &vis){\n queue<pair<int,int>>q;\n int n = dis.size();\n if(dis[0][0] < val)return 0;\n vis[0][0] = val + 1;\n q.push({0,0});\n // vis[0][0] = 1;\n int dx[] = {0, 1, 0, -1};\n int dy[] = {1, 0, -1, 0};\n while(!q.empty()){\n auto [x, y] = q.front();q.pop();\n // int x = ele[0], y = ele[1];\n if(x == n - 1 && y == n - 1)return 1;\n for(int i = 0; i < 4; i++){\n int newx = x + dx[i], newy = y + dy[i];\n // if(newx < 0 || newx >=n || newy < 0 || newy >=n || vis[newx][newy] == val + 1)continue;\n // if(dis[newx][newy] < val)continue;\n if(newx >= 0 && newx < n && newy >= 0 && newy < n && vis[newx][newy] != val + 1 && dis[newx][newy] >=val){\n q.push({newx, newy});\n vis[newx][newy] = val + 1;\n }\n }\n }\n return 0;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>>dis(n, vector<int>(n, 1e9));\n queue<pair<int,int>>q;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j] == 1){\n q.push({i,j});\n dis[i][j] = 0;\n }\n }\n }\n int dx[] = {0, 1, 0, -1};\n int dy[] = {1, 0, -1, 0};\n int len = 1;\n while(!q.empty()){\n int s = q.size();\n for(int i = 0; i < s; i++){\n auto [x, y] = q.front();\n q.pop();\n // int x = a[0], y = a[1];\n for(int j = 0; j < 4; j++){\n int newx = x + dx[j], newy = y + dy[j];\n // if(newx < 0 || newx >= n || newy < 0 || newy >=n)continue;\n // if(dis[newx][newy] <= len)continue;\n if(newx >= 0 && newx < n && newy >= 0 && newy < n && dis[newx][newy] > len){\n q.push({newx, newy});\n dis[newx][newy] = len;\n }\n }\n }\n len++;\n }\n int l = 0, r = 400;\n int ans = 0;\n vector<vector<int>>vis(dis.size(), vector<int>(dis.size(), 0));\n\n while(l <= r){\n int mid = (l + r)/2;\n if(poss(dis, mid, vis)){\n ans = mid;\n l = mid + 1;\n } else r = mid -1;\n }\n return ans;\n }\n};", "memory": "208456" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool poss(const vector<vector<int>> &dis, int val, vector<vector<int>> &vis) {\n queue<pair<int, int>> q;\n int n = dis.size();\n if (dis[0][0] < val) return false;\n \n q.push({0, 0});\n vis[0][0] = val + 1;\n int dx[] = {0, 1, 0, -1};\n int dy[] = {1, 0, -1, 0};\n \n while (!q.empty()) {\n auto [x, y] = q.front(); q.pop();\n if (x == n - 1 && y == n - 1) return true;\n \n for (int i = 0; i < 4; ++i) {\n int newx = x + dx[i], newy = y + dy[i];\n if (newx >= 0 && newx < n && newy >= 0 && newy < n && vis[newx][newy] != val + 1 && dis[newx][newy] >= val) {\n q.push({newx, newy});\n vis[newx][newy] = val + 1;\n }\n }\n }\n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>> &grid) {\n int n = grid.size();\n vector<vector<int>> dis(n, vector<int>(n, INT_MAX));\n queue<pair<int, int>> q;\n \n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n dis[i][j] = 0;\n }\n }\n }\n \n int dx[] = {0, 1, 0, -1};\n int dy[] = {1, 0, -1, 0};\n int len = 1;\n \n while (!q.empty()) {\n int s = q.size();\n for (int i = 0; i < s; ++i) {\n auto [x, y] = q.front(); q.pop();\n for (int j = 0; j < 4; ++j) {\n int newx = x + dx[j], newy = y + dy[j];\n if (newx >= 0 && newx < n && newy >= 0 && newy < n && dis[newx][newy] > len) {\n q.push({newx, newy});\n dis[newx][newy] = len;\n }\n }\n }\n len++;\n }\n\n int l = 0, r = 400, ans = 0;\n vector<vector<int>> vis(n, vector<int>(n, 0));\n\n while (l <= r) {\n int mid = (l + r) / 2;\n if (poss(dis, mid, vis)) {\n ans = mid;\n l = mid + 1;\n } else {\n r = mid - 1;\n }\n }\n \n return ans;\n }\n};\n", "memory": "208456" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n vector<int> rowDir = {-1, 1, 0, 0};\n vector<int> colDir = {0, 0, -1, 1};\n \n bool isValid(vector<vector<bool>>& visited, int i, int j, int n) {\n return i >= 0 && j >= 0 && i < n && j < n && !visited[i][j];\n }\n \n bool isSafe(vector<vector<int>>& distToThief, int safeDist) {\n int n = distToThief.size();\n queue<pair<int, int>> q;\n if (distToThief[0][0] < safeDist) return false;\n q.push({0, 0});\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n visited[0][0] = true;\n \n while (!q.empty()) {\n pair<int, int> curr = q.front();\n q.pop();\n int currRow = curr.first;\n int currCol = curr.second;\n \n if (currRow == n - 1 && currCol == n - 1) return true;\n \n for (int dirIdx = 0; dirIdx < 4; dirIdx++) {\n int newRow = currRow + rowDir[dirIdx];\n int newCol = currCol + colDir[dirIdx];\n \n if (isValid(visited, newRow, newCol, n)) {\n if (distToThief[newRow][newCol] < safeDist) continue;\n visited[newRow][newCol] = true;\n q.push({newRow, newCol});\n }\n }\n }\n return false;\n }\n \npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n queue<pair<int, int>> q;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n vector<vector<int>> distToThief(n, vector<int>(n, 0));\n \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n visited[i][j] = true;\n q.push({i, j});\n }\n }\n }\n \n int dist = 0;\n while (!q.empty()) {\n int size = q.size();\n while (size-- > 0) {\n pair<int, int> curr = q.front();\n q.pop();\n int currRow = curr.first;\n int currCol = curr.second;\n distToThief[currRow][currCol] = dist;\n \n for (int dirIdx = 0; dirIdx < 4; dirIdx++) {\n int newRow = currRow + rowDir[dirIdx];\n int newCol = currCol + colDir[dirIdx];\n \n if (!isValid(visited, newRow, newCol, n)) continue;\n \n visited[newRow][newCol] = true;\n q.push({newRow, newCol});\n }\n }\n dist++;\n }\n \n int low = 0, high = INT_MAX;\n int ans = 0;\n while (low <= high) {\n int mid = low + (high - low) / 2;\n if (isSafe(distToThief, mid)) {\n ans = mid;\n low = mid + 1;\n } else {\n high = mid - 1;\n }\n }\n return ans;\n }\n};\n", "memory": "213138" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int>rowDir = {-1, 1, 0, 0};\n vector<int>colDir = {0, 0, -1, 1};\n bool isValid(vector<vector<bool>>&visited, int i, int j)\n {\n if (i < 0 || j < 0 || i == visited.size() || j == visited[0].size() || visited[i][j])\n return false;\n return true;\n }\n //============================================================================================================\n bool isSafe(vector<vector<int>>&distToTheif, int safeDist) //check the validity of safenessFactor\n {\n int n = distToTheif.size();\n queue<pair<int, int>>q;\n if (distToTheif[0][0] < safeDist) return false;\n q.push({0, 0});\n vector<vector<bool>>visited(n, vector<bool>(n, false));\n visited[0][0] = true;\n while(!q.empty())\n {\n int currRow = q.front().first, currCol = q.front().second;\n q.pop();\n if (currRow == n - 1 && currCol == n - 1) return true;\n for (int dirIdx = 0; dirIdx < 4; dirIdx++)\n {\n int newRow = currRow + rowDir[dirIdx];\n int newCol = currCol + colDir[dirIdx];\n if (isValid(visited, newRow, newCol))\n {\n if (distToTheif[newRow][newCol] < safeDist) continue;\n visited[newRow][newCol] = true;\n q.push({newRow, newCol});\n }\n }\n }\n return false;\n }\n //===========================================================================================================\n int maximumSafenessFactor(vector<vector<int>>& grid) \n {\n int n = grid.size();\n queue<pair<int, int>>q;\n vector<vector<bool>>visited(n, vector<bool>(n, false));\n vector<vector<int>>distToTheif(n, vector<int>(n, -1));\n //========================================================================\n //Add all the theives in current queue\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j < n; j++)\n {\n if (grid[i][j] == 1) \n {\n visited[i][j] = true;\n q.push({i, j});\n }\n }\n }\n //=============================================================================\n //BFS to fill the \"DistToTheif\" 2D array\n int dist = 0;\n while(!q.empty())\n {\n int size = q.size();\n while(size--)\n {\n int currRow = q.front().first, currCol = q.front().second;\n q.pop();\n distToTheif[currRow][currCol] = dist;\n for (int dirIdx = 0; dirIdx < 4; dirIdx++)\n {\n int newRow = currRow + rowDir[dirIdx], newCol = currCol + colDir[dirIdx];\n if (!isValid(visited, newRow, newCol)) continue;\n \n visited[newRow][newCol] = true;\n q.push({newRow, newCol});\n }\n }\n dist++;\n }\n //==================================================================================\n //BINARY SEARCH\n int low = 0, high = INT_MAX;\n int ans = 0;\n while(low <= high)\n {\n int mid = low + (high - low) / 2;\n if (isSafe(distToTheif, mid))\n {\n ans = mid;\n low = mid + 1;\n }\n else high = mid - 1;\n }\n //=========================================================================================\n return ans;\n \n }\n};", "memory": "213138" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n \n bool safe(int r, int c, int n) {\n return(r>=0 && c>=0 && r<n && c<n);\n }\n \n vector<int> x = {-1, 1, 0, 0};\n vector<int> y = {0, 0, -1, 1};\n \n class Comp {\n public:\n bool operator() (vector<int> &a, vector<int> &b) {\n return a[0]<b[0];\n }\n };\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n \n queue<pair<int, int>> q;\n \n for(int i=0;i<n;i++) {\n for(int j=0;j<n;j++) {\n if(grid[i][j]==1) {\n q.push(make_pair(i, j));\n grid[i][j] = 0;\n }\n else {\n grid[i][j] = 1e4;\n }\n }\n }\n int step = 1;\n while(!q.empty()) {\n int l = q.size();\n while(l--) {\n int row = q.front().first;\n int col = q.front().second; \n q.pop();\n for(int i=0;i<4;i++) {\n int xp = row+x[i];\n int yp = col+y[i];\n if(safe(xp, yp, n) && grid[xp][yp]==1e4) {\n grid[xp][yp] = step;\n q.push(make_pair(xp, yp));\n }\n }\n }\n step++;\n }\n \n // for(int i=0;i<n;i++) {\n // for(int j=0;j<n;j++) cout << grid[i][j] << \" \";\n // cout << endl;\n // }\n // cout << \"--\" << endl;\n \n // done with filling\n vector<vector<int>> dp(n, vector<int> (n, -1));\n dp[0][0] = grid[0][0];\n priority_queue<vector<int>, vector<vector<int>>, Comp> pq;\n pq.push({dp[0][0], 0, 0});\n \n while(!pq.empty()) {\n int row = pq.top()[1];\n int col = pq.top()[2];\n int dist = pq.top()[0];\n // cout << row << \" \" << col << \" \" << dist << endl;\n pq.pop();\n for(int i=0;i<4;i++) {\n int xp = row+x[i];\n int yp = col+y[i];\n if(safe(xp, yp, n)) {\n if(dp[xp][yp]==-1) {\n dp[xp][yp] = min(dist, grid[xp][yp]);\n pq.push({dp[xp][yp], xp, yp});\n }\n // else {\n // if(dp[xp][yp]<min(dist, grid[xp][yp])) {\n // dp[xp][yp] = min(dist, grid[xp][yp]);\n // pq.push({dp[xp][yp], xp, yp});\n // }\n // }\n }\n }\n }\n \n // cout << \"--\" << endl;\n // for(int i=0;i<n;i++) {\n // for(int j=0;j<n;j++) cout << dp[i][j] << \" \";\n // cout << endl;\n // }\n return dp[n-1][n-1];\n \n }\n};", "memory": "217821" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n const int dirs[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n const int M = grid.size();\n const int N = M == 0 ? 0 : grid[0].size();\n vector< vector<int> > safeness(M, vector<int>(N, -1));\n queue< pair<int, int> > que;\n int safe = 1;\n vector< vector<int> > visited(M, vector<int>(N));\n priority_queue< vector<int> > maxHeap;\n\n for (int i = 0; i < M; ++i) {\n for (int j = 0; j < N; ++j) {\n if (grid[i][j] == 1) {\n que.emplace(i, j);\n safeness[i][j] = 0;\n }\n }\n }\n\n while (!que.empty()) {\n for (int i = que.size(); i > 0; --i) {\n auto p = que.front();\n auto x = p.first;\n auto y = p.second;\n\n que.pop();\n\n for (const auto& dir : dirs) {\n auto dx = x + dir[0];\n auto dy = y + dir[1];\n\n if (dx >= 0 && dx < M && dy >= 0 && dy < N && safeness[dx][dy] == -1) {\n safeness[dx][dy] = safe;\n que.emplace(dx, dy);\n }\n }\n }\n\n ++safe;\n }\n\n maxHeap.push({safeness[0][0], 0, 0});\n\n while (!maxHeap.empty()) {\n auto v = maxHeap.top();\n auto d = v[0];\n auto x = v[1];\n auto y = v[2];\n\n maxHeap.pop();\n\n if (x + 1 == M && y + 1 == N) {\n return d;\n }\n\n if (visited[x][y] != 0) {\n continue;\n }\n visited[x][y] = 1;\n\n for (const auto& dir : dirs) {\n auto dx = x + dir[0];\n auto dy = y + dir[1];\n\n if (dx >= 0 && dx < M && dy >= 0 && dy < N && visited[dx][dy] == 0) {\n maxHeap.push({min(d, safeness[dx][dy]), dx, dy});\n }\n }\n }\n\n return -1;\n }\n};", "memory": "217821" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n typedef pair<int, pair<int, int>> ppi;\n\n int n;\n vector<int> delR = {-1, 0, 1, 0}, delC = {0, 1, 0, -1};\n\n bool isValid(int row, int col){\n return row >= 0 && row < n && col >= 0 && col < n;\n }\n\n void bfs(vector<vector<int>>& grid, vector<vector<int>>& dist){\n // Dijsktra Algo - Shortest path from thief to each cell;\n\n set<ppi> st;\n \n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j]){\n st.insert({0, {i, j}});\n dist[i][j] = 0;\n } \n }\n }\n\n while(!st.empty()){\n auto it = *(st.begin());\n int row = it.second.first;\n int col = it.second.second;\n int dis = it.first;\n st.erase(it);\n\n for(int i = 0; i < 4; i++){\n int nRow = row + delR[i];\n int nCol = col + delC[i];\n \n int totalD = 1 + dis;\n if(isValid(nRow, nCol) && totalD < dist[nRow][nCol]){\n int adjD = dist[nRow][nCol];\n\n if(adjD != INT_MAX) st.erase({adjD, {nRow, nCol}});\n\n dist[nRow][nCol] = totalD;\n st.insert({totalD, {nRow, nCol}});\n } \n }\n }\n }\n\n int maxSafe(vector<vector<int>>& dist){\n // Dijsktra Algo using maxHeap\n\n priority_queue<ppi> maxH;\n vector<vector<int>> vis(n, vector<int>(n, 0));\n\n maxH.push({dist[0][0], {0, 0}});\n vis[0][0] = 1;\n\n while(!maxH.empty()){\n auto it = maxH.top();\n maxH.pop();\n int safeD = it.first;\n int row = it.second.first;\n int col = it.second.second;\n\n if(row == n-1 && col == n-1) return safeD;\n\n for(int i = 0; i < 4; i++){\n int nRow = row + delR[i];\n int nCol = col + delC[i];\n\n if(isValid(nRow, nCol) && !vis[nRow][nCol]){\n int s = dist[nRow][nCol];\n int safe = min(s, safeD);\n vis[nRow][nCol] = 1;\n maxH.push({safe, {nRow, nCol}});\n }\n }\n }\n\n return -1;\n }\n\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n // Solved By KS\n\n n = grid.size();\n\n if(grid[0][0] || grid[n-1][n-1]) return 0;\n vector<vector<int>> dist(n, vector<int>(n, INT_MAX));\n\n bfs(grid, dist);\n\n return maxSafe(dist);\n }\n};", "memory": "222503" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int delrow[4] = {-1,0,+1,0};\n int delcol[4] = {0,+1,0,-1};\n\n bool isValid(int row,int col,vector<vector<int>> &grid){\n int n = grid.size();\n if(row >= 0 && row < n && col >= 0 && col < n){\n return true;\n }\n return false;\n }\n\n bool helper(int row,int col,int factor,vector<vector<int>> &maxsafe,vector<vector<int>> &grid,vector<vector<int>> &visited){\n int n = grid.size();\n if(maxsafe[row][col] < factor){\n return false;\n }\n if(row == n -1 && col == n -1){\n return true;\n }\n visited[row][col] = 1;\n\n for(int i = 0;i<4;i++){\n int nrow = row + delrow[i];\n int ncol = col + delcol[i];\n if(isValid(nrow,ncol,grid) && maxsafe[nrow][ncol] >= factor && visited[nrow][ncol] == 0){\n bool b = helper(nrow,ncol,factor,maxsafe,grid,visited);\n if(b){\n return true;\n }\n }\n }\n return false;\n }\n\n void calculateMaxSafeness(vector<vector<int>> &maxsafe,vector<vector<int>>& grid){\n int n = maxsafe.size();\n queue<pair<int,int>> q;\n for(int i = 0;i<n;i++){\n for(int j = 0;j<n;j++){\n if(grid[i][j] == 1){\n maxsafe[i][j] = 0;\n q.push({i,j});\n }\n else{\n maxsafe[i][j] = -1;\n }\n }\n }\n while(!q.empty()){\n auto front = q.front();\n int row = front.first;\n int col = front.second;\n q.pop();\n for(int i = 0;i<4;i++){\n int nrow = row + delrow[i];\n int ncol = col + delcol[i];\n if(isValid(nrow,ncol,grid) && maxsafe[nrow][ncol] == -1){\n maxsafe[nrow][ncol] = maxsafe[row][col] + 1;\n q.push({nrow,ncol});\n }\n }\n\n }\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n int low = 0;\n int high = n - 1;\n vector<vector<int>> maxsafe(n,vector<int>(n));\n calculateMaxSafeness(maxsafe,grid);\n\n while(low <= high){\n int mid = (high + low)/2;\n vector<vector<int>> visited(n,vector<int>(n,0));\n if(helper(0,0,mid,maxsafe,grid,visited)){\n low = mid + 1;\n }\n else{\n high = mid - 1;\n }\n }\n return high;\n }\n};", "memory": "222503" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n const int dirs[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n const int M = grid.size();\n const int N = M == 0 ? 0 : grid[0].size();\n vector< vector<int> > safeness(M, vector<int>(N, -1));\n queue< pair<int, int> > que;\n int safe = 1;\n vector< vector<int> > dist(M, vector<int>(N, -1));\n vector< vector<int> > visited(M, vector<int>(N));\n priority_queue< vector<int> > maxHeap;\n\n for (int i = 0; i < M; ++i) {\n for (int j = 0; j < N; ++j) {\n if (grid[i][j] == 1) {\n que.emplace(i, j);\n safeness[i][j] = 0;\n }\n }\n }\n\n while (!que.empty()) {\n for (int i = que.size(); i > 0; --i) {\n auto p = que.front();\n auto x = p.first;\n auto y = p.second;\n\n que.pop();\n\n for (const auto& dir : dirs) {\n auto dx = x + dir[0];\n auto dy = y + dir[1];\n\n if (dx >= 0 && dx < M && dy >= 0 && dy < N && safeness[dx][dy] == -1) {\n safeness[dx][dy] = safe;\n que.emplace(dx, dy);\n }\n }\n }\n\n ++safe;\n }\n\n dist[0][0] = safeness[0][0];\n maxHeap.push({safeness[0][0], 0, 0});\n\n while (!maxHeap.empty()) {\n auto v = maxHeap.top();\n auto d = v[0];\n auto x = v[1];\n auto y = v[2];\n\n maxHeap.pop();\n\n if (x + 1 == M && y + 1 == N) {\n return d;\n }\n\n if (visited[x][y] != 0) {\n continue;\n }\n visited[x][y] = 1;\n\n for (const auto& dir : dirs) {\n auto dx = x + dir[0];\n auto dy = y + dir[1];\n\n if (dx >= 0 && dx < M && dy >= 0 && dy < N && visited[dx][dy] == 0) {\n dist[dx][dy] = min(d, safeness[dx][dy]);\n maxHeap.push({dist[dx][dy], dx, dy});\n }\n }\n }\n\n return -1;\n }\n};", "memory": "227186" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n int rows;\n int cols;\n vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n bool isValidCell(int x, int y) {\n return (x >= 0 && x < rows && y >= 0 && y < cols);\n }\n\n bool bfsPathExists(vector<vector<int>>& distFromThief,\n int& minPermittedSafeFactor) {\n if (distFromThief[0][0] < minPermittedSafeFactor ||\n distFromThief[rows - 1][cols - 1] < minPermittedSafeFactor) {\n return false;\n }\n vector<vector<bool>> visited(rows, vector<bool>(cols, false));\n queue<pair<int, int>> q;\n q.push({0, 0});\n while (!q.empty()) {\n pair<int, int> p = q.front();\n int x = p.first;\n int y = p.second;\n q.pop();\n visited[x][y] = true;\n if (x == rows - 1 && y == cols - 1) {\n return true;\n }\n for (auto direction : directions) {\n int nextX = x + direction.first;\n int nextY = y + direction.second;\n if (!isValidCell(nextX, nextY) || visited[nextX][nextY] ||\n distFromThief[nextX][nextY] < minPermittedSafeFactor) {\n continue;\n }\n q.push(make_pair(nextX, nextY));\n }\n }\n return false;\n }\n\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int tr = grid.size();\n int tc = grid[0].size();\n rows = tr;\n cols = tc;\n vector<vector<int>> distFromThief(tr, vector<int>(tc, INT_MAX));\n queue<pair<int, int>> q;\n for (int i = 0; i < tr; i++) {\n for (int j = 0; j < tc; j++) {\n if (grid[i][j] == 1) {\n q.push(make_pair(i, j));\n }\n }\n }\n int maxSafeFactor = 0;\n int dist = 0;\n while (!q.empty()) {\n int qs = q.size();\n maxSafeFactor = dist;\n while (qs--) {\n pair<int, int> p = q.front();\n int x = p.first;\n int y = p.second;\n q.pop();\n if (isValidCell(x, y) == false || dist >= distFromThief[x][y]) {\n continue;\n }\n distFromThief[x][y] = dist;\n q.push(make_pair(x + 1, y));\n q.push(make_pair(x - 1, y));\n q.push(make_pair(x, y + 1));\n q.push(make_pair(x, y - 1));\n }\n dist++;\n }\n // cout << \"bfs complete\" << endl;\n\n priority_queue<vector<int>>pq;\n pq.push({distFromThief[0][0],0,0});\n while(!pq.empty()) {\n vector<int> curr = pq.top();\n pq.pop();\n if(curr[1]==rows-1 && curr[2]==cols-1) {\n return curr[0];\n }\n for(pair<int, int> p: directions) {\n int x = curr[1] + p.first;\n int y = curr[2] + p.second;\n if(isValidCell(x,y) && distFromThief[x][y]!=-1) {\n pq.push({min(curr[0], distFromThief[x][y]), x, y});\n distFromThief[x][y] = -1;\n }\n }\n }\n return -1;\n // int lo = 0;\n // int hi = maxSafeFactor;\n // int mid = lo + (hi - lo) / 2;\n // int ans = 0;\n // while (lo <= hi) {\n // mid = lo + (hi - lo) / 2;\n // if (bfsPathExists(distFromThief, mid)) {\n // ans = mid;\n // lo = mid + 1;\n // } else {\n // hi = mid - 1;\n // }\n // }\n // return ans;\n }\n};", "memory": "227186" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> dr = {0, 0, 1, -1};\n vector<int> dc = {1, -1, 0, 0};\n\n bool isValidDist(int n, int i, int j) {\n return (i >= 0 && i < n && j >= 0 && j < n);\n }\n\n bool isValidSafe(vector<vector<int>>& visited, int n, int i, int j, int safe, vector<vector<int>>& dist) {\n return (i >= 0 && i < n && j >= 0 && j < n && !visited[i][j] && dist[i][j] >= safe);\n }\n\n bool isPossible(vector<vector<int>>& dist, int safe, int n) {\n if (dist[0][0] < safe)\n return false;\n\n queue<pair<int,int>> q;\n q.push({0, 0});\n\n vector<vector<int>> visited(n, vector<int>(n, 0));\n visited[0][0] = 1;\n\n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n\n if (x == n - 1 && y == n - 1)\n return true;\n\n for (int i = 0; i < 4; i++) {\n int new_x = x + dr[i];\n int new_y = y + dc[i];\n\n if (isValidSafe(visited, n, new_x, new_y, safe, dist)) {\n q.push({new_x, new_y});\n visited[new_x][new_y] = 1;\n }\n }\n }\n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n\n vector<vector<int>> dist(n, vector<int>(n, -1));\n queue<pair<int, int>> q;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n dist[i][j] = 0;\n }\n }\n }\n\n int len = 1;\n while (!q.empty()) {\n int size = q.size();\n for (int i = 0; i < size; i++) {\n auto [x, y] = q.front();\n q.pop();\n\n for (int j = 0; j < 4; j++) {\n int new_x = x + dr[j];\n int new_y = y + dc[j];\n\n if (isValidDist(n, new_x, new_y) && dist[new_x][new_y] == -1) {\n dist[new_x][new_y] = len;\n q.push({new_x, new_y});\n }\n }\n }\n len++;\n }\n\n int low = 0, high = len - 1; \n int ans = 0;\n\n while (low <= high) {\n int mid = low + (high - low) / 2;\n if (isPossible(dist, mid, n)) {\n ans = mid;\n low = mid + 1;\n } else {\n high = mid - 1;\n }\n }\n return ans;\n }\n};\n", "memory": "231868" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> dx {0,1,0,-1};\n vector<int> dy {1,0,-1,0};\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size() , m = grid[0].size() ;\n if (grid[0][0] || grid[n-1][m-1]) return 0 ;\n queue<pair<int,int>> q;\n for(int i = 0 ; i < n ; i++) \n for(int j = 0 ; j < m ; j++) \n if (grid[i][j])\n q.push({i,j}) ;\n while(q.size()){\n int sz = q.size() ;\n while (sz--){\n auto [x,y] = q.front() ; q.pop() ;\n for(int i = 0 ; i < 4; i++){\n int nx = x + dx[i] , ny = y + dy[i] ;\n if (nx >= 0 && ny >= 0 && nx < n && ny < m && !grid[nx][ny])\n {\n grid[nx][ny] = grid[x][y] + 1 ; \n q.push({nx,ny}) ;\n }\n } \n }\n }\n \n auto fx = [&] (int k){\n q = queue<pair<int,int>> () ;\n vector<vector<int>> vis (n , vector<int> (m)) ;\n if (grid[0][0]-1 < k) return 0 ;\n q.push({0,0});\n vis[0][0] = 1 ;\n while(q.size()){\n auto [x,y] = q.front() ; q.pop() ;\n if (x == n-1 && y == m-1) return 1;\n \n for(int i = 0 ; i < 4; i++){\n int nx = x + dx[i] , ny = y + dy[i] ;\n if (nx >= 0 && ny >= 0 && nx < n && ny < m && !vis[nx][ny] && grid[nx][ny]-1 >= k)\n {\n q.push({nx,ny}) ;\n vis[nx][ny] = 1 ; \n }\n } \n }\n return 0 ;\n };\n\n int lo = 0 , hi = 400 ;\n while(lo <= hi){\n int mid = (lo + hi)>>1;\n if (fx(mid)) lo = mid+1 ;\n else hi = mid-1 ;\n }\n return hi ;\n }\n};", "memory": "231868" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool dfs(int i,int j,int leastSafety,vector<vector<int>>& safetyFactor,int n,vector<vector<int>>& vis)\n {\n vis[i][j] = 1;\n if(i== n-1 && j == n-1 && safetyFactor[i][j] >= leastSafety)\n {\n return true;\n }\n if(safetyFactor[i][j] < leastSafety)\n {\n return false;\n }\n int dir_r[] = {1,0,-1,0};\n int dir_c[] = {0,1,0,-1};\n\n for(int x = 0; x < 4; x++)\n {\n int n_row = i + dir_r[x];\n int n_col = j + dir_c[x];\n\n if(n_row >= 0 && n_row < n && n_col >= 0 && n_col < n\n && !vis[n_row][n_col] && dfs(n_row,n_col,leastSafety,safetyFactor,n,vis) == true)\n {\n return true;\n }\n }\n\n return false;\n }\n bool possible(int leastSafety,vector<vector<int>>& safetyFactor,int n)\n {\n vector<vector<int>> vis(n+1,vector<int>(n+1,0));\n return dfs(0,0,leastSafety,safetyFactor,n,vis);\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> safetyFactor(n,vector<int>(n,-1));\n\n // BFS to calculate the safetyFactor for each cell as minimum manahatten distance \n //from a cell to any thief in the grid is the safety factor\n\n queue<pair<pair<int,int>,int>> q;\n\n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j < n; j++)\n {\n if(grid[i][j] == 1)\n {\n q.push({{i,j},0});\n safetyFactor[i][j] = 0;\n }\n }\n }\n // O(n*n)\n\n // BFS\n int dir_r[] = {1,0,-1,0};\n int dir_c[] = {0,1,0,-1};\n while(!q.empty())\n {\n auto node = q.front();\n q.pop();\n\n for(int i = 0; i < 4; i++)\n {\n int n_row = node.first.first + dir_r[i];\n int n_col = node.first.second + dir_c[i];\n\n if(n_row >= 0 && n_row < n && n_col >= 0 && n_col < n\n && safetyFactor[n_row][n_col] == -1)\n {\n safetyFactor[n_row][n_col] = node.second + 1;\n q.push({{n_row,n_col},safetyFactor[n_row][n_col]});\n }\n }\n }\n // O(n*n) traversed all cell \n // Space - O(n*n)\n\n // now we will perform binary search on answer to get\n // maximum safetyFactor to reach n-1,n-1 from 0,0\n int low = 0;\n int high = 2*n; // max possible safety factor as maximum manhatten distance could be 2*(n-1) //\n int ans = -1;\n while(low <= high) // O(log(2*n))\n {\n int mid = low + high >> 1;\n\n if(possible(mid,safetyFactor,n)) // O(n*n)\n {\n ans = mid;\n low = mid+1;\n }\n else\n {\n high = mid-1;\n }\n }\n\n // TC - O(n*n) --> BFS Traversal + O(n*n *(log(2*n))) --> dfs for each iteration of binary search\n // ==> O(n*2) + O(n*2*logn)\n // SC - O(n*n)\n return ans;\n }\n};", "memory": "236551" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> dx {0,1,0,-1};\n vector<int> dy {1,0,-1,0};\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size() , m = grid[0].size() ;\n if (grid[0][0] || grid[n-1][m-1]) return 0 ;\n queue<pair<int,int>> q;\n for(int i = 0 ; i < n ; i++) \n for(int j = 0 ; j < m ; j++) \n if (grid[i][j])\n q.push({i,j}) ;\n while(q.size()){\n int sz = q.size() ;\n while (sz--){\n auto [x,y] = q.front() ; q.pop() ;\n for(int i = 0 ; i < 4; i++){\n int nx = x + dx[i] , ny = y + dy[i] ;\n if (nx >= 0 && ny >= 0 && nx < n && ny < m && !grid[nx][ny])\n {\n grid[nx][ny] = grid[x][y] + 1 ; \n q.push({nx,ny}) ;\n }\n } \n }\n }\n for(auto I: grid) {\n for(auto j : I) cout << j <<\" \" ;\n cout << endl;\n }\n auto fx = [&] (int k){\n q = queue<pair<int,int>> () ;\n vector<vector<int>> vis (n , vector<int> (m)) ;\n if (grid[0][0]-1 < k) return 0 ;\n q.push({0,0});\n vis[0][0] = 1 ;\n while(q.size()){\n auto [x,y] = q.front() ; q.pop() ;\n if (x == n-1 && y == m-1) return 1;\n \n for(int i = 0 ; i < 4; i++){\n int nx = x + dx[i] , ny = y + dy[i] ;\n if (nx >= 0 && ny >= 0 && nx < n && ny < m && !vis[nx][ny] && grid[nx][ny]-1 >= k)\n {\n q.push({nx,ny}) ;\n vis[nx][ny] = 1 ; \n }\n } \n }\n return 0 ;\n };\n\n int lo = 0 , hi = 400 ;\n while(lo <= hi){\n int mid = (lo + hi)>>1;\n if (fx(mid)) lo = mid+1 ;\n else hi = mid-1 ;\n }\n return hi ;\n }\n};", "memory": "236551" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n/*\n Nice: think in the reverse direction i.e. in order to find the shortest \n distance for each cell to to theives (1) whcih will result in tc (n^2*m^2) , \nthink in the opposite fashion i.e. try to start from 1s and reach every 0s in\nminimum possible path using multi source BFS in (n*m) time\n\nalgo:\n1. precompute the shortest distance of each cell(0) to the nearest theif\n2. do binary search on answer i.e disatance [0...800]\n3. check function: is it possible to reach d from s with mid as the safness factor\n condition: shortest dist val at each cell>= safeness factor else it will act as the blockage\n4. print the maximum safeness factor ans the answer\n\n*/\n vector<int>dr={0,0,-1,1}, dc= {1,-1,0,0}; \n int calMinShortestDist(vector<vector<int>>&grid, vector<vector<int>>&shortestPath){\n int n= grid.size();\n queue< pair<int, int>>q;// dist,{r,c}\n vector<vector<int>>vis(n, vector<int>(n,0));\n // push all the thieves into the queue\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(grid[i][j]==1) {\n q.push({i,j});\n vis[i][j]=1;\n }\n }\n }\n int len=0;\n// Multi sources BFS\n while(!q.empty()){\n int size = q.size();\n while(size--){\n auto curr = q.front();\n q.pop(); \n int i= curr.first;\n int j= curr.second;\n shortestPath[i][j]= len;\n \n for(int d=0; d<4; d++){\n int ni = i + dr[d], nj = j + dc[d];\n if(ni>=0 && ni<n && nj>=0 && nj<n && vis[ni][nj]==0) {\n q.push({ni,nj});\n vis[ni][nj]=1;\n }\n }\n }\n len++;\n } \n return len;\n \n }\n\n\n bool isPossible(int safenessFactor, vector<vector<int>>&grid, vector<vector<int>>& shortestPath) {\n if (shortestPath[0][0] < safenessFactor) return false;\n int n= grid.size();\n queue<pair<int, int>> q;\n q.push({0, 0});\n vector<vector<int>> vis(n, vector<int>(n, 0));\n vis[0][0] = 1;\n\n while (!q.empty()) {\n auto [i, j] = q.front();\n q.pop();\n\n if (i == n - 1 && j == n - 1) return true;\n\n for (int d = 0; d < 4; d++) {\n int ni = i + dr[d], nj = j + dc[d];\n if (ni >= 0 && ni < n && nj >= 0 && nj < n && !vis[ni][nj] && shortestPath[ni][nj] >= safenessFactor) {\n vis[ni][nj] = 1;\n q.push({ni, nj});\n }\n }\n }\n\n return false;\n}\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>>shortestPath(n, vector<int>(n,1e5));\n int len = calMinShortestDist(grid,shortestPath);\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n cout<<shortestPath[i][j]<<\" \";\n }cout<<\"\\n\";\n }\n int l=0, hi= len, ans=0;\n while(l<=hi){\n int mid = l + (hi-l)/2; \n if(isPossible(mid,grid,shortestPath)) {\n ans = mid;\n l= mid +1;\n }\n else hi= mid-1;\n }\n // if(isPossible(0,grid,shortestPath)) cout<<\"yes\";\n // if(isPossible(3,grid,shortestPath)) cout<<\"yes\";\n // if(isPossible(0,grid,shortestPath)) cout<<\"yes\";\n // else cout<<\"no\";\n return ans;\n }\n};", "memory": "241233" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(vector<vector<int>>& grid, int mid, vector<vector<int>> &safe) {\n if (safe[0][0] < mid) return false;\n int n = grid.size();\n queue<pair<int, int>> q;\n q.push({0, 0});\n vector<vector<int>> vis(n, vector<int>(n, 0));\n vis[0][0] = 1;\n\n vector<int> drow = {-1, 0, 1, 0};\n vector<int> dcol = {0, 1, 0, -1};\n\n while (!q.empty()) {\n int sizee = q.size();\n for(int i = 0; i < sizee; i++) {\n int r = q.front().first;\n int c = q.front().second;\n if (r == n-1 && c == n-1) return true;\n q.pop();\n\n for(int a = 0; a < 4; a++) {\n int nr = r + drow[a];\n int nc = c + dcol[a];\n\n if (nr >= 0 && nr < n && nc >= 0 && nc < n) {\n if (vis[nr][nc] == 0 && safe[nr][nc] >= mid) {\n vis[nr][nc] = 1;\n q.push({nr, nc});\n }\n }\n }\n }\n }\n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n int maxi = INT_MIN;\n vector<vector<int>> safeness(n, vector<int>(n, 1e9));\n queue<pair<int, pair<int, int>>> q;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n safeness[i][j] = 0;\n q.push({0, {i, j}});\n }\n }\n }\n vector<int> drow = {-1, 0, 1, 0};\n vector<int> dcol = {0, 1, 0, -1};\n while (!q.empty()) {\n int sizee = q.size();\n for(int i = 0; i < sizee; i++) {\n int dis = q.front().first;\n int r = q.front().second.first;\n int c = q.front().second.second;\n q.pop();\n\n for(int a = 0; a < 4; a++) {\n int nr = r + drow[a];\n int nc = c + dcol[a];\n if (nr >= 0 && nr < n && nc >= 0 && nc < n) {\n if (safeness[nr][nc] > dis + 1) {\n safeness[nr][nc] = dis + 1;\n maxi = max(maxi, dis + 1);\n q.push({dis + 1, {nr, nc}});\n }\n }\n }\n }\n }\n\n int start = 0;\n int end = maxi;\n int ans = 0;\n while (start <= end) {\n int mid = (start + end)/2;\n if (solve(grid, mid, safeness)) {\n ans = mid;\n start = mid + 1;\n }\n else end = mid - 1;\n }\n return ans;\n }\n};", "memory": "241233" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n int dx[4]={0,1,0,-1};\n int dy[4]={1,0,-1,0};\n\n bool possible(int val ,vector<vector<int>>& grid )\n {\n queue<pair<int,int>> q;\n int n=grid.size();\n if(grid[0][0]>=val)\n q.push({0,0});\n vector<vector<int>> vis(n, vector<int>(n,0));\n int x, y;\n\n while(!q.empty())\n {\n x=q.front().first;\n y=q.front().second;\n q.pop();\n if(vis[x][y]==1)continue;\n vis[x][y]=1;\n if(x==n-1 &&y==n-1)\n return true;\n\n for(int i=0;i<4;i++)\n {\n int newx=x+dx[i];\n int newy=y+dy[i];\n if(newx<0 || newx>=n || newy<0 || newy>=n)\n continue;\n if(vis[newx][newy]==0 && grid[newx][newy]>=val)\n { \n q.push({newx,newy});\n }\n }\n }\n \n return false;\n }\n int func(vector<vector<int>>& grid)\n {\n\n int n=grid.size();\n vector<vector<int>> visited(n,vector<int>(n,INT_MAX));\n queue<pair<pair<int,int>,int>> pq;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n {\n if(grid[i][j]==1)\n {\n pq.push({{i,j},0});\n visited[i][j]=0;\n }\n }\n while(!pq.empty())\n {\n pair<pair<int,int>,int> temp = pq.front();\n pq.pop();\n int x = temp.first.first;\n int y = temp.first.second;\n int dis = temp.second;\n \n for(int i=0;i<4;i++)\n {\n int newx = dx[i]+x;\n int newy = dy[i]+y;\n if(newx<0 || newx>=n || newy<0 || newy>=n)\n continue;\n if(visited[newx][newy]>dis+1)\n {\n visited[newx][newy]=dis+1;\n pq.push({{newx,newy},dis+1});\n }\n }\n }\n\n int low = 0;\n int high = 2*(n-1) ;\n int mid;\n while(low<=high)\n {\n \n mid=(low+high)>>1;\n if(possible(mid, visited))\n {\n low=mid+1;\n }\n else \n {\n\n high=mid-1;\n }\n }\n return high;\n \n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n if(grid[0][0]==1||grid[grid.size()-1][grid.size()-1]==1)return 0;\n return func(grid);\n return 0;\n }\n};", "memory": "245916" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n if(grid[0][0] == 1 || grid[n-1][m-1] == 1){\n return 0;\n }\n\n vector<vector<int>> dis(n,vector<int>(m,-1));\n priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>> > pq;\n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j] == 1){\n pq.push({0,{i,j}});\n }\n }\n }\n\n while(!pq.empty()){\n auto it = pq.top();\n pq.pop();\n int val = it.first;\n int row =it.second.first;\n int col = it.second.second;\n\n\n\n if(dis[row][col] == -1){\n dis[row][col] = val;\n vector<int> row_vec = {0,0,1,-1};\n vector<int> col_vec = {1,-1,0,0};\n\n\n for(int i=0;i<4;i++){\n int nr = row + row_vec[i];\n int nc = col + col_vec[i];\n\n if(nr>=0 && nr<n && nc>=0 && nc<m && dis[nr][nc] == -1){\n pq.push({val+1,{nr,nc}});\n }\n }\n }\n }\n\n priority_queue<pair<int,pair<int,int>> > spq;\n int ans =dis[0][0];\n\n spq.push({dis[0][0],{0,0}});\n \n\n \n\n while(!spq.empty()){\n auto it = spq.top();\n spq.pop();\n int val = it.first;\n int row =it.second.first;\n int col = it.second.second;\n if(dis[row][col] == -1){\n continue;\n }\n ans = min(ans,val);\n dis[row][col] = -1;\n if(row == n-1 && col==m-1 || ans==0){\n return ans;\n }\n \n\n vector<int> row_vec = {0,0,1,-1};\n vector<int> col_vec = {1,-1,0,0};\n\n for(int i=0;i<4;i++){\n int nr = row + row_vec[i];\n int nc = col + col_vec[i];\n\n if(nr>=0 && nr<n && nc>=0 && nc<m && dis[nr][nc] != -1){\n spq.push({dis[nr][nc],{nr,nc}});\n }\n }\n\n\n\n }\n\n return ans;\n\n\n \n }\n};", "memory": "274011" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n // Do BFS from all the 1s\n vector<vector<int>> distances(n, vector<int>(n, INT_MAX));\n queue<pair<int, int>> q;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(grid[i][j] == 1) {\n q.push({i, j});\n }\n }\n }\n int level = 0;\n int directions[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n while(!q.empty()) {\n int size = q.size();\n for(int i = 0; i < size; i++) {\n auto front = q.front();\n q.pop();\n if(distances[front.first][front.second] != INT_MAX) continue;\n distances[front.first][front.second] = level;\n for(auto direction: directions) {\n int newRow = front.first + direction[0];\n int newCol = front.second + direction[1];\n if(newRow >= 0 && newRow < n && newCol >= 0 && newCol < n && distances[newRow][newCol] == INT_MAX) {\n q.push({newRow, newCol});\n }\n }\n }\n level++;\n }\n\n // Do Dijkstra's Algorithm\n priority_queue<vector<int>> pq;\n pq.push({distances[0][0], 0, 0});\n int res = INT_MAX;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n while(!pq.empty()) {\n auto top = pq.top();\n pq.pop();\n int dist = top[0], r = top[1], c = top[2];\n if(visited[r][c]) continue;\n res = min(dist, res);\n if(r == n - 1 && c == n - 1) return res;\n visited[r][c] = true;\n\n // Push all its valid neighbors to the max-heap\n for(auto direction: directions) {\n int newRow = r + direction[0];\n int newCol = c + direction[1];\n if(newRow >= 0 && newRow < n && newCol >= 0 && newCol < n && !visited[newRow][newCol]) {\n pq.push({distances[newRow][newCol], newRow, newCol});\n }\n }\n }\n return -1;\n }\n};", "memory": "274011" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int is_path(vector<vector<int>> &m_dist, int safeness_factor){\n int m = m_dist.size();\n int n = m_dist[0].size();\n queue<pair<int, int>> q;\n vector<vector<int>> vis(m, vector<int>(n, 0));\n if(m_dist[0][0] >= safeness_factor){\n q.push({0, 0});\n vis[0][0] = true;\n }\n \n vector<vector<int>> temp = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n \n \n while(!q.empty()){\n int x = q.front().first;\n int y = q.front().second;\n q.pop();\n if(x == m-1 && y == n-1){\n return true;\n }\n for(int i = 0; i < 4; i++){\n int new_x = x + temp[i][0];\n int new_y = y + temp[i][1];\n // int diff = ;\n if(new_x >= 0 && new_x < m && new_y >= 0 && new_y < n && !vis[new_x][new_y] && m_dist[new_x][new_y] >= safeness_factor){\n q.push({new_x, new_y});\n vis[new_x][new_y] = 1;\n }\n }\n }\n return false;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n queue<pair<int, int>> q;\n vector<vector<int>>m_dist(m, vector<int>(n, 1e9));\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j] == 1){\n q.push({i, j});\n m_dist[i][j] = 0;\n }\n }\n }\n vector<vector<int>> vis(m, vector<int>(n, 0));\n vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n while(!q.empty()){\n // int size = q.size();\n // while(size--){\n int r = q.front().first;\n int c = q.front().second;\n // int dist = q.front().second;\n // m_dist[r][c] = dist;\n q.pop();\n for(int i = 0; i < 4; i++){\n int new_r = r + dir[i][0];\n int new_c = c + dir[i][1];\n if(new_r >= 0 && new_c >= 0 && new_r < m && new_c < n && grid[new_r][new_c] == 0 && !vis[new_r][new_c]){\n q.push({new_r, new_c});\n vis[new_r][new_c] = 1;\n m_dist[new_r][new_c] = m_dist[r][c] + 1;\n }\n }\n // }\n }\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n cout<<m_dist[i][j]<<\" \";\n }\n cout<<endl;\n }\n // for(int i = n-1; i >= 0; i--){\n // int safeness_factor = i;\n // if(is_path(m_dist, safeness_factor)){\n // return safeness_factor;\n // }\n // }\n \n // Binary search\n int low = 0, high = n, ans;\n while(low <= high){\n int mid = (low + high)/2;\n int safeness_factor = mid;\n if(is_path(m_dist, safeness_factor)){\n ans = safeness_factor;\n low = mid + 1;\n }\n else{\n high = mid - 1;\n }\n }\n return ans;\n }\n};", "memory": "278693" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n queue<vector<int>> q;\n int n = grid.size();\n vector<vector<bool>> seen(n, vector<bool>(n));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j, 0});\n grid[i][j] = 0;\n seen[i][j] = true;\n }\n }\n }\n vector<vector<int>> dir = {{0,1}, {1,0}, {0,-1}, {-1,0}};\n while (!q.empty()) {\n int x = q.front()[0];\n int y = q.front()[1];\n int c = q.front()[2];\n q.pop();\n for (const auto& d : dir) {\n if (x + d[0] < 0 || x + d[0] >= n) continue;\n if (y + d[1] < 0 || y + d[1] >= n) continue;\n if (seen[x + d[0]][y + d[1]]) continue;\n seen[x + d[0]][y + d[1]] = true;\n grid[x + d[0]][y + d[1]] = c+1;\n q.push({x + d[0], y + d[1], c+1});\n }\n }\n auto comp = [] (vector<int>& a, vector<int>& b) { return a[0] < b[0]; };\n priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> pq(comp);\n\n //insert frist\n //keep on getting the highest one available, once end is reached you have the score\n pq.push({grid[0][0], 0, 0});\n seen[0][0] = false;\n int current_score = grid[0][0];\n while (!pq.empty()) {\n int x = pq.top()[1];\n int y = pq.top()[2];\n current_score = min(current_score, grid[x][y]);\n pq.pop();\n for (const auto& d : dir) {\n if (x + d[0] < 0 || x + d[0] >= n) continue;\n if (y + d[1] < 0 || y + d[1] >= n) continue;\n if (!seen[x + d[0]][y + d[1]]) continue;\n seen[x + d[0]][y + d[1]] = false;\n pq.push({grid[x + d[0]][y + d[1]], x + d[0], y + d[1]});\n if ((x + d[0] == n-1) && (y + d[1] == n-1)) {\n return min(current_score, grid[n-1][n-1]);\n }\n }\n }\n\n return current_score;\n }\n};", "memory": "278693" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> g, td, sf;\n vector<vector<int>> dlst = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n g = grid;\n n = g.size();\n td = vector<vector<int>>(n, vector<int>(n, INT_MAX)); // min dist to thief\n sf = vector<vector<int>>(n, vector<int>(n, 0)); // max safeness factor to here\n bfsDistanceToThief();\n dijkstraPathToGoal();\n return sf[n-1][n-1];\n }\n void bfsDistanceToThief(){\n queue<vector<int>> que; // {(r, c), ...}\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n if(g[i][j]==1){\n que.push({i, j});\n td[i][j]=0;\n }\n }\n }\n int step=1;\n while(!que.empty()){\n int sz=que.size();\n while(sz--){\n int cr = que.front()[0];\n int cc = que.front()[1];\n que.pop();\n for(auto &d : dlst){\n int nr = cr+d[0];\n int nc = cc+d[1];\n if(nr<0 || nr>=n || nc<0 || nc>=n || td[nr][nc]<=step) continue;\n td[nr][nc]=step;\n que.push({nr, nc});\n }\n }\n step++;\n }\n }\n void dijkstraPathToGoal(){\n priority_queue<vector<int>> pq; // {(min_fs_till_now, r, c), ...}\n if(g[0][0]==0) pq.push({td[0][0], 0, 0});\n while(!pq.empty()){\n int cf = pq.top()[0];\n int cr = pq.top()[1];\n int cc = pq.top()[2];\n pq.pop();\n for(auto &d : dlst){\n int nr = cr + d[0];\n int nc = cc + d[1];\n if(nr<0 || nr>=n || nc<0 || nc>=n) continue;\n int nf = min(cf, td[nr][nc]);\n if(nf<=sf[nr][nc]) continue;\n sf[nr][nc] = nf;\n if(nr==n-1 && nc==n-1) return;\n pq.push({nf, nr, nc});\n }\n }\n }\n};", "memory": "283376" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n bool isValid(int i, int j, vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n if (i >= 0 && i < n && j >= 0 && j < m)\n return true;\n\n return false;\n }\n\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n if (grid[0][0] || grid[n - 1][m - 1])\n return 0;\n\n queue<vector<int>> q;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j]) {\n q.push({i, j, 0});\n grid[i][j] = 0;\n } else {\n grid[i][j] = INT_MAX;\n }\n }\n }\n int temp = INT_MIN;\n\n while (!q.empty()) {\n vector<int> top = q.front();\n q.pop();\n int i = top[0];\n int j = top[1];\n int d = top[2];\n\n for (auto dir : dirs) {\n int x = i + dir.first;\n int y = j + dir.second;\n\n if (!isValid(x, y, grid) || grid[x][y] != INT_MAX) {\n continue;\n }\n grid[x][y] = d + 1;\n q.push({x, y, d + 1});\n }\n }\n\n priority_queue<vector<int>> pq;\n // Push starting cell to the priority queue\n pq.push(vector<int>{grid[0][0], 0, 0}); // [maximum_safeness_till_now, x-coordinate, y-coordinate]\n grid[0][0] = -1; // Mark the source cell as visited\n\n // BFS to find the path with maximum safeness factor\n while (!pq.empty()) {\n auto curr = pq.top();\n pq.pop();\n\n // If reached the destination, return safeness factor\n if (curr[1] == n - 1 && curr[2] == n - 1) {\n return curr[0];\n }\n\n // Explore neighboring cells\n for (auto& d : dirs) {\n int di = d.first + curr[1];\n int dj = d.second + curr[2];\n if (isValid(di, dj,grid) && grid[di][dj] != -1) {\n // Update safeness factor for the path and mark the cell as visited\n pq.push(vector<int>{min(curr[0], grid[di][dj]), di, dj});\n grid[di][dj] = -1;\n }\n }\n }\n\n return -1;\n\n\n\n\n\n\n\n \n\n for (int i = 1; i < n; i++) {\n grid[i][0] = min(grid[i][0], grid[i - 1][0]);\n grid[0][i] = min(grid[0][i], grid[0][i - 1]);\n }\n\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n grid[i][j] =\n min(grid[i][j], max(grid[i - 1][j], grid[i][j - 1]));\n }\n // cout << \"\" << endl;\n }\n return grid[n - 1][m - 1];\n }\n};", "memory": "283376" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int dx[4] = {-1, 1, 0, 0};\n int dy[4] = {0, 0, -1, 1};\n \n bool isValid(int x, int y, int n) {\n return (0 <= x && x < n && 0 <= y && y < n);\n }\n \n vector<vector<int>> calculateSafeness(vector<vector<int>>& grid) {\n int n = grid.size();\n queue<pair<int,int>> q;\n vector<vector<int>> safeness(n, vector<int>(n, -1));\n \n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < n; ++j) {\n if(grid[i][j] == 1) {\n q.push({i, j});\n safeness[i][j] = 0;\n }\n }\n }\n \n while(!q.empty()) {\n auto x = q.front();\n q.pop();\n for(int i = 0; i < 4; ++i) {\n int nx = x.first + dx[i];\n int ny = x.second + dy[i];\n if(isValid(nx, ny, n) && safeness[nx][ny] == -1) {\n q.push({nx, ny});\n safeness[nx][ny] = safeness[x.first][x.second] + 1;\n }\n }\n }\n return safeness;\n }\n \n bool check(int mid, vector<vector<int>>& safeness) {\n int n = safeness.size();\n vector<vector<int>> visit(n, vector<int>(n, false));\n queue<pair<int,int>> q;\n if(safeness[0][0] >= mid) {\n q.push({0, 0});\n visit[0][0] = true;\n }\n \n while(!q.empty()) {\n auto x = q.front();\n if(x.first == n - 1 && x.second == n - 1) {\n return true;\n }\n q.pop();\n for(int i = 0; i < 4; ++i) {\n int nx = x.first + dx[i];\n int ny = x.second + dy[i];\n if(isValid(nx, ny, n) && !visit[nx][ny] && safeness[nx][ny] >= mid) {\n q.push({nx, ny});\n visit[nx][ny] = true;\n }\n }\n }\n return false;\n }\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n vector<vector<int>> safeness = calculateSafeness(grid);\n \n int low = 0, high = 400;\n int ans;\n while(low <= high) {\n int mid = (low + high) / 2;\n if(check(mid, safeness)) {\n low = mid + 1;\n ans = mid;\n }\n else {\n high = mid - 1;\n }\n }\n return ans;\n }\n};", "memory": "288058" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n vector<pair<int, int>> index = {{1,0}, {-1,0}, {0,1}, {0,-1}};\n vector<vector<int>> vis;\n void fillDist(vector<vector<int>>& grid) {\n int k=0; n=grid.size();\n vis.resize(n,vector<int>(n,0));\n queue<pair<int,int>> q;\n for(int i=0;i<n;i++){\n for (int j=0;j<n;j++){\n if(grid[i][j]==1){ q.push({i,j}); vis[i][j]=1;}\n }\n }\n\n while(!q.empty()) {\n int sz = q.size();\n while(sz--) {\n auto [x,y] = q.front(); \n q.pop();\n grid[x][y]=k;\n for(auto [i,j]: index){\n if(x+i<n && y+j<n && x+i>=0 && y+j>=0 && vis[x+i][y+j]==0){q.push({x+i, y+j}); vis[x+i][y+j]=1;}\n }\n }\n k++;\n }\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n fillDist(grid);\n priority_queue<vector<int>> pq;\n int ans = 0; n=grid.size();\n vis.clear();\n vis.resize(n,vector<int>(n,0));\n // for(int i=0;i<n;i++){\n // for (int j=0;j<n;j++){\n // cout<<grid[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n // cout<<endl;\n\n pq.push({grid[0][0], 0, 0});\n vis[0][0]=1;\n while(!pq.empty()){\n vector<int> vec = pq.top();\n pq.pop();\n if(vec[1]==n-1 && vec[2]==n-1)ans = max(ans, vec[0]);\n for(auto [i,j]: index){\n if(vec[1]+i<n && vec[2]+j<n && vec[1]+i>=0 && vec[2]+j>=0 && vis[vec[1]+i][vec[2]+j]==0) {\n pq.push({min(vec[0], grid[vec[1]+i][vec[2]+j]), vec[1]+i, vec[2]+j});\n vis[vec[1]+i][vec[2]+j]=1;\n }\n }\n }\n return ans;\n }\n};", "memory": "288058" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n vector<vector<int>> DIRs = {\n {1,0},{0,1},{-1,0},{0,-1}\n };\n\n int dist(int x1, int y1, int x2, int y2) {\n return abs(x1 - x2) + abs(y1 - y2);\n }\n\n struct comp{\n bool operator()(const vector<int>& v1, const vector<int>& v2) {\n return v1[2] < v2[2];\n }\n };\n\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n queue<vector<int>> q;\n\n vector<vector<int>> dists(n, vector<int>(n, -1));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n dists[i][j] = 0;\n q.push({i, j});\n }\n }\n }\n\n while(!q.empty()) {\n int lx = q.front()[0];\n int ly = q.front()[1];\n q.pop();\n\n for (int k = 0; k < 4; k++) {\n int nx = lx + DIRs[k][0];\n int ny = ly + DIRs[k][1];\n\n if (nx >= 0 && nx < n&& ny >= 0 && ny < n && dists[nx][ny] == -1) {\n dists[nx][ny] = dists[lx][ly] + 1;\n q.push({nx, ny});\n }\n }\n }\n\n vector<vector<int>> sfs(n, vector<int>(n, -1));\n // priority_queue<vector<int>, vector<vector<int>>, comp> pq; // v[0]:x, v[1]:y; v[2]:sf\n priority_queue<vector<int>> pq; // v[0]:sf, v[1]:x; v[2]:y\n pq.push({dists[0][0], 0, 0});\n\n while(!pq.empty()) {\n int lsf = pq.top()[0];\n int lx = pq.top()[1];\n int ly = pq.top()[2];\n pq.pop();\n\n if (sfs[lx][ly] != -1) continue;\n \n sfs[lx][ly] = lsf;\n if (lx == n-1 && ly == n-1) {\n return lsf;\n }\n\n for (int k = 0; k < 4; k++) {\n int nx = lx + DIRs[k][0];\n int ny = ly + DIRs[k][1];\n\n if (nx >= 0 && nx < n && ny >= 0 && ny < n && sfs[nx][ny] == -1) {\n pq.push({min(lsf, dists[nx][ny]), nx, ny});\n }\n }\n }\n\n return sfs[n-1][n-1];\n }\n\n // int maximumSafenessFactor(vector<vector<int>>& grid) {\n // int m = grid.size();\n // int n = grid[0].size();\n\n // vector<vector<int>> thieves;\n // for (int i = 0; i < m; i++) {\n // for (int j = 0; j < n; j++) {\n // if (grid[i][j] == 1) thieves.push_back({i, j});\n // }\n // }\n\n // vector<vector<int>> dists(m, vector<int>(n, m+n));\n // int tl = thieves.size();\n // for (int k = 0; k < tl; k++) {\n // int tx = thieves[k][0];\n // int ty = thieves[k][1];\n\n // for (int i = 0; i < m; i++) {\n // for (int j = 0; j < n; j++) {\n // if (grid[i][j] == 0) dists[i][j] = min(dists[i][j], dist(tx, ty, i, j));\n // else dists[i][j] = 0;\n // }\n // }\n // }\n\n // vector<vector<int>> dp(m, vector<int>(n, -1));\n // queue<vector<int>> q;\n // q.push({0,0});\n // dp[0][0] = dists[0][0];\n\n // while(!q.empty()) {\n // int lx = q.front()[0];\n // int ly = q.front()[1];\n // q.pop();\n\n // for (int k = 0; k < 4; k++) {\n // int nx = lx + DIRs[k][0];\n // int ny = ly + DIRs[k][1];\n\n // if (nx >= 0 && nx < m && ny >= 0 && ny < n) {\n // // if (grid[nx][ny] == 1 && dp[nx][ny] == -1) dp[nx][ny] = 0;\n // // else \n // if (dp[nx][ny] == -1 || dp[nx][ny] < min(dists[nx][ny], dp[lx][ly])) {\n // dp[nx][ny] = min(dists[nx][ny], dp[lx][ly]);\n // q.push({nx, ny});\n // }\n // }\n // }\n // }\n\n // return dp[m-1][n-1];\n // }\n};", "memory": "292741" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dir[5]={-1,0,1,0,-1};\n queue<vector<int>> q;\n void bfs(vector<vector<int>>& grid,vector<vector<int>>& mDist,int n){\n while(!q.empty()){\n auto vec=q.front();\n int i=vec[0],j=vec[1];\n q.pop();\n for(int d=0;d<4;d++){\n int x=i+dir[d],y=j+dir[d+1];\n if(x<0 || x>=n || y<0 || y>=n)continue;\n if(mDist[x][y]>mDist[i][j]+1){\n mDist[x][y] = mDist[i][j]+1;\n q.push({x,y});\n }\n }\n }\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n if(grid[0][0]==1 || grid[n-1][n-1]==1)return 0;\n vector<vector<int>> mDist(n,vector<int>(n,1e9));\n // vector<vector<int>> ones;\n // for(int i=0;i<n;i++){\n // for(int j=0;j<n;j++){\n // if(grid[i][j]==1){\n // ones.push_back({i,j});\n // mDist[i][j] = 0;\n // }\n // }\n // }\n // for(int i=0;i<n;i++){\n // for(int j=0;j<n;j++){\n // if(grid[i][j]==1)continue;\n // for(auto &one:ones){\n // int d=abs(i-one[0])+abs(j-one[1]);\n // mDist[i][j] = min(mDist[i][j],d);\n // }\n // }\n // }\n \n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]==1){\n q.push({i,j});\n mDist[i][j] = 0;\n }\n }\n }\n bfs(grid,mDist,n);\n priority_queue<vector<int>> mxHeap;\n mxHeap.push({mDist[0][0],0,0});\n \n int ans=mDist[0][0];\n vector<vector<bool>> vis(n,vector<bool>(n,false));\n vis[0][0]=true; \n bool reached=false; \n while(!mxHeap.empty()){\n auto vec=mxHeap.top();\n int currDist=vec[0],i=vec[1],j=vec[2];\n mxHeap.pop();\n if(i==n-1 && j==n-1){\n return currDist;\n }\n for(int d=0;d<4;d++){\n int x=i+dir[d],y=j+dir[d+1];\n if(x<0 || x>=n || y<0 || y>=n || vis[x][y])continue;\n vis[x][y]=true;\n int mn=min(currDist,mDist[x][y]);\n mDist[x][y]=mn;\n mxHeap.push({mn,x,y});\n }\n }\n return 0;\n }\n};", "memory": "292741" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic: \n int dx[4] = {-1, 0, 1, 0};\n int dy[4] = {0, -1, 0, 1};\n int n;\nprivate:\n bool check(vector<vector<int>> &distToNearestThief, int sf)\n {\n queue<pair<int,int>> q;\n \n vector<vector<int>> vis(n, vector<int> (n, 0));\n \n //(0,0) to (n-1, n-1);\n //Applying simple BFS\n \n q.push({0,0});\n vis[0][0] = 1;\n \n if(distToNearestThief[0][0] < sf)\n return false;\n \n while(!q.empty())\n {\n auto curr = q.front();\n q.pop();\n \n int x = curr.first;\n int y = curr.second;\n \n if(x == n-1 && y == n-1)\n {\n return true;\n }\n \n for(int i=0; i<4; i++)\n {\n int a = x+dx[i];\n int b = y+dy[i];\n \n if(a>=0 && b>=0 && a<n && b<n && vis[a][b] == 0 && distToNearestThief[a][b] >= sf)\n {\n vis[a][b] = 1;\n q.push({a,b});\n }\n }\n }\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n n = grid.size();\n \n //Step 1 - Precalculation of distToNearestThief - for each cell\n vector<vector<int>> distToNearestThief(n, vector<int> (n, -1));\n \n queue<pair<int,int>> q;\n vector<vector<int>> vis(n, vector<int> (n, 0));\n \n //push all cells in queue where thieves are present\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if(grid[i][j] == 1)\n {\n q.push({i,j});\n vis[i][j] = 1;\n // distToNearestThief[i][j] = 0; //dist to a thief to itself is 0\n }\n }\n }\n \n \n // int maxiDist = 0;\n //Applying Multi-source BFS and updating Manhatten distance of each cell to its nearest thief\n \n int level = 0;\n while(!q.empty())\n {\n int size = q.size();\n for(int k=0; k<size; k++)\n {\n auto curr = q.front();\n q.pop();\n\n int x = curr.first;\n int y = curr.second;\n \n distToNearestThief[x][y]= level;\n\n for(int i=0; i<4; i++)\n {\n int a = x+dx[i];\n int b = y+dy[i];\n\n if(a>=0 && b>=0 && a<n && b<n && vis[a][b]== 0)\n {\n // distToNearestThief[a][b] = distToNearestThief[x][y] + 1;\n // maxiDist = max(maxiDist, distToNearestThief[a][b]); //(for hi)\n vis[a][b] = 1;\n q.push({a,b});\n }\n }\n }\n level++;\n }\n \n \n \n //Step 2: Apply Binary Search on Safe Factor (SF)\n int lo = 0, hi = 400;\n int res = 0;\n \n while(lo <= hi)\n {\n int mid_sf = lo + (hi - lo)/2;\n //to find a path where every cell should be >= mid\n \n if(check(distToNearestThief, mid_sf)) //if there is a path where all cell >= mid_sf\n {\n res = mid_sf;\n lo = mid_sf + 1;\n }\n else{\n hi = mid_sf - 1;\n }\n }\n \n return res;\n }\n};", "memory": "297423" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "//T.C : O(log(n) * n^2)\n//S.C : O(n^2)\n\nclass Solution {\npublic: \n int dx[4] = {-1, 0, 1, 0};\n int dy[4] = {0, -1, 0, 1};\n int n;\nprivate:\n bool check(vector<vector<int>> &distToNearestThief, int sf)\n {\n queue<pair<int,int>> q;\n \n vector<vector<int>> vis(n, vector<int> (n, 0));\n \n //(0,0) to (n-1, n-1);\n //Applying simple BFS\n \n q.push({0,0});\n vis[0][0] = 1;\n \n if(distToNearestThief[0][0] < sf)\n return false;\n \n while(!q.empty())\n {\n auto curr = q.front();\n q.pop();\n \n int x = curr.first;\n int y = curr.second;\n \n if(x == n-1 && y == n-1)\n {\n return true;\n }\n \n for(int i=0; i<4; i++)\n {\n int a = x+dx[i];\n int b = y+dy[i];\n \n if(a>=0 && b>=0 && a<n && b<n && vis[a][b] == 0 && distToNearestThief[a][b] >= sf)\n {\n vis[a][b] = 1;\n q.push({a,b});\n }\n }\n }\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n n = grid.size();\n \n //Step 1 - Precalculation of distToNearestThief - for each cell\n vector<vector<int>> distToNearestThief(n, vector<int> (n, -1));\n \n queue<pair<int,int>> q;\n vector<vector<int>> vis(n, vector<int> (n, 0));\n \n //push all cells in queue where thieves are present\n // O(n^2)\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if(grid[i][j] == 1)\n {\n q.push({i,j});\n vis[i][j] = 1;\n // distToNearestThief[i][j] = 0; //dist to a thief to itself is 0\n }\n }\n }\n \n \n // int maxiDist = 0;\n //Applying Multi-source BFS and updating Manhatten distance of each cell to its nearest thief\n \n // O(n^2)\n int level = 0;\n while(!q.empty())\n {\n int size = q.size();\n for(int k=0; k<size; k++)\n {\n auto curr = q.front();\n q.pop();\n\n int x = curr.first;\n int y = curr.second;\n \n distToNearestThief[x][y]= level;\n\n for(int i=0; i<4; i++)\n {\n int a = x+dx[i];\n int b = y+dy[i];\n\n if(a>=0 && b>=0 && a<n && b<n && vis[a][b]== 0)\n {\n // distToNearestThief[a][b] = distToNearestThief[x][y] + 1;\n // maxiDist = max(maxiDist, distToNearestThief[a][b]); //(for hi)\n vis[a][b] = 1;\n q.push({a,b});\n }\n }\n }\n level++;\n }\n \n \n \n //Step 2: Apply Binary Search on Safe Factor (SF)\n int lo = 0, hi = 400;\n int res = 0;\n \n \n // O(log(n) * n^2)\n while(lo <= hi)\n {\n int mid_sf = lo + (hi - lo)/2;\n //to find a path where every cell should be >= mid\n \n if(check(distToNearestThief, mid_sf)) //if there is a path where all cell >= mid_sf\n {\n res = mid_sf;\n lo = mid_sf + 1;\n }\n else{\n hi = mid_sf - 1;\n }\n }\n \n return res;\n }\n};", "memory": "297423" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n bool f(vector<vector<int>> dist, int dis){\n int n=dist.size();\n if(dist[0][0]<dis) return false; \n\n vector<vector<int>> vis(n,vector<int>(n,0));\n\n queue<pair<int,int>> q;\n q.push({0,0});\n vis[0][0]=1;\n\n int dr[4]={-1,0,1,0}, dc[4]={0,-1,0,1};\n\n while(!q.empty()){\n int r=q.front().first;\n int c=q.front().second;\n q.pop();\n\n if(r==n-1 && c==n-1) return true;\n\n for(int i=0;i<4;i++){\n int nr=r+dr[i], nc=c+dc[i];\n\n if(nr>=0 && nr<n && nc>=0 && nc<n && !vis[nr][nc]){\n if(dist[nr][nc]<dis) continue;\n vis[nr][nc]=1;\n q.push({nr,nc});\n }\n }\n }\n\n return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n=grid[0].size();\n \n if(grid[0][0] || grid[n-1][n-1]) return 0;\n\n vector<vector<int>> vis(n,vector<int>(n,0));\n queue<pair<pair<int,int>,int>> q;\n \n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]){\n q.push({{i,j},0});\n vis[i][j]=1;\n }\n }\n }\n\n vector<vector<int>> dist(n,vector<int>(n,0));\n int dr[4]={-1,0,1,0}, dc[4]={0,-1,0,1};\n while(!q.empty()){\n int r=q.front().first.first;\n int c=q.front().first.second;\n int d=q.front().second;\n q.pop();\n\n for(int i=0;i<4;i++){\n int nr=r+dr[i], nc=c+dc[i];\n if(nr>=0 && nr<n && nc>=0 && nc<n && !vis[nr][nc]){\n dist[nr][nc]=d+1;\n q.push({{nr,nc},d+1});\n vis[nr][nc]=1;\n }\n }\n }\n\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n cout << dist[i][j] << \" \";\n }\n cout << \"\\n\";\n }\n\n int low=0, high=400;\n int ret=0;\n while(low<=high){\n int mid=(high+low)/2;\n\n if(f(dist,mid)){\n ret=mid;\n low=mid+1;\n }\n else{\n high=mid-1;\n }\n }\n\n return ret;\n }\n};", "memory": "302106" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int m, n;\n int dx[4] = {-1, 0, 0, 1};\n int dy[4] = {0, 1, -1, 0};\n \n bool valid(int i, int j) {\n return (i>=0 && j>=0 && i<n && j<n);\n }\n\n bool ok(vector<vector<int>> dist, int d) {\n queue<pair<int, int>> q;\n vector<vector<bool>> vis(n, vector<bool> (n, false));\n\n if(dist[0][0] < d)\n return false;\n\n q.push({0, 0});\n vis[0][0] = true;\n\n while(!q.empty()) {\n int cx = q.front().first;\n int cy = q.front().second;\n q.pop();\n\n if(cx == n-1 && cy == n-1)\n return true;\n\n for(int i=0;i<4;i++) {\n int ni = cx + dx[i];\n int nj = cy + dy[i];\n\n if(valid(ni, nj) && dist[ni][nj] >= d && !vis[ni][nj]) {\n q.push({ni, nj});\n vis[ni][nj] = true;\n }\n }\n }\n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n m = grid.size();\n n = grid[0].size();\n\n queue<pair<int, int>> q;\n vector<vector<int>> distThief(n, vector<int> (n, -1));\n vector<vector<bool>> vis(n, vector<bool> (n, false));\n\n for(int i=0;i<m;i++) {\n for(int j=0;j<n;j++) {\n if(grid[i][j] == 1) {\n q.push({i, j});\n vis[i][j] = true;\n }\n }\n }\n\n int lvl=0;\n while(!q.empty()) {\n int sz = q.size();\n\n while(sz--) {\n int ci = q.front().first;\n int cj = q.front().second;\n q.pop();\n\n distThief[ci][cj] = lvl;\n\n for(int i=0;i<4;i++) {\n int ni = ci + dx[i];\n int nj = cj + dy[i];\n\n if(valid(ni, nj) && !vis[ni][nj]) {\n q.push({ni, nj});\n vis[ni][nj] = true;\n }\n }\n }\n lvl++;\n }\n \n int l=0, r=404;\n int sf=0;\n\n while(l<=r) {\n int mid = (l+r)/2;\n\n if(ok(distThief, mid)) {\n sf = mid;\n l = mid+1;\n }\n else \n r = mid-1;\n }\n\n return sf;\n }\n};", "memory": "302106" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n// optimised BFS + Binary search \nint delRow[4]={-1,0,1,0};\nint delCol[4]={0,1,0,-1};\nbool check(int safeDistance,vector<vector<int>>&dp,int n,int m){\n queue<pair<int,int>>q;\n vector<vector<int>>vis(n,vector<int>(m,0));\n q.push({0,0});\n vis[0][0]=1;\n\n if(dp[0][0]<safeDistance) return false;\n \n while(!q.empty()){\n int row=q.front().first;\n int col=q.front().second;\n q.pop();\n if(row==n-1 && col==m-1){\n return true;\n }\n for(int i=0;i<4;i++){\n int nRow=row+delRow[i];\n int nCol=col+delCol[i];\n if((nRow>=0 and nRow<n) and (nCol>=0 and nCol<m) and (!vis[nRow][nCol]) and (dp[nRow][nCol]>=safeDistance)){\n q.push({nRow,nCol});\n vis[nRow][nCol]=1;\n }\n }\n }\n return false;\n}\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n\n vector<vector<int>>dp(n,vector<int>(m,-1));\n vector<vector<int>>vis(n,vector<int>(m,0));\n queue<pair<int,pair<int,int>>>q;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n q.push({0,{i,j}});\n vis[i][j]=1;\n }\n }\n }\n while(!q.empty()){\n int row=q.front().second.first;\n int col=q.front().second.second;\n int steps=q.front().first;\n q.pop();\n dp[row][col]=steps;\n\n for(int i=0;i<4;i++){\n int nRow=row+delRow[i];\n int nCol=col+delCol[i];\n if((nRow>=0 and nRow<n) and (nCol>=0 and nCol<m) and (!vis[nRow][nCol])){\n vis[nRow][nCol]=1;\n q.push({steps+1,{nRow,nCol}});\n }\n }\n }\n\n int s=0;\n int e=400;\n int ans=0;\n while(s<=e){\n int mid=(s+e)/2;\n if(check(mid,dp,n,m)){\n ans=mid;\n s=mid+1;\n }\n else{\n e=mid-1;\n }\n }\n return ans;\n }\n};", "memory": "306788" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool possible(int mid,vector<vector<int>>&dis){\n queue<pair<int,pair<int,int>>>q;\n if(dis[0][0]<mid)\n return false;\n q.push({dis[0][0],{0,0}});\n int n=dis.size();\n vector<vector<int>>vis(n,vector<int>(n));\n int dr[]={-1,1,0,0};\n int dc[]={0,0,-1,1};\n vis[0][0]=1;\n while(!q.empty()){\n auto d=q.front().first;\n int row=q.front().second.first;\n int col=q.front().second.second;\n q.pop();\n if(row==n-1 && col==n-1)\n return true;\n for(int i=0;i<4;i++){\n int nrow=row+dr[i];\n int ncol=col+dc[i];\n if(nrow>=0 && ncol>=0 && nrow<n && ncol<n && dis[nrow][ncol]>=mid && !vis[nrow][ncol]){\n q.push({dis[nrow][ncol],{nrow,ncol}});\n vis[nrow][ncol]=1;\n }\n }\n }\n return false;\n}\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n vector<vector<int>>dis(m,vector<int>(n,1e9));\n queue<pair<int,pair<int,int>>>q;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]==1){\n q.push({0,{i,j}});\n dis[i][j]=0;\n }\n }\n }\n int dr[]={-1,1,0,0};\n int dc[]={0,0,-1,1};\n while(!q.empty()){\n auto d=q.front().first;\n int row=q.front().second.first;\n int col=q.front().second.second;\n q.pop();\n for(int i=0;i<4;i++){\n int nrow=row+dr[i];\n int ncol=col+dc[i];\n if(nrow>=0 && ncol>=0 && nrow<n && ncol<n && dis[nrow][ncol]>dis[row][col]+1){\n dis[nrow][ncol]=dis[row][col]+1;\n q.push({d+1,{nrow,ncol}});\n }\n }\n }\n int low=0;\n int high=1e9;\n int ans=-1;\n while(low<=high){\n int mid=low+(high-low)/2;\n if(possible(mid,dis)){\n ans=mid;\n low=mid+1;\n }\n else\n high=mid-1;\n }\n return ans;\n }\n};", "memory": "306788" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n if(grid[m - 1][n - 1] == 1){\n return 0;\n }\n queue<vector<int>> q;\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j] == 1){\n grid[i][j] = 0;\n q.push({i, j, 0});\n }else {\n grid[i][j] = -1;\n }\n }\n }\n\n while(!q.empty()){\n vector<int> curr = q.front();\n q.pop();\n\n int x[] = {-1, 0, 1, 0};\n int y[] = {0, 1, 0, -1};\n\n for(int i = 0; i < 4; i++){\n int next_row = curr[0] + x[i];\n int next_col = curr[1] + y[i];\n\n if(next_row < 0 || next_row >= m || next_col < 0 || next_col >= n || grid[next_row][next_col] != -1){\n continue;\n }\n\n grid[next_row][next_col] = curr[2] + 1;\n q.push({next_row, next_col, grid[next_row][next_col]});\n }\n }\n\n auto mycomp = [&] (vector<int>& a, vector<int>& b){\n return a[2] < b[2];\n };\n\n vector<vector<int>> cost1(m, vector<int> (n, 0));\n priority_queue<vector<int>, vector<vector<int>>, decltype(mycomp)> pq(mycomp);\n\n pq.push({0, 0, grid[0][0]});\n grid[0][0] = -1;\n while(!pq.empty()){\n vector<int> curr = pq.top();\n pq.pop();\n\n int x[] = {-1, 0, 1, 0};\n int y[] = {0, 1, 0, -1};\n\n if(curr[0] == m - 1 && curr[1] == n - 1){\n return curr[2];\n }\n\n for(int i = 0; i < 4; i++){\n int next_row = curr[0] + x[i];\n int next_col = curr[1] + y[i];\n\n if(next_row < 0 || next_row >= m || next_col < 0 || next_col >= n || grid[next_row][next_col] == -1){\n continue;\n }\n pq.push({next_row, next_col, min(curr[2], grid[next_row][next_col])});\n grid[next_row][next_col] = -1;\n }\n }\n return -1;\n }\n};", "memory": "311471" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n if(grid[m - 1][n - 1] == 1){\n return 0;\n }\n queue<vector<int>> q;\n for(int i = 0; i < m; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j] == 1){\n grid[i][j] = 0;\n q.push({i, j, 0});\n }else {\n grid[i][j] = -1;\n }\n }\n }\n\n while(!q.empty()){\n vector<int> curr = q.front();\n q.pop();\n\n int x[] = {-1, 0, 1, 0};\n int y[] = {0, 1, 0, -1};\n\n for(int i = 0; i < 4; i++){\n int next_row = curr[0] + x[i];\n int next_col = curr[1] + y[i];\n\n if(next_row < 0 || next_row >= m || next_col < 0 || next_col >= n || grid[next_row][next_col] != -1){\n continue;\n }\n\n grid[next_row][next_col] = curr[2] + 1;\n q.push({next_row, next_col, grid[next_row][next_col]});\n }\n }\n\n auto mycomp = [&] (vector<int>& a, vector<int>& b){\n return a[2] < b[2];\n };\n\n vector<vector<int>> cost1(m, vector<int> (n, 0));\n priority_queue<vector<int>, vector<vector<int>>, decltype(mycomp)> pq(mycomp);\n\n pq.push({0, 0, grid[0][0]});\n grid[0][0] = -1;\n while(!pq.empty()){\n vector<int> curr = pq.top();\n pq.pop();\n\n int x[] = {-1, 0, 1, 0};\n int y[] = {0, 1, 0, -1};\n\n if(curr[0] == m - 1 && curr[1] == n - 1){\n return curr[2];\n }\n\n for(int i = 0; i < 4; i++){\n int next_row = curr[0] + x[i];\n int next_col = curr[1] + y[i];\n\n if(next_row < 0 || next_row >= m || next_col < 0 || next_col >= n || grid[next_row][next_col] == -1){\n continue;\n }\n pq.push({next_row, next_col, min(curr[2], grid[next_row][next_col])});\n grid[next_row][next_col] = -1;\n }\n }\n return -1;\n }\n};", "memory": "311471" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n \n \n queue<vector<int>> q;\n int n = grid.size();\n\n if(grid[n-1][n-1] == 1)\n return 0;\n vector<vector<int>> dist(n, vector<int>(n,-1));\n\n for(int i=0; i< n; i++) {\n for(int j=0; j< n; j++) {\n if(grid[i][j])\n q.push({i,j, 0}), dist[i][j] = 0;\n }\n }\n\n int dx[4] = {-1,1,0,0};\n int dy[4] = {0,0,1,-1};\n int newx, newy;\n int ans = INT_MAX;\n\n while(!q.empty()) {\n auto t = q.front();\n q.pop();\n\n for(int k=0; k<4; k++) {\n newx = t[0] + dx[k];\n newy = t[1] + dy[k];\n\n if(newx >=0 && newx < n && newy >= 0 && newy < n) {\n if(dist[newx][newy] == -1 )\n {\n dist[newx][newy] = 1 + t[2];\n q.push({newx,newy, dist[newx][newy]});\n }\n else {\n dist[newx][newy] = min(dist[newx][newy], \n 1 + t[2]);\n }\n // cout<<newx<<\" \"<<newy<<\" \"<<dist[newx][newy]<<endl;\n // ans = min(ans, dist[newx][newy]);\n }\n }\n }\n\n q.push({0,0});\n grid[0][0] = -1;\n ans = dist[0][0];\n priority_queue<vector<int>> pq;\n pq.push({dist[0][0], 0,0});\n\n while(!pq.empty()) {\n auto t = pq.top();\n pq.pop();\n ans = min(ans, t[0]);\n // cout<<t[0] <<\" \"<<t[1]<<\" \"<<t[2]<<endl;\n\n if(t[1] == n-1 && t[2] == n-1)\n return ans;\n\n for(int k=0; k<4; k++) {\n newx = t[1] + dx[k];\n newy = t[2] + dy[k];\n if(newx >=0 && newx < n && newy >= 0 && newy < n && grid[newx][newy] != -1) {\n pq.push({dist[newx][newy],newx, newy});\n grid[newx][newy] = -1;\n // ans = min(dist[newx][newy], ans);\n }\n\n }\n\n\n }\n\n return ans;\n \n }\n};", "memory": "316153" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isP(int r, int c, int &safe, vector<vector<int>> &grid, vector<vector<bool>> &isVis){\n if(grid[r][c] < safe) return false;\n \n int n = grid.size();\n\n isVis[r][c] = true;\n\n if(r == (n-1) and c == (n-1)) return true;\n\n vector<int> dr = {-1, 1, 0, 0};\n vector<int> dc = {0, 0, 1, -1};\n\n for(int i = 0; i < 4; i++){\n int nr = r + dr[i], nc = c + dc[i];\n if(nr < n and nc < n and nr >= 0 and nc >= 0 and !isVis[nr][nc]){\n if(isP(nr, nc, safe, grid, isVis)) return true;\n }\n }\n\n return false;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);\n int n = grid.size();\n if(n == 1) return 0;\n if(grid[0][0] || grid[n - 1][n - 1]) return 0;\n queue<pair<int, int>> q;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n if(grid[i][j]){\n q.push({i, j});\n grid[i][j] = 0;\n }\n else grid[i][j] = -1;\n }\n }\n\n // multisource bfs from every cell containing a thief \n // finding shortest manhattan distance from a given node to one of the cells containing thief\n\n while(!q.empty()){\n int size = q.size();\n for(int i = 0; i < size; i++){\n auto [r, c] = q.front();\n q.pop();\n if(r + 1 < n and grid[r+1][c] == -1){\n q.push({r+1, c});\n grid[r+1][c] = grid[r][c] + 1;\n }\n if(r - 1 >= 0 and grid[r-1][c] == -1){\n q.push({r-1, c});\n grid[r-1][c] = grid[r][c] + 1;\n }\n if(c + 1 < n and grid[r][c+1] == -1){\n q.push({r, c+1});\n grid[r][c+1] = grid[r][c] + 1;\n }\n if(c - 1 >= 0 and grid[r][c-1] == -1){\n q.push({r, c-1});\n grid[r][c-1] = grid[r][c] + 1;\n }\n }\n }\n\n\n int low = 0, high = (2*n) - 2;\n int res = 0;\n while(low <= high){\n int mid = (low + high)/2;\n vector<vector<bool>> isvis(n, vector<bool>(n, false));\n if(isP(0, 0, mid, grid, isvis)){\n res = mid;\n low = mid+1;\n }\n else high = mid-1;\n }\n\n return res;\n }\n};", "memory": "316153" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic: \n vector<pair<int, int>> arr = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n bool dfs(int n, int i, int j, int mid, vector<vector<int>>& dist, vector<vector<int>>& visited) {\n if (dist[i][j] < mid) {\n return false;\n }\n if (i == n - 1 && j == n - 1) {\n return true;\n }\n visited[i][j] = 1;\n for (int k = 0; k < 4; k++) {\n int ni = i + arr[k].first;\n int nj = j + arr[k].second;\n if (ni >= 0 && ni < n && nj >= 0 && nj < n && !visited[ni][nj]) {\n if (dfs(n, ni, nj, mid, dist, visited)) {\n return true;\n }\n }\n }\n return false;\n }\n int result(int step, vector<vector<int>>& dist) {\n int ans = 0;\n int l = 0;\n int r = step;\n while (l <= r) {\n int mid = (l + r) / 2;\n vector<vector<int>> visited(dist.size(), vector<int>(dist[0].size(), false));\n if (dfs(dist.size(), 0, 0, mid, dist, visited)) {\n ans = mid;\n l = mid + 1;\n }\n else {\n r = mid - 1;\n }\n }\n return ans;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n queue<pair<int,int>> q;\n vector<vector<int>> dist(n,vector<int> (m,0));\n vector<vector<int>> vis(n,vector<int> (m,0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++){\n if (grid[i][j] == 1) {\n q.push({i, j});\n vis[i][j] = 1;\n } \n }\n } \n int step = 0;\n while (!q.empty()) {\n int size = q.size();\n while (size--) {\n auto front = q.front();\n int r = front.first;\n int c = front.second;\n q.pop();\n vector<pair<int, int>> arr = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n for (auto& dir : arr) {\n int ni = r + dir.first;\n int nj = c + dir.second;\n if (ni >= 0 && ni < grid.size() && nj >= 0 && nj < grid[0].size() \n && !vis[ni][nj]) {\n q.push({ni, nj});\n vis[ni][nj] = 1;\n dist[ni][nj] = step + 1;\n }\n }\n }\n step++;\n }\n return result(step, dist);\n }\n};", "memory": "320836" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> dist(n, vector<int>(n, 1e5));\n queue<vector<int>> q;\n for (int i=0; i<n; i++) {\n for (int j=0; j<n; j++) {\n if (grid[i][j]) {\n q.push({i,j,0});\n dist[i][j] = 0;\n }\n }\n }\n\n while (!q.empty()) {\n vector<int> v = q.front();\n q.pop();\n int row = v[0], col = v[1], dis = v[2];\n if (row-1>=0) {\n if (dis+1<dist[row-1][col]) {\n dist[row-1][col] = dis+1;\n q.push({row-1,col,dis+1});\n }\n }\n if (row+1<n) {\n if (dis+1<dist[row+1][col]) {\n dist[row+1][col] = dis+1;\n q.push({row+1,col,dis+1});\n }\n }\n if (col-1>=0) {\n if (dis+1<dist[row][col-1]) {\n dist[row][col-1] = dis+1;\n q.push({row,col-1,dis+1});\n }\n }\n if (col+1<n) {\n if (dis+1<dist[row][col+1]) {\n dist[row][col+1] = dis+1;\n q.push({row,col+1,dis+1});\n }\n }\n }\n\n vector<vector<int>> vis(n, vector<int>(n, 0));\n priority_queue<pair<int, pair<int,int>>> pq;\n pq.push({dist[0][0], {0,0}});\n vis[0][0] = 1;\n while (!pq.empty()) {\n auto ele = pq.top();\n pq.pop();\n int dis = ele.first, row=ele.second.first, col=ele.second.second;\n if (row==n-1 && col==n-1) return dis;\n if (row-1>=0 && !vis[row-1][col]) {\n vis[row-1][col] = 1;\n pq.push({min(dis, dist[row-1][col]), {row-1,col}});\n }\n if (row+1<n && !vis[row+1][col]) {\n vis[row+1][col] = 1;\n pq.push({min(dis, dist[row+1][col]), {row+1,col}});\n }\n if (col-1>=0 && !vis[row][col-1]) {\n vis[row][col-1] = 1;\n pq.push({min(dis, dist[row][col-1]), {row,col-1}});\n }\n if (col+1<n && !vis[row][col+1]) {\n vis[row][col+1] = 1;\n pq.push({min(dis, dist[row][col+1]), {row,col+1}});\n }\n }\n return 0;\n }\n};", "memory": "320836" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int,pair<int,int>> pii;\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>>man(n,vector<int>(n,INT_MAX));\n queue<vector<int>>q;\n vector<int>dirx = {1,-1,0,0}, diry = {0,0,1,-1};\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]==1){\n man[i][j]=0;\n q.push({i,j});\n }\n }\n }\n\n while(!q.empty()){\n int sz = q.size();\n while(sz--){\n auto v = q.front();\n q.pop();\n for(int k=0;k<4;k++){\n int x = v[0]+dirx[k], y = v[1]+diry[k];\n if(x>=0 && y>=0 && x<n && y<n && man[x][y]>1+man[v[0]][v[1]]){\n q.push({x,y});\n man[x][y] = man[v[0]][v[1]]+1;\n }\n }\n }\n }\n \n vector<vector<int>>vis(n,vector<int>(n,0));\n priority_queue<pii>pq;\n vis[0][0]=1;\n pq.push({man[0][0],{0,0}});\n int ans = INT_MAX;\n while(!pq.empty()){\n auto v = pq.top();\n pq.pop();\n int sf = v.first;\n auto p = v.second;\n ans=min(ans,sf);\n if(p.first==n-1 && p.second==n-1)return ans;\n for(int k=0;k<4;k++){\n int x = p.first+dirx[k], y = p.second+diry[k];\n if(x<0 || x>=n || y<0 || y>=n || vis[x][y])continue;\n pq.push({man[x][y],{x,y}});\n vis[x][y]=1;\n }\n }\n return ans;\n // return -1;\n }\n};", "memory": "325518" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dx[4] = {1, 0, -1, 0};\n int dy[4] = {0, 1, 0, -1};\n\n vector<vector<int>> calculateSafenessFactors(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> safeness(\n n, vector<int>(n, INT_MAX)); \n queue<pair<int, int>> q;\n\n \n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({i, j});\n safeness[i][j] = 0;\n }\n }\n }\n\n vector<int> dirX = {-1, 1, 0, 0};\n vector<int> dirY = {0, 0, -1, 1};\n\n while (!q.empty()) {\n auto cell = q.front();\n q.pop();\n int x = cell.first;\n int y = cell.second;\n\n for (int i = 0; i < 4; i++) {\n int newX = x + dirX[i];\n int newY = y + dirY[i];\n\n if (newX >= 0 && newX < n && newY >= 0 && newY < n &&\n safeness[newX][newY] > safeness[x][y] + 1) {\n safeness[newX][newY] = safeness[x][y] + 1;\n q.push({newX, newY});\n }\n }\n }\n\n return safeness;\n }\n\n bool possible(int maxdist, vector<vector<int>> dist) {\n\n int n = dist.size();\n int m = dist[0].size();\n\n if (dist[0][0] < maxdist)\n return false;\n\n vector<vector<int>> visited(n, vector<int>(m, 0));\n\n queue<pair<int, int>> q;\n q.push({0, 0});\n\n while (!q.empty()) {\n auto [x, y] = q.front();\n visited[x][y] = 1;\n q.pop();\n\n if (x == n - 1 && y == m - 1) {\n return true;\n }\n\n for (int i = 0; i < 4; i++) {\n int newR = x + dx[i];\n int newC = y + dy[i];\n\n if (newR >= 0 && newR < n && newC >= 0 && newC < m &&\n !visited[newR][newC] && dist[newR][newC] >= maxdist) {\n\n visited[newR][newC] = 1;\n q.push({newR, newC});\n }\n }\n }\n\n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>> & grid) {\n\n int n = grid.size();\n int m = grid[0].size();\n int maxSafness = 0;\n\n vector<vector<int>> safeness = calculateSafenessFactors(grid);\n\n int low = 0;\n int high = 500;\n \n int result = 0;\n\n while (low <= high) {\n\n int mid = low + (high - low) / 2;\n\n bool is = possible(mid, safeness);\n\n\n if (is) {\n low = mid + 1;\n result = mid;\n\n } else {\n high = mid - 1;\n }\n }\n\n return result;\n }\n };\n", "memory": "325518" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void calculateAllOverSafetyOfCells(vector<vector<int>>& grid, \n vector<vector<int>>& safety, int n, int xAxis[], int yAxis[]) \n {\n queue<vector<int>>q ; // steps, row, col\n for(int i=0 ; i<n ; i++)\n {\n for(int j=0 ; j<n ; j++) \n {\n if(grid[i][j] == 1) \n {\n safety[i][j] = 0 ;\n q.push({0, i, j}) ;\n }\n }\n }\n\n while(!q.empty())\n {\n auto it = q.front() ;\n q.pop() ;\n\n int safe = it[0], row = it[1], col = it[2] ;\n\n for(int idx=0 ; idx<4 ; idx++)\n {\n int dr = row+xAxis[idx] ;\n int dc = col+yAxis[idx] ;\n\n if(dr>=0 && dr<n && dc>=0 && dc<n && safety[dr][dc] > safe+1)\n {\n safety[dr][dc] = safe+1 ;\n q.push({safety[dr][dc], dr, dc}) ;\n }\n }\n }\n }\n\n bool bfs(vector<vector<int>>& grid, \n vector<vector<int>>& safety, int n, int xAxis[], int yAxis[], int safeFactor) \n {\n vector<vector<bool>>vis(n, vector<bool>(n, false)) ;\n queue<pair<int,int>>q ;\n\n q.push({0, 0}) ;\n vis[0][0] = true ;\n if(safety[0][0] < safeFactor) return false ;\n\n while(!q.empty())\n { \n auto it = q.front() ;\n q.pop() ;\n int row = it.first, col = it.second ;\n\n if(row==n-1 && col==n-1) return true ;\n\n for(int i=0 ; i<4 ; i++)\n {\n int dr = row+xAxis[i] ;\n int dc = col+yAxis[i] ;\n\n if(dr>=0 && dr<n && dc>=0 && dc<n && !vis[dr][dc] && safety[dr][dc] >= safeFactor)\n {\n q.push({dr,dc}) ;\n vis[dr][dc] = true ;\n }\n }\n }\n return false ;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size() ;\n if(grid[0][0]==1 || grid[n-1][n-1]==1) return 0;\n \n vector<vector<int>>safety(n, vector<int>(n, INT_MAX)) ;\n \n int xAxis[] = {-1,0,+1,0} ;\n int yAxis[] = {0,+1,0,-1} ;\n\n calculateAllOverSafetyOfCells(grid, safety, n, xAxis, yAxis) ;\n\n int low = 0, high = 400 ;\n while(low <= high)\n {\n int mid = low+(high-low)/2 ;\n\n if(bfs(grid, safety, n, xAxis, yAxis, mid)) {\n low = mid+1 ;\n }\n else {\n high = mid-1 ;\n }\n }\n return high ;\n }\n};", "memory": "330201" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": " int delrow[] = {0,1,0,-1};\n int delcol[] = {1,0,-1,0};\n\nclass Solution {\npublic:\n vector<vector<int>> bfs(vector<vector<int>>& grid){\n int n=grid.size();\n int m=grid[0].size();\n vector<vector<int>> dist(n,vector<int>(m,0));\n vector<vector<int>> vis(n,vector<int>(m,0));\n queue<pair<int,pair<int,int>>> q;\n \n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1 && !vis[i][j]){\n q.push({0,{i,j}});\n vis[i][j]=1;\n }\n }\n }\n while(!q.empty()){\n auto it = q.front();\n q.pop();\n int val=it.first;\n int row = it.second.first;\n int col = it.second.second;\n dist[row][col]=val;\n for(int i=0;i<4;i++){\n int nr = row + delrow[i];\n int nc = col + delcol[i];\n if(nr>=0 && nr<n && nc>=0 && nc<m && !vis[nr][nc]){\n q.push({val+1,{nr,nc}});\n vis[nr][nc]=1;\n }\n }\n \n }\n return dist;\n }\n bool check(int safedist,vector<vector<int>> dist){\n int n=dist.size();\n int m=dist[0].size();\n queue<pair<int,int>> q;\n vector<vector<int>> vis(n,vector<int>(m,0));\n q.push({0,0});\n vis[0][0]=1;\n if(dist[0][0]<safedist)return false;\n while(!q.empty()){\n int row =q.front().first;\n int col = q.front().second;\n q.pop();\n if(row==n-1 && col==m-1)return true;\n for(int i=0;i<4;i++){\n int nr = row + delrow[i];\n int nc = col + delcol[i];\n // if(nr==n-1 && nc==m-1)return true;\n if(nr>=0 && nr<n && nc>=0 && nc<m && !vis[nr][nc]){\n if(dist[nr][nc] < safedist)continue;\n vis[nr][nc]=1;\n q.push({nr,nc});\n }\n\n }\n\n }\n return false;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n vector<vector<int>> dist = bfs(grid);\n int n=grid.size();\n int m=grid[0].size();\n int maxi=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n maxi = max(maxi,dist[i][j]);\n }\n }\n int low=0;\n int high = maxi;\n int ans=-1;\n while(low<=high){\n int mid = low + (high-low)/2;\n if(check(mid,dist)){\n ans=mid;\n low=mid+1;\n }\n else{\n high=mid-1;\n }\n }\n return ans;\n\n }\n};", "memory": "330201" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "using ppi = pair<pair<int, int>, int>;\nclass Solution {\npublic:\n\n bool isOk(vector<vector<int>>& grid, vector<vector<int>>&vis) {\n if (vis[0][0] == 1) return false;\n int n = grid.size();\n int v[] = { -1, 0, 1, 0, -1};\n queue<pair<int, int>> q;\n q.push({0, 0});\n vis[0][0] = 1;\n while (!q.empty()) {\n auto it = q.front();\n q.pop();\n int r = it.first;\n int c = it.second;\n if (r == n - 1 && c == n - 1) return true;\n for (int i = 0; i < 4; i++) {\n int nr = r + v[i];\n int nc = c + v[i + 1];\n\n if (nr < 0 || nr >= n || nc < 0 || nc >= n || vis[nr][nc] == 1)continue;\n vis[nr][nc] = 1;\n q.push({nr, nc});\n }\n }\n return false;\n }\n\n void bfs(vector<vector<int>>& grid, vector<vector<int>>&vis, int x) {\n queue<ppi> q;\n int n = grid.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({{i, j}, x-1});\n vis[i][j] = 1;\n }\n }\n }\n if(x == 0) return;\n int v[] = { -1, 0, 1, 0, -1};\n while (!q.empty()) {\n auto it = q.front();\n q.pop();\n int r = it.first.first;\n int c = it.first.second;\n int d = it.second;\n if (d == 0) continue;\n for (int i = 0; i < 4; i++) {\n int nr = r + v[i];\n int nc = c + v[i + 1];\n\n if (nr < 0 || nr >= n || nc < 0 || nc >= n || vis[nr][nc] == 1)continue;\n vis[nr][nc] = 1;\n q.push({{nr, nc}, d - 1});\n }\n }\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n int s = 0, e = n - 1;\n int ans = 0;\n while (s <= e) {\n int mid = s + (e - s) / 2;\n vector<vector<int>> vis(n, vector<int>(n, 0));\n bfs(grid, vis, mid);\n if (isOk(grid, vis)) {\n ans = max(mid, ans);\n s = mid + 1;\n } else {\n e = mid - 1;\n\n }\n }\n return ans;\n }\n};", "memory": "334883" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "int sf[401][401];\nint dp[401][401];\n\nclass Solution {\npublic:\n void safecompute(vector<vector<int>>& grid) {\n int n = grid.size();\n queue<pair<int,int>> q;\n for(int i=0;i<n;i++) {\n for(int j=0;j<n;j++) {\n if(grid[i][j] == 1) {\n q.push({i,j});\n }\n }\n }\n int cnt = 0;\n vector<vector<int>> visited(n,vector<int>(n));\n while(!q.empty()) {\n int size = q.size();\n while(size--) {\n int x = q.front().first;\n int y = q.front().second;\n q.pop();\n if(visited[x][y] == 1) continue;\n sf[x][y] = cnt;\n\n vector<int> vx = {0,0,-1,1};\n vector<int> vy = {1,-1,0,0};\n visited[x][y] = 1;\n for(int i=0;i<4;i++) {\n int nx = x+vx[i];\n int ny = y+vy[i];\n\n if(nx >= 0 && ny >=0 && nx < n && ny < n && visited[nx][ny] == 0) {\n q.push({nx, ny});\n }\n }\n }\n cnt++;\n }\n }\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n\n for(int i=0;i<n;i++) {\n for(int j=0;j<n;j++) {\n sf[i][j] = 1e8;\n }\n }\n \n safecompute(grid);\n priority_queue<pair<int,pair<int,int>>> pq;\n pq.push({sf[0][0], {0,0}});\n vector<vector<int>> visited(n, vector<int>(n));\n while(!pq.empty()) {\n int safeness = pq.top().first;\n int x = pq.top().second.first;\n int y = pq.top().second.second;\n\n pq.pop();\n if(x == n-1 && y == n-1) {\n return safeness;\n }\n if(visited[x][y] == 1) continue;\n visited[x][y] = 1;\n vector<int> vx = {0,0,-1,1};\n vector<int> vy = {1,-1,0,0};\n\n for(int k=0;k<4;k++) {\n int nx = x+vx[k];\n int ny = y+vy[k];\n\n if(nx >=0 && nx < n && ny >=0 && ny < n && visited[nx][ny] == 0) {\n pq.push({min(safeness, sf[nx][ny]), {nx,ny}});\n }\n }\n }\n\n return 0;\n }\n};", "memory": "339566" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dx[4] = {1,-1,0,0};\n int dy[4] = {0,0,-1,1};\n bool bfs(vector<vector<int>>&grid,vector<vector<int>>&dist,int x){\n int n = grid.size();\n vector<vector<int>>vis(n,vector<int>(n));\n vis[0][0] = 1;\n queue<pair<int,int>>q;\n q.push({0,0});\n if(dist[0][0]<x) return false;\n while(!q.empty()){\n auto cur = q.front();\n q.pop();\n int r = cur.first;\n int c = cur.second;\n if(r==n-1 && c==n-1){\n return true;\n }\n for(int k=0;k<4;k++){\n int nr = dx[k]+r;\n int nc = dy[k]+c;\n if(nr>=0 && nr<n && nc>=0 && nc<n && !vis[nr][nc] && dist[nr][nc]>=x){\n vis[nr][nc] = 1;\n q.push({nr,nc});\n }\n }\n }\n return false;\n\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n queue<pair<int,pair<int,int>>>q;\n vector<vector<int>>vis(n,vector<int>(n));\n vector<vector<int>>dist(n,vector<int>(n,1e9));\n for(int i = 0;i<n;i++){\n for(int j = 0;j<n;j++){\n if(grid[i][j]==0) continue;\n dist[i][j] = 0;\n vis[i][j]=1;\n q.push({dist[i][j],{i,j}});\n }\n }\n while(!q.empty()){\n auto cur = q.front();\n q.pop();\n int dis = cur.first;\n int r = cur.second.first;\n int c = cur.second.second;\n for(int k=0;k<4;k++){\n int nr = dx[k]+r;\n int nc = dy[k]+c;\n if(nr>=0 && nr<n && nc>=0 && nc<n && !vis[nr][nc]){\n vis[nr][nc] = 1;\n dist[nr][nc] = 1+dist[r][c];\n q.push({dist[nr][nc],{nr,nc}});\n }\n }\n\n }\n int lo = 0 , hi = n*n;\n int ans = 0;\n while(hi-lo>1){\n int mid = (lo+hi)/2;\n if(bfs(grid,dist,mid)){\n ans = max(ans,mid);\n lo = mid;\n }\n else{\n hi = mid-1;\n }\n }\n if(bfs(grid,dist,hi)) ans = max(ans,hi);\n return ans;\n\n\n }\n};", "memory": "344248" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n,m;\n vector<vector<int>>dist;\n vector<vector<int>>dir{ {-1,0} , {0,1} , {1,0} , {0,-1}};\n \n void preComputeMinDist_bfs(vector<vector<int>>&grid){\n \n queue<vector<int>>q;\n vector<vector<bool>>vis(n,vector<bool>(m,false));\n \n for(int i = 0 ; i < grid.size() ; i++){\n for(int j = 0 ; j < grid[i].size() ; j++){\n if(grid[i][j] == 1){\n vis[i][j] = true;\n q.push({i,j,0});\n dist[i][j] = 0;\n }\n }\n }\n\n while(!q.empty()){\n auto v = q.front();\n q.pop();\n\n int row = v[0];\n int col = v[1];\n int d = v[2];\n\n for(int i = 0 ; i < dir.size() ; i++){\n int r = dir[i][0];\n int c = dir[i][1];\n if(isValid(row+r,col+c) == true and vis[row+r][col+c] == false){\n vis[row+r][col+c] = true;\n dist[row+r][col+c] = d+1;\n q.push({row+r,col+c,d+1});\n }\n }\n }\n //print(dist);\n return;\n\n }\n void print(vector<vector<int>>&arr){\n for(int i = 0 ; i < arr.size() ; i++){\n for(int j = 0 ; j < arr[i].size() ; j++){\n cout<<arr[i][j]<<\" \";\n }\n cout<<endl;\n }\n cout<<endl<<endl;\n\n return;\n }\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n if(grid[0][0] == 1 or grid[grid.size()-1][grid[0].size()-1] == 1) \n return 0;\n\n n = grid.size();\n m = grid[0].size();\n\n dist.resize(n,vector<int>(m,INT_MAX)); //to keep the min dis of cell from theif\n preComputeMinDist_bfs(grid);\n\n int start = 0;\n int end = INT_MAX;\n int ans = end;\n \n while(start <= end){\n int mid = (0LL + start + end)/(1LL * 2);\n if(bfs(mid) == true){\n ans = mid;\n start = mid+1; //we will try to maximize distance \n }\n else{\n end = mid -1; //we will try to minimize distance\n }\n //cout<<\"ans = \"<<ans<<endl<<endl<<endl;\n }\n return ans;\n }\n //dist[row][col] = x\n //x is the minimum distance from any theive in the (grid) to the cell row col in the (path)\n bool bfs(int &mid){\n //cout<<\"mid = \"<<mid<<endl<<endl<<endl;\n\n queue<pair<int,int>>q;\n vector<vector<bool>>vis(n,vector<bool>(m,false));\n \n if(dist[0][0] >= mid){\n q.push({0,0});\n vis[0][0] = true;\n }\n else return false;\n\n while(!q.empty()){\n\n auto p = q.front(); q.pop();\n int row = p.first;\n int col = p.second;\n\n if(row == n-1 and col == m-1)\n return true;\n \n for(int i = 0 ; i < dir.size() ; i++){\n int r = dir[i][0];\n int c = dir[i][1];\n //cout<<\"row = \"<<row<<\" \"<<\"col = \"<<col<<endl;\n\n if(isValid(row+r,col+c) == true and vis[row+r][col+c] == false and dist[row+r][col+c] >= mid){\n //cout<<\"row+r = \"<<row+r<<\" \"<<\"col+c = \"<<col+c<<endl;\n\n q.push({row+r,col+c});\n vis[row+r][col+c] = true;\n }\n } \n \n }\n return false;\n }\n bool isValid(int row , int col){\n if(row >= 0 and row < n and col >=0 and col < m){\n return true;\n }\n return false;\n }\n};", "memory": "372343" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canReachEnd(vector<vector<int>>& safe, int minSafety) {\n int n = safe.size();\n vector<vector<int>> vis(n, vector<int>(n, 0));\n queue<pair<int, int>> q;\n \n if (safe[0][0] < minSafety) return false;\n \n q.push({0, 0});\n vis[0][0] = 1;\n \n int dr[] = {0, 1, 0, -1};\n int dc[] = {1, 0, -1, 0};\n \n while (!q.empty()) {\n auto [r, c] = q.front();\n q.pop();\n \n if (r == n - 1 && c == n - 1) return true;\n \n for (int i = 0; i < 4; ++i) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n \n if (nr >= 0 && nr < n && nc >= 0 && nc < n && !vis[nr][nc] && safe[nr][nc] >= minSafety) {\n vis[nr][nc] = 1;\n q.push({nr, nc});\n }\n }\n }\n \n return false;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> safe(n, vector<int>(n, 1e9));\n queue<vector<int>> pq;\n \n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n if (grid[i][j] == 1) {\n pq.push({0, i, j});\n safe[i][j] = 0;\n }\n }\n }\n \n while (!pq.empty()) {\n int curr = pq.front()[0];\n int r = pq.front()[1];\n int c = pq.front()[2];\n pq.pop();\n \n int dr[] = {0, 1, 0, -1};\n int dc[] = {1, 0, -1, 0};\n \n for (int i = 0; i < 4; ++i) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n \n if (nr >= 0 && nr < n && nc >= 0 && nc < n && safe[nr][nc] == 1e9) {\n safe[nr][nc] = curr + 1;\n pq.push({curr + 1, nr, nc});\n }\n }\n }\n \n int low = 0, high = safe[0][0], ans = 0;\n while (low <= high) {\n int mid = (low + high) / 2;\n if (canReachEnd(safe, mid)) {\n ans = mid;\n low = mid + 1;\n } else {\n high = mid - 1;\n }\n }\n \n return ans;\n }\n};\n", "memory": "372343" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = int(grid.size());\n\n vector<vector<int>> dists(n, vector<int>(n, -1));\n\n queue<pair<int, int>> q;\n\n int oneCount = 0;\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if (grid[i][j] == 1)\n {\n q.push({ i,j });\n dists[i][j] = 0;\n }\n }\n }\n\n while (!q.empty())\n {\n auto t = q.front(); q.pop();\n int x = t.first, y = t.second;\n int dist = dists[x][y];\n\n if (y - 1 >= 0 && dists[x][y - 1] == -1) dists[x][y - 1] = dist + 1, q.push({ x,y - 1 });\n if (y + 1 < n && dists[x][y + 1] == -1) dists[x][y + 1] = dist + 1, q.push({ x,y + 1 });\n if (x - 1 >= 0 && dists[x - 1][y] == -1) dists[x - 1][y] = dist + 1, q.push({ x - 1,y });\n if (x + 1 < n && dists[x + 1][y] == -1) dists[x + 1][y] = dist + 1, q.push({ x + 1,y });\n\n }\n\n int left=0,right = n,r=0;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n while(left<=right)\n {\n int test = (left+right)/2;\n\n if(feasible(dists,n,test,visited))\n {\n r=test;\n left=test+1;\n }\n else\n right=test-1;\n }\n\n return r;\n\n }\ndeque<pair<int, int>> q;\n bool feasible(const vector<vector<int>>& dists, int n, int testDist,vector<vector<bool>> &visited)\n {\n q.clear();\n q.push_back({0,0});\n\n for(auto& v:visited) v.assign(n,false);\n\n \n\n while (!q.empty())\n {\n auto t = q.front(); q.pop_front();\n int x = t.first, y = t.second;\n\n if(visited[x][y]) continue;\n\n int dist = dists[x][y];\n if(dist<testDist) continue;\n\n if(x==n-1&&y==n-1) return true;\n\n if (y - 1 >= 0) q.push_back({ x,y - 1 });\n if (y + 1 < n) q.push_back({ x,y + 1 });\n if (x - 1 >= 0) q.push_back({ x - 1,y });\n if (x + 1 < n) q.push_back({ x + 1,y });\n\n visited[x][y] = true;\n\n }\n\n return false;\n }\n\n};", "memory": "377026" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = int(grid.size());\n\n vector<vector<int>> dists(n, vector<int>(n, -1));\n\n queue<pair<int, int>> q;\n\n int oneCount = 0;\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if (grid[i][j] == 1)\n {\n q.push({ i,j });\n dists[i][j] = 0;\n }\n }\n }\n\n while (!q.empty())\n {\n auto t = q.front(); q.pop();\n int x = t.first, y = t.second;\n int dist = dists[x][y];\n\n if (y - 1 >= 0 && dists[x][y - 1] == -1) dists[x][y - 1] = dist + 1, q.push({ x,y - 1 });\n if (y + 1 < n && dists[x][y + 1] == -1) dists[x][y + 1] = dist + 1, q.push({ x,y + 1 });\n if (x - 1 >= 0 && dists[x - 1][y] == -1) dists[x - 1][y] = dist + 1, q.push({ x - 1,y });\n if (x + 1 < n && dists[x + 1][y] == -1) dists[x + 1][y] = dist + 1, q.push({ x + 1,y });\n\n }\n\n int left=0,right = n,r=0;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n while(left<=right)\n {\n int test = (left+right)/2;\n\n if(feasible(dists,n,test,visited))\n {\n r=test;\n left=test+1;\n }\n else\n right=test-1;\n }\n\n return r;\n\n }\n\n bool feasible(const vector<vector<int>>& dists, int n, int testDist,vector<vector<bool>> &visited)\n {\n queue<pair<int, int>> q;\n q.push({0,0});\n\n for(auto& v:visited) v.assign(n,false);\n\n \n\n while (!q.empty())\n {\n auto t = q.front(); q.pop();\n int x = t.first, y = t.second;\n\n if(visited[x][y]) continue;\n\n int dist = dists[x][y];\n if(dist<testDist) continue;\n\n if(x==n-1&&y==n-1) return true;\n\n if (y - 1 >= 0) q.push({ x,y - 1 });\n if (y + 1 < n) q.push({ x,y + 1 });\n if (x - 1 >= 0) q.push({ x - 1,y });\n if (x + 1 < n) q.push({ x + 1,y });\n\n visited[x][y] = true;\n\n }\n\n return false;\n }\n\n};", "memory": "377026" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = int(grid.size());\n\n vector<vector<int>> dists(n, vector<int>(n, -1));\n\n queue<pair<int, int>> q;\n\n int oneCount = 0;\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if (grid[i][j] == 1)\n {\n q.push({ i,j });\n dists[i][j] = 0;\n }\n }\n }\n\n while (!q.empty())\n {\n auto t = q.front(); q.pop();\n int x = t.first, y = t.second;\n int dist = dists[x][y];\n\n if (y - 1 >= 0 && dists[x][y - 1] == -1) dists[x][y - 1] = dist + 1, q.push({ x,y - 1 });\n if (y + 1 < n && dists[x][y + 1] == -1) dists[x][y + 1] = dist + 1, q.push({ x,y + 1 });\n if (x - 1 >= 0 && dists[x - 1][y] == -1) dists[x - 1][y] = dist + 1, q.push({ x - 1,y });\n if (x + 1 < n && dists[x + 1][y] == -1) dists[x + 1][y] = dist + 1, q.push({ x + 1,y });\n\n }\n\n int left=0,right = 2*n,r=0;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n while(left<=right)\n {\n int test = (left+right)/2;\n\n if(feasible(dists,n,test,visited))\n {\n r=test;\n left=test+1;\n }\n else\n right=test-1;\n }\n\n return r;\n\n }\ndeque<pair<int, int>> q;\n bool feasible(const vector<vector<int>>& dists, int n, int testDist,vector<vector<bool>> &visited)\n {\n q.clear();\n q.push_back({0,0});\n\n for(auto& v:visited) v.assign(n,false);\n \n\n while (!q.empty())\n {\n auto t = q.front(); q.pop_front();\n int x = t.first, y = t.second;\n\n if(visited[x][y]) continue;\n\n int dist = dists[x][y];\n if(dist<testDist) continue;\n\n if(x==n-1&&y==n-1) return true;\n\n if (y - 1 >= 0) q.push_back({ x,y - 1 });\n if (y + 1 < n) q.push_back({ x,y + 1 });\n if (x - 1 >= 0) q.push_back({ x - 1,y });\n if (x + 1 < n) q.push_back({ x + 1,y });\n\n visited[x][y] = true;\n\n }\n\n return false;\n }\n\n};", "memory": "381708" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = int(grid.size());\n\n vector<vector<int>> dists(n, vector<int>(n, -1));\n\n queue<pair<int, int>> q;\n\n int oneCount = 0;\n for (int i = 0; i < n; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if (grid[i][j] == 1)\n {\n q.push({ i,j });\n dists[i][j] = 0;\n }\n }\n }\n\n while (!q.empty())\n {\n auto t = q.front(); q.pop();\n int x = t.first, y = t.second;\n int dist = dists[x][y];\n\n if (y - 1 >= 0 && dists[x][y - 1] == -1) dists[x][y - 1] = dist + 1, q.push({ x,y - 1 });\n if (y + 1 < n && dists[x][y + 1] == -1) dists[x][y + 1] = dist + 1, q.push({ x,y + 1 });\n if (x - 1 >= 0 && dists[x - 1][y] == -1) dists[x - 1][y] = dist + 1, q.push({ x - 1,y });\n if (x + 1 < n && dists[x + 1][y] == -1) dists[x + 1][y] = dist + 1, q.push({ x + 1,y });\n\n }\n\n int left=0,right = n,r=0;\n\n while(left<=right)\n {\n int test = (left+right)/2;\n\n if(feasible(dists,n,test))\n {\n r=test;\n left=test+1;\n }\n else\n right=test-1;\n }\n\n return r;\n\n }\n\n bool feasible(const vector<vector<int>>& dists, int n, int testDist)\n {\n queue<pair<int, int>> q;\n q.push({0,0});\n\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n\n while (!q.empty())\n {\n auto t = q.front(); q.pop();\n int x = t.first, y = t.second;\n\n if(visited[x][y]) continue;\n\n int dist = dists[x][y];\n if(dist<testDist) continue;\n\n if(x==n-1&&y==n-1) return true;\n\n if (y - 1 >= 0) q.push({ x,y - 1 });\n if (y + 1 < n) q.push({ x,y + 1 });\n if (x - 1 >= 0) q.push({ x - 1,y });\n if (x + 1 < n) q.push({ x + 1,y });\n\n visited[x][y] = true;\n\n }\n\n return false;\n }\n\n};", "memory": "381708" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool Check(int i, int j, int n){\n return i < n && j < n && i >= 0 && j >= 0;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n std::queue<std::vector<int>> q;\n int n = grid.size();\n vector<vector<int>> safety(n, vector<int>(n, INT_MAX));\n for (int i = 0; i < n; i++){\n for (int j = 0; j < n; j++){\n if (grid[i][j] == 1){\n safety[i][j] = 0;\n q.push({i, j});\n }\n }\n }\n int x[] = {1, -1, 0, 0};\n int y[] = {0, 0, 1, -1};\n while (!q.empty()){\n int size = q.size();\n for (int k = 0; k < size; k++){\n auto pos = q.front();\n q.pop();\n for (int i = 0; i < 4; i++){\n int ni = pos[0] + x[i];\n int nj = pos[1] + y[i];\n if (Check(ni, nj, n) && safety[ni][nj] > safety[pos[0]][pos[1]] + 1){\n safety[ni][nj] = safety[pos[0]][pos[1]] + 1;\n q.push({ni, nj});\n }\n }\n }\n }\n vector<vector<int>> visit(n, vector<int>(n, 0));\n priority_queue<pair<int, vector<int>>> pq;\n pq.push({safety[0][0], {0, 0}});\n visit[0][0] = 1;\n while (!pq.empty()){\n auto pos = pq.top();\n pq.pop();\n if (pos.second[0] == n - 1 && pos.second[1] == n - 1){\n return pos.first;\n }\n for (int i = 0; i < 4; i++){\n int ni = pos.second[0] + x[i];\n int nj = pos.second[1] + y[i];\n if (Check(ni, nj, n) && !visit[ni][nj]){\n int s = min(safety[ni][nj], pos.first);\n visit[ni][nj] = 1;\n pq.push({s, {ni, nj}});\n }\n }\n }\n return 0;\n }\n};", "memory": "386391" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<vector<int>> &grid)\n {\n int n = grid.size();\n int m = grid[0].size();\n\n vector<vector<int>> safeness(n, vector<int>(m, INT_MIN));\n safeness[0][0] = grid[0][0];\n\n priority_queue<vector<int>> pq;\n pq.push({grid[0][0], 0, 0});\n\n while(!pq.empty())\n {\n vector<int> tmp = pq.top();\n pq.pop();\n\n int currentSafeness = tmp[0];\n int i = tmp[1], j = tmp[2];\n\n if(i == n-1 && j == m-1)\n return currentSafeness;\n\n vector<vector<int>> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};\n\n for(int d=0; d<4; d++)\n {\n int x = i + dir[d][0];\n int y = j + dir[d][1];\n\n if(x >= 0 && y >= 0 && x < n && y < m)\n {\n int newSafeness = min(grid[x][y], currentSafeness);\n if(newSafeness > safeness[x][y])\n {\n safeness[x][y] = newSafeness;\n pq.push({newSafeness, x, y});\n }\n }\n }\n }\n\n return -1;\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n \n int n = grid.size();\n int m = grid[0].size();\n queue<pair<int,int>> q;\n\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<m; j++)\n {\n if(grid[i][j] == 0) grid[i][j] = INT_MAX;\n else \n {\n grid[i][j] = 0;\n q.push({i, j});\n }\n }\n }\n\n while(!q.empty())\n {\n pair<int,int> tmp = q.front();\n q.pop();\n\n int i = tmp.first, j = tmp.second;\n\n if((i-1) >= 0 && grid[i-1][j] > 1 + grid[i][j])\n grid[i-1][j] = grid[i][j] + 1, q.push({i-1, j});\n\n if((j-1) >= 0 && grid[i][j-1] > grid[i][j] + 1)\n grid[i][j-1] = grid[i][j] + 1, q.push({i, j-1});\n\n if((i+1) < n && grid[i+1][j] > grid[i][j] + 1)\n grid[i+1][j] = grid[i][j] + 1, q.push({i+1, j});\n\n if((j+1) < m && grid[i][j+1] > grid[i][j] + 1)\n grid[i][j+1] = grid[i][j] + 1, q.push({i, j+1});\n }\n\n vector<vector<bool>> vis(n, vector<bool>(m, false));\n int ans = solve(grid);\n return ans;\n }\n};", "memory": "386391" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool valid(int row,int col,int n,int m){\n if(row<0 ||row>=n || col<0 || col>=m){\n return false;\n }\n return true;\n }\n bool check(int mid,int n,int m,vector<vector<int>>&dis){\n if(dis[0][0]<mid)return false;\n queue<pair<int,int>>q;\n q.push({0,0});\n int delrow[4]={0,1,0,-1};\n int delcol[4]={1,0,-1,0};\n vector<vector<int>>vis(n,vector<int>(m,0));\n vis[0][0]=1;\n int flag=0;\n while(!q.empty()){\n auto it=q.front();\n int row=it.first;\n int col=it.second;\n q.pop();\n if(row==n-1 && col==n-1){\n flag=1;\n break;\n }\n for(int i=0;i<4;i++){\n int nrow=row+delrow[i];\n int ncol=col+delcol[i];\n if(valid(nrow,ncol,n,m) && vis[nrow][ncol]==0 && dis[nrow][ncol]>=mid){\n vis[nrow][ncol]=1;\n q.push({nrow,ncol});\n }\n }\n }\n if(flag==1){\n return true;\n }\n return false;\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n queue<pair<pair<int,int>,int>>q;\n int n=grid.size();\n int m=grid[0].size();\n map<pair<int,int>,int>mp2;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n q.push({{i,j},0});\n mp2[{i,j}]=1;\n }\n }\n }\n vector<vector<int>>dis(n,vector<int>(m,1e9+7));\n int delrow[4]={0,1,0,-1};\n int delcol[4]={1,0,-1,0};\n while(!q.empty()){\n auto it=q.front();\n int row=it.first.first;\n int col=it.first.second;\n int curr_dis=it.second;\n q.pop();\n for(int i=0;i<4;i++){\n int nrow=row+delrow[i];\n int ncol=col+delcol[i];\n if(valid(nrow,ncol,n,m) && mp2[{nrow,ncol}]==0){\n if(curr_dis+1<dis[nrow][ncol]){\n dis[nrow][ncol]=curr_dis+1;\n q.push({{nrow,ncol},dis[nrow][ncol]});\n }\n }\n }\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(dis[i][j]==1e9+7){\n dis[i][j]=0;\n }\n // cout<<dis[i][j]<<\" \";\n }\n // cout<<endl;\n }\n // cout<<endl;\n int lo=0;\n int hi=2*n;\n int ans=0;\n while(lo<=hi){\n int mid=lo+(hi-lo)/2;\n if(check(mid,n,m,dis)){\n lo=mid+1;\n ans=mid;\n }\n else{\n hi=mid-1;\n }\n }\n return ans;\n }\n};", "memory": "391073" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void relax(int& r,int& c,vector<vector<int>>& safeD,vector<vector<int>>& grid,queue<pair<int,int>>& q)\n {\n int i,j,n=grid.size();\n q.push({r,c});\n q.push({-1,-1});\n\n int dist=0;\n while(!q.empty())\n {\n i=q.front().first;\n j=q.front().second;\n q.pop();\n\n if(i==-1){\n dist++;\n if(!q.empty()) q.push({-1,-1});\n continue;\n }\n\n if(i-1 >=0 && grid[i-1][j]==0 && dist+1<safeD[i-1][j]) {\n safeD[i-1][j]=dist+1;\n q.push({i-1,j});\n }\n if(j-1 >=0 && grid[i][j-1]==0 && dist+1<safeD[i][j-1]) {\n safeD[i][j-1]=dist+1;\n q.push({i,j-1});\n }\n if(i+1 < n && grid[i+1][j]==0 && dist+1<safeD[i+1][j]) {\n safeD[i+1][j]=dist+1;\n q.push({i+1,j});\n }\n if(j+1 < n && grid[i][j+1]==0 && dist+1<safeD[i][j+1]) {\n safeD[i][j+1]=dist+1;\n q.push({i,j+1});\n }\n }\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) \n {\n int i,j,n=grid.size();\n vector<vector<int>> safeD(n,vector<int>(n,INT_MAX));\n\n for(i=0;i<n;i++){\n for(j=0;j<n;j++){\n if(grid[i][j]==1) safeD[i][j]=0;\n }\n } \n\n queue<pair<int,int>> q;\n\n for(i=0;i<n;i++)\n {\n for(j=0;j<n;j++)\n {\n if(grid[i][j]==1) relax(i,j,safeD,grid,q);\n }\n }\n\n priority_queue<pair<int,pair<int,int>>> pq;\n pq.push({safeD[0][0],{0,0}});\n grid[0][0]=-1;\n\n int safe;\n while(!pq.empty())\n {\n int val=pq.top().first;\n i=pq.top().second.first;\n j=pq.top().second.second;\n if(i==n-1 && j==n-1) return val;\n\n pq.pop();\n // right and bottom i+1,j+1\n\n if(i-1 >= 0 && grid[i-1][j]!=-1){\n safe=safeD[i-1][j];\n grid[i-1][j]=-1;\n pq.push({min(safe,val),{i-1,j}});\n }\n\n if(j-1 >= 0 && grid[i][j-1]!=-1){\n safe=safeD[i][j-1];\n grid[i][j-1]=-1;\n pq.push({min(safe,val),{i,j-1}});\n }\n\n if(i+1 < n && grid[i+1][j]!=-1){\n safe=safeD[i+1][j];\n grid[i+1][j]=-1;\n pq.push({min(safe,val),{i+1,j}});\n }\n\n if(j+1 < n && grid[i][j+1]!=-1){\n safe=safeD[i][j+1];\n grid[i][j+1]=-1;\n pq.push({min(val,safe),{i,j+1}});\n }\n }\n return 0;\n }\n};", "memory": "391073" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> dir = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n void dfs(vector<vector<int>>& grid, vector<vector<int>>& score) {\n queue<pair<int, int>> q;\n\n for(int i=0; i<n; i++) {\n for(int j=0; j<n; j++) {\n if(grid[i][j]) {\n score[i][j] = 0;\n q.push({i, j});\n }\n }\n }\n\n while(!q.empty()) {\n auto temp = q.front();\n q.pop();\n\n int x = temp.first;\n int y = temp.second;\n int s = score[x][y];\n\n for(auto d : dir) {\n int newX = x + d[0];\n int newY = y + d[1];\n\n if(newX>=0 && newX<n && newY>=0 && newY<n && score[newX][newY] > 1+s) {\n score[newX][newY] = 1+s;\n q.push({newX, newY});\n }\n }\n }\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n n = grid.size();\n if(grid[0][0] == 1 || grid[n-1][n-1] == 1) return 0;\n\n vector<vector<int>> score(n, vector<int>(n, INT_MAX));\n\n // calculate the score\n dfs(grid, score);\n\n vector<vector<int>> vis(n, vector<int>(n, 0));\n\n priority_queue<pair<int, pair<int, int>>> pq;\n pq.push({score[0][0], {0, 0}});\n\n while(!pq.empty()) {\n auto top = pq.top();\n pq.pop();\n\n int x = top.second.first;\n int y = top.second.second;\n int safe = top.first;\n\n if(x == n-1 && y == n-1) return safe;\n vis[x][y] = 1;\n\n for(auto d : dir) {\n int newX = x + d[0];\n int newY = y + d[1];\n\n if(newX >= 0 && newX < n && newY >= 0 && newY < n && !vis[newX][newY]) {\n int s = min(safe, score[newX][newY]);\n pq.push({s, {newX, newY}});\n vis[newX][newY] = true; \n }\n }\n\n }\n return -1;\n }\n};", "memory": "395756" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n if(grid[0][0] == 1 || grid[m-1][n-1] == 1){\n return 0 ;\n }\n queue<pair<int, int>> q;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({i,j});\n grid[i][j] = 0;\n }\n else{\n grid[i][j] = -1;\n }\n }\n }\n while (!q.empty()) {\n int sz = q.size();\n for(int i =0;i<sz;i++){\n auto fir = q.front();\n q.pop();\n for(auto d : dir){\n int x1= fir.first + d[0];\n int y1 = fir.second + d[1];\n if(x1>=0 && x1<m && y1>=0 && y1< n && grid[x1][y1] == -1){\n grid[x1][y1] = grid[fir.first][fir.second]+1;\n q.push({x1,y1});\n }\n }\n }\n }\n priority_queue<vector<int>> pq;\n pq.push({grid[0][0],0,0});\n grid[0][0] = -1;\n while(!pq.empty()){\n auto curr = pq.top();\n pq.pop();\n for(auto itr : dir){\n int x1= itr[0] + curr[1];\n int y1= itr[1] + curr[2];\n if(x1 >=0 && x1<m && y1>=0 && y1<n && grid[x1][y1] != -1){\n int sf = min(grid[x1][y1] , curr[0]);\n grid[x1][y1] = -1;\n if(x1 == m-1 && y1 == n-1){\n return sf;\n }\n pq.push({sf , x1,y1});\n }\n }\n }\n return 0;\n }\n};", "memory": "400438" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n if(grid[0][0] == 1 || grid[m-1][n-1] == 1){\n return 0 ;\n }\n queue<pair<int, int>> q;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == 1) {\n q.push({i,j});\n grid[i][j] = 0;\n }\n else{\n grid[i][j] = -1;\n }\n }\n }\n while (!q.empty()) {\n int sz = q.size();\n for(int i =0;i<sz;i++){\n auto fir = q.front();\n q.pop();\n for(auto d : dir){\n int x1= fir.first + d[0];\n int y1 = fir.second + d[1];\n if(x1>=0 && x1<m && y1>=0 && y1< n && grid[x1][y1] == -1){\n grid[x1][y1] = grid[fir.first][fir.second]+1;\n q.push({x1,y1});\n }\n }\n }\n }\n priority_queue<vector<int>> pq;\n pq.push({grid[0][0],0,0});\n grid[0][0] = 1;\n while(!pq.empty()){\n auto curr = pq.top();\n pq.pop();\n for(auto itr : dir){\n int x1= itr[0] + curr[1];\n int y1= itr[1] + curr[2];\n if(x1 >=0 && x1<m && y1>=0 && y1<n && grid[x1][y1] != -1){\n int sf = min(grid[x1][y1] , curr[0]);\n grid[x1][y1] = -1;\n if(x1 == m-1 && y1 == n-1){\n return sf;\n }\n pq.push({sf , x1,y1});\n }\n }\n }\n return 0;\n }\n};", "memory": "400438" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class DSU {\npublic:\n DSU(int n) {\n prnt = vector<int>(n + 1);\n val = vector<int>(n + 1);\n for (int i = 1; i <= n; i++) {\n prnt[i] = i;\n val[i] = 1;\n }\n }\n int root(int x) {\n if (x != prnt[x]) {\n return prnt[x] = root(prnt[x]);\n }\n return x;\n }\n\n bool merge(int x, int y) {\n int px = root(x), py = root(y);\n if (px == py) {\n return false;\n }\n if (val[px] > val[py]) {\n swap(py, px);\n }\n prnt[px] = py;\n val[py] += val[px];\n return true;\n }\n vector<int> prnt, val;\n};\n\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> dis(n, vector<int>(n, 1e9));\n queue<pair<int, int>> q;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j]) {\n dis[i][j] = 0;\n q.push({i, j});\n }\n }\n }\n auto valid = [&](int x, int y) -> bool {\n if (x >= 0 && y >= 0 && x < n && y < n && dis[x][y] == 1e9) {\n return true;\n }\n return false;\n };\n\n while (!q.empty()) {\n int x = q.front().first, y = q.front().second;\n q.pop();\n if (valid(x, y + 1)) {\n q.push({x, y + 1});\n dis[x][y + 1] = dis[x][y] + 1;\n }\n if (valid(x, y - 1)) {\n q.push({x, y - 1});\n dis[x][y - 1] = dis[x][y] + 1;\n }\n if (valid(x + 1, y)) {\n q.push({x + 1, y});\n dis[x + 1][y] = dis[x][y] + 1;\n }\n if (valid(x - 1, y)) {\n q.push({x - 1, y});\n dis[x - 1][y] = dis[x][y] + 1;\n }\n }\n\n vector<vector<int>> all;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n all.push_back({dis[i][j], i, j});\n }\n }\n sort(all.begin(), all.end());\n reverse(all.begin(), all.end());\n DSU dsu(n * n);\n map<pair<int, int>, bool> appeared;\n const int last = n * n - 1;\n for (auto v : all) {\n int d = v[0], x = v[1], y = v[2];\n appeared[{x, y}] = true;\n int idx = x * n + y;\n if (appeared[{x, y + 1}]) {\n dsu.merge(idx, idx + 1);\n }\n if (appeared[{x, y - 1}]) {\n dsu.merge(idx, idx - 1);\n }\n if (appeared[{x - 1, y}]) {\n dsu.merge(idx, idx - n);\n }\n if (appeared[{x + 1, y}]) {\n dsu.merge(idx, idx + n);\n }\n if (dsu.root(0) == dsu.root(last)) {\n return d;\n }\n }\n return -1;\n }\n};", "memory": "405121" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class DisjointSet{\nprivate:\n vector<int> par;\n vector<int> size;\npublic:\n DisjointSet(int n){\n size.resize(n+1,1);\n for(int i=0;i<=n;i++){\n par.push_back(i);\n }\n }\n\n int findUpar(int a){\n if(par[a]==a) return a;\n return par[a]=findUpar(a);\n }\n\n void Union(int a, int b){\n int par1=findUpar(a);\n int par2=findUpar(b);\n if(par1==par2) return;\n if(size[par1]>size[par2]){\n par[par2]=par2;\n size[par1]+=size[par2];\n }\n else{\n par[par1]=par2;\n size[par2]+=size[par1];\n }\n }\n};\n\nvoid bfs(int i,int j,vector<vector<int>>&grid,vector<vector<int>>&scores){\n vector<int> drow={1,-1,0,0};\n vector<int> dcol={0,0,1,-1};\n scores[i][j]=0;\n queue<pair<pair<int,int>,int>> q;\n q.push({{i,j},0});\n while(!q.empty()){\n int r=q.front().first.first;\n int c=q.front().first.second;\n int d=q.front().second;\n q.pop();\n for(int k=0;k<4;k++){\n int nr=r+drow[k];\n int nc=c+dcol[k];\n if(nr>=0 && nc>=0 && nr<grid.size() && nc<grid.size()){\n if(d+1<scores[nr][nc]){\n scores[nr][nc]=d+1;\n q.push({{nr,nc},d+1});\n }\n }\n }\n\n\n }\n}\nclass Solution {\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n=grid.size();\n if(grid[0][0]==1 || grid[n-1][n-1]==1) return 0;\n vector<vector<int>> scores(n,vector<int>(n,1e8));\n DisjointSet ds(n*n-1);\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(grid[i][j]==1){\n bfs(i,j,grid,scores);\n }\n }\n }\n vector<vector<int>>v(n,vector<int>(n,-1));\n v[0][0]=scores[0][0];\n priority_queue<pair<int,pair<int,int>>> pq;\n pq.push({scores[0][0],{0,0}});\n vector<int> drow={1,-1,0,0};\n \n vector<int> dcol={0,0,1,-1};\n while(!pq.empty()){\n int dis=pq.top().first;\n int r=pq.top().second.first;\n int c=pq.top().second.second;\n pq.pop();\n for(int k=0;k<4;k++){\n int nr=r+drow[k];\n int nc=c+dcol[k];\n if(nr>=0 && nc>=0 && nr<grid.size() && nc<grid.size() && grid[nr][nc]==0){\n int m=min(dis,scores[nr][nc]);\n if(m>v[nr][nc]){\n \n v[nr][nc]=m;\n pq.push({m,{nr,nc}});\n }\n }\n }\n\n\n }\n if(v[n-1][n-1]==-1) return 0;\n return v[n-1][n-1];\n\n }\n};", "memory": "409803" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n vector<int>dir={0,1,0,-1,0};\n bool isValid(int row,int col,int n){\n return row>=0 && col>=0 && row<n && col<n;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = (int)grid.size();\n if(n==0)return -1;\n vector<vector<int>>safeness(n,vector<int>(n,INT_MAX));\n queue<vector<int>>q;\n for (int row = 0; row < n; ++row) {\n for (int col = 0; col < n; ++col) {\n if(grid[row][col]==1){\n safeness[row][col]=0;\n q.push({row,col,0});\n }\n }\n }\n while(!q.empty()){\n auto cell = q.front();\n q.pop();\n auto row = cell[0], col = cell[1] ,dis = cell[2];\n for (int i = 0; i < 4; ++i) {\n int nextRow = row+dir[i];\n int nextCol = col+dir[i+1];\n if(!isValid(nextRow,nextCol,n) || safeness[nextRow][nextCol]!=INT_MAX)continue;\n safeness[nextRow][nextCol] = dis+1;\n q.push({nextRow,nextCol,safeness[nextRow][nextCol]});\n }\n }\n vector<vector<int>>vis(n,vector<int>(n));\n deque<vector<int>>d;\n int minSafety = safeness[0][0];\n d.push_front({safeness[0][0],0,0});\n vis[0][0]=1;\n while(!d.empty()){\n auto cell = d.front();\n d.pop_front();\n int safety=cell[0], row = cell[1], col=cell[2];\n minSafety = min(minSafety,safety);\n if(row==n-1 && col==n-1)break;\n for (int i = 0; i < 4; ++i) {\n int nextRow = row+dir[i];\n int nextCol = col+dir[i+1];\n if(!isValid(nextRow,nextCol,n) || vis[nextRow][nextCol])continue;\n vis[nextRow][nextCol]=1;\n if(safeness[nextRow][nextCol] >= minSafety){\n d.push_front({safeness[nextRow][nextCol],nextRow,nextCol});\n }\n else{\n d.push_back({safeness[nextRow][nextCol],nextRow,nextCol});\n }\n }\n }\n return minSafety;\n }\n};\n", "memory": "414486" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n vector<int>dir={0,1,0,-1,0};\n bool isValid(int row,int col,int n){\n return row>=0 && col>=0 && row<n && col<n;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = (int)grid.size();\n if(n==0)return -1;\n vector<vector<int>>safeness(n,vector<int>(n,INT_MAX));\n queue<vector<int>>q;\n for (int row = 0; row < n; ++row) {\n for (int col = 0; col < n; ++col) {\n if(grid[row][col]==1){\n safeness[row][col]=0;\n q.push({row,col,0});\n }\n }\n }\n while(!q.empty()){\n auto cell = q.front();\n q.pop();\n auto row = cell[0], col = cell[1] ,dis = cell[2];\n for (int i = 0; i < 4; ++i) {\n int nextRow = row+dir[i];\n int nextCol = col+dir[i+1];\n if(!isValid(nextRow,nextCol,n) || safeness[nextRow][nextCol]!=INT_MAX)continue;\n safeness[nextRow][nextCol] = dis+1;\n q.push({nextRow,nextCol,safeness[nextRow][nextCol]});\n }\n }\n vector<vector<int>>vis(n,vector<int>(n));\n deque<vector<int>>d;\n int minSafety = safeness[0][0];\n d.push_front({safeness[0][0],0,0});\n vis[0][0]=1;\n while(!d.empty()){\n auto cell = d.front();\n d.pop_front();\n int safety=cell[0], row = cell[1], col=cell[2];\n minSafety = min(minSafety,safety);\n if(row==n-1 && col==n-1)break;\n for (int i = 0; i < 4; ++i) {\n int nextRow = row+dir[i];\n int nextCol = col+dir[i+1];\n if(!isValid(nextRow,nextCol,n) || vis[nextRow][nextCol])continue;\n vis[nextRow][nextCol]=1;\n if(safeness[nextRow][nextCol] >= minSafety){\n d.push_front({safeness[nextRow][nextCol],nextRow,nextCol});\n }\n else{\n d.push_back({safeness[nextRow][nextCol],nextRow,nextCol});\n }\n }\n }\n return minSafety;\n }\n};\n", "memory": "419168" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\n vector<int>dir={0,1,0,-1,0};\n bool isValid(int row,int col,int n){\n return row>=0 && col>=0 && row<n && col<n;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = (int)grid.size();\n if(n==0)return -1;\n vector<vector<int>>safeness(n,vector<int>(n,INT_MAX));\n queue<vector<int>>q;\n for (int row = 0; row < n; ++row) {\n for (int col = 0; col < n; ++col) {\n if(grid[row][col]==1){\n safeness[row][col]=0;\n q.push({row,col,0});\n }\n }\n }\n while(!q.empty()){\n auto cell = q.front();\n q.pop();\n auto row = cell[0], col = cell[1] ,dis = cell[2];\n for (int i = 0; i < 4; ++i) {\n int nextRow = row+dir[i];\n int nextCol = col+dir[i+1];\n if(!isValid(nextRow,nextCol,n) || safeness[nextRow][nextCol]!=INT_MAX)continue;\n safeness[nextRow][nextCol] = dis+1;\n q.push({nextRow,nextCol,safeness[nextRow][nextCol]});\n }\n }\n vector<vector<int>>vis(n,vector<int>(n));\n deque<vector<int>>d;\n int minSafety = safeness[0][0];\n d.push_front({safeness[0][0],0,0});\n vis[0][0]=1;\n while(!d.empty()){\n auto cell = d.front();\n d.pop_front();\n int safety=cell[0], row = cell[1], col=cell[2];\n minSafety = min(minSafety,safety);\n if(row==n-1 && col==n-1)break;\n for (int i = 0; i < 4; ++i) {\n int nextRow = row+dir[i];\n int nextCol = col+dir[i+1];\n if(!isValid(nextRow,nextCol,n) || vis[nextRow][nextCol])continue;\n vis[nextRow][nextCol]=1;\n if(safeness[nextRow][nextCol]>=minSafety){\n d.push_front({safeness[nextRow][nextCol],nextRow,nextCol});\n }\n else{\n d.push_back({safeness[nextRow][nextCol],nextRow,nextCol});\n }\n }\n }\n return minSafety;\n }\n};\n", "memory": "419168" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int INF = 1e5;\n vector<int> dr = {-1, 1, 0, 0};\n vector<int> dc = {0, 0, 1, -1};\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> costs(n, vector<int>(n, INF));\n vector<vector<int>> dists(n, vector<int>(n));\n vector<pair<int,int>> thieves;\n queue<vector<int>> Q;\n priority_queue<vector<int>> PQ;\n\n for (int r = 0; r < n; r++)\n for (int c = 0; c < n; c++)\n if (grid[r][c])\n thieves.push_back({r,c});\n\n for (auto p : thieves) {\n Q.push({p.first, p.second, 0});\n costs[p.first][p.second] = 0;\n }\n \n while (!Q.empty()) {\n auto p = Q.front(); Q.pop();\n int r = p[0], c = p[1], d = p[2];\n for (int i = 0; i < 4; i++) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n if (in_bounds(nr, nc, n) and costs[nr][nc] == INF) {\n costs[nr][nc] = d + 1;\n Q.push({nr, nc, d + 1});\n }\n }\n }\n\n dists[0][0] = costs[0][0];\n PQ.push({dists[0][0], 0, 0});\n while (!PQ.empty()) {\n auto p = PQ.top(); PQ.pop();\n int r = p[1], c = p[2];\n for (int i = 0; i < 4; i++) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n\n if (!in_bounds(nr, nc, n))\n continue;\n\n int move_cost = min(dists[r][c], costs[nr][nc]);\n if (move_cost > dists[nr][nc]) {\n dists[nr][nc] = move_cost;\n PQ.push({move_cost, nr, nc});\n }\n }\n }\n\n return dists[n-1][n-1];\n }\n\n bool in_bounds(int r, int c, int n) {\n return r >= 0 and r < n and c >= 0 and c < n; \n }\n};", "memory": "451946" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void dfs(int i,int j,vector<vector<int>> &dis, vector<vector<int>> &vis,int mid)\n {\n if(dis[i][j]<mid) return;\n int n=dis.size();\n vis[i][j]=1;\n int dx[4]={0,0,1,-1};\n int dy[4]={1,-1,0,0};\n for(int k=0;k<4;k++)\n {\n int nx=i+dx[k];\n int ny=j+dy[k];\n if(nx>=0&&nx<n&&ny>=0&&ny<n&&!vis[nx][ny]&&dis[nx][ny]>=mid)\n dfs(nx,ny,dis,vis,mid);\n }\n }\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n=grid.size();\n vector<vector<int>> dis(n,vector<int>(n));\n queue<pair<int,int>> Q;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n dis[i][j]=INT_MAX;\n if(grid[i][j]==1){\n Q.push({i,j});\n dis[i][j]=0;\n }\n }\n }\n \n while(!Q.empty())\n {\n auto cur=Q.front();\n int x=cur.first;\n int y=cur.second;\n Q.pop();\n int dx[4]={0,0,1,-1};\n int dy[4]={1,-1,0,0};\n for(int k=0;k<4;k++)\n {\n int nx=x+dx[k];\n int ny=y+dy[k];\n if(nx>=0&&nx<n&&ny>=0&&ny<n&&dis[nx][ny]>dis[x][y]+1)\n {\n Q.push({nx,ny});\n dis[nx][ny]=dis[x][y]+1;\n }\n }\n }\n // for(int i=0;i<n;i++){\n // for(int j=0;j<n;j++)\n // cout<<dis[i][j]<<\" \";\n // cout<<\"\\n\";\n // }\n //BS on answer\n int l=0,r=INT_MAX,ans=r;\n while(l<=r)\n {\n int mid=(l+r)/2;\n vector<vector<int>> vis(n,vector<int>(n,0));\n dfs(0,0,dis,vis,mid);\n if(vis[n-1][n-1]) // all distances should be <= mid\n {\n ans=mid;\n l=mid+1;\n }\n else\n r=mid-1;\n }\n\n return ans;\n }\n};", "memory": "451946" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int INF = 1e5;\n vector<int> dr = {-1, 1, 0, 0};\n vector<int> dc = {0, 0, 1, -1};\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> costs(n, vector<int>(n, INF));\n vector<vector<int>> dists(n, vector<int>(n));\n vector<vector<int>> processed(n, vector<int>(n));\n vector<pair<int,int>> thieves;\n queue<vector<int>> Q;\n priority_queue<vector<int>> PQ;\n\n for (int r = 0; r < n; r++)\n for (int c = 0; c < n; c++)\n if (grid[r][c])\n thieves.push_back({r,c});\n\n for (auto p : thieves) {\n Q.push({p.first, p.second, 0});\n costs[p.first][p.second] = 0;\n }\n \n while (!Q.empty()) {\n auto p = Q.front(); Q.pop();\n int r = p[0], c = p[1], d = p[2];\n for (int i = 0; i < 4; i++) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n if (in_bounds(nr, nc, n) and costs[nr][nc] == INF) {\n costs[nr][nc] = d + 1;\n Q.push({nr, nc, d + 1});\n }\n }\n }\n\n dists[0][0] = costs[0][0];\n PQ.push({dists[0][0], 0, 0});\n while (!PQ.empty()) {\n auto p = PQ.top(); PQ.pop();\n int r = p[1], c = p[2];\n if (processed[r][c]) continue;\n processed[r][c] = 1;\n for (int i = 0; i < 4; i++) {\n int nr = r + dr[i];\n int nc = c + dc[i];\n\n if (!in_bounds(nr, nc, n))\n continue;\n\n int move_cost = min(dists[r][c], costs[nr][nc]);\n if (move_cost > dists[nr][nc]) {\n dists[nr][nc] = move_cost;\n PQ.push({move_cost, nr, nc});\n }\n }\n }\n\n return dists[n-1][n-1];\n }\n\n bool in_bounds(int r, int c, int n) {\n return r >= 0 and r < n and c >= 0 and c < n; \n }\n};", "memory": "456628" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void bfs(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n \n queue<vector<int>> q;\n \n vector<vector<int>> vis(m, vector<int>(n, 0));\n \n for(int i=0; i<m; i++) {\n for(int j=0; j<n; j++) {\n if(grid[i][j]) {\n grid[i][j] = 0;\n q.push({0, i, j});\n vis[i][j] = 1;\n }\n }\n }\n \n while(!q.empty()) {\n auto temp = q.front();\n q.pop();\n \n int dis = temp[0];\n int x = temp[1];\n int y = temp[2];\n \n int dirX[4] = {-1, 0, 1, 0};\n int dirY[4] = {0, -1, 0, 1};\n \n for(int i=0; i<4; i++) {\n int nx = x + dirX[i];\n int ny = y + dirY[i];\n \n if(nx < 0 || nx > m-1 || ny < 0 || ny > n-1 || vis[nx][ny]) continue;\n \n grid[nx][ny] = 1 + dis;\n \n q.push({grid[nx][ny], nx, ny});\n \n vis[nx][ny] = 1;\n \n }\n }\n }\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n bfs(grid);\n vector<vector<int>> dis(m, vector<int>(n, INT_MIN));\n priority_queue<vector<int>> pq;\n pq.push({grid[0][0], 0, 0});\n dis[0][0] = grid[0][0];\n while(!pq.empty()) {\n auto temp = pq.top();\n pq.pop();\n \n int val = temp[0];\n int x = temp[1];\n int y = temp[2];\n \n int dirX[4] = {0, -1, 0, 1};\n int dirY[4] = {1, 0, -1, 0};\n \n for(int i=0; i<4; i++) {\n int nx = x + dirX[i];\n int ny = y + dirY[i];\n \n if(nx < 0 || nx > m-1 || ny < 0 || ny > n-1) continue;\n \n if(dis[nx][ny] < min(val, grid[nx][ny])) {\n dis[nx][ny] = min(val, grid[nx][ny]);\n pq.push({dis[nx][ny], nx, ny});\n }\n }\n }\n return dis[m-1][n-1];\n }\n};", "memory": "456628" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n void findDistance(vector<vector<int>>& grid, vector<vector<int>>& dis) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> vis(n + 1, vector<int>(m + 1, 0));\n queue<pair<int, int>> q;\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < m; ++j) {\n if(grid[i][j] == 1) {\n q.push({i, j});\n vis[i][j] = 1;\n dis[i][j] = 0;\n }\n }\n }\n\n while(!q.empty()) {\n int curx = q.front().first;\n int cury = q.front().second;\n q.pop();\n\n int dx[] = {-1, 1, 0, 0};\n int dy[] = {0, 0, -1, 1};\n for(int i = 0; i < 4; ++i) {\n int newx = dx[i] + curx, newy = dy[i] + cury;\n if(newx >= 0 and newy >= 0 and newx < n and newy < m and !vis[newx][newy]) {\n q.push({newx, newy});\n vis[newx][newy] = 1;\n dis[newx][newy] = dis[curx][cury] + 1;\n }\n }\n }\n }\nprivate:\n bool find(int x, int y, vector<vector<int>>& grid, vector<vector<int>>& dis, int mid, vector<vector<int>>& vis) {\n int n = grid.size(), m = grid[0].size();\n if(x == n - 1 and y == m - 1) {\n if(dis[x][y] >= mid) return true;\n else return false;\n }\n vis[x][y] = 1;\n\n int dx[] = {-1, 1, 0, 0};\n int dy[] = {0, 0, -1, 1};\n\n for(int i = 0; i < 4; ++i) {\n int newx = dx[i] + x, newy = dy[i] + y;\n if(newx >= 0 and newy >= 0 and newx < n and newy < m and !vis[newx][newy] and dis[newx][newy] >= mid) {\n if(find(newx, newy, grid, dis, mid, vis)) return true;\n }\n }\n return false;\n }\nprivate:\n bool predicate(vector<vector<int>>& grid, vector<vector<int>>& dis, int mid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> vis(n + 1, vector<int>(m + 1, 0));\n if(dis[0][0] >= mid) {\n return find(0, 0, grid, dis, mid, vis);\n } else return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> dis(n + 1, vector<int>(m + 1, INT_MAX));\n findDistance(grid, dis);\n\n int low = 0, high = 1e9;\n int maxSafeFactor = INT_MIN;\n while(low <= high) {\n int mid = low + (high - low) / 2;\n if(predicate(grid, dis, mid)) {\n maxSafeFactor = max(maxSafeFactor, mid);\n low = mid + 1;\n } else high = mid - 1;\n }\n return maxSafeFactor;\n }\n};", "memory": "461311" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n void findDistance(vector<vector<int>>& grid, vector<vector<int>>& dis) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> vis(n + 1, vector<int>(m + 1, 0));\n queue<pair<int, int>> q;\n for(int i = 0; i < n; ++i) {\n for(int j = 0; j < m; ++j) {\n if(grid[i][j] == 1) {\n q.push({i, j});\n vis[i][j] = 1;\n dis[i][j] = 0;\n }\n }\n }\n\n while(!q.empty()) {\n int curx = q.front().first;\n int cury = q.front().second;\n q.pop();\n\n int dx[] = {-1, 1, 0, 0};\n int dy[] = {0, 0, -1, 1};\n for(int i = 0; i < 4; ++i) {\n int newx = dx[i] + curx, newy = dy[i] + cury;\n if(newx >= 0 and newy >= 0 and newx < n and newy < m and !vis[newx][newy]) {\n q.push({newx, newy});\n vis[newx][newy] = 1;\n dis[newx][newy] = dis[curx][cury] + 1;\n }\n }\n }\n }\nprivate:\n bool find(int x, int y, vector<vector<int>>& grid, vector<vector<int>>& dis, int mid, vector<vector<int>>& vis) {\n int n = grid.size(), m = grid[0].size();\n if(x == n - 1 and y == m - 1) {\n if(dis[x][y] >= mid) return true;\n else return false;\n }\n vis[x][y] = 1;\n\n int dx[] = {-1, 1, 0, 0};\n int dy[] = {0, 0, -1, 1};\n\n for(int i = 0; i < 4; ++i) {\n int newx = dx[i] + x, newy = dy[i] + y;\n if(newx >= 0 and newy >= 0 and newx < n and newy < m and !vis[newx][newy] and dis[newx][newy] >= mid) {\n if(find(newx, newy, grid, dis, mid, vis)) return true;\n }\n }\n return false;\n }\nprivate:\n bool predicate(vector<vector<int>>& grid, vector<vector<int>>& dis, int mid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> vis(n + 1, vector<int>(m + 1, 0));\n if(dis[0][0] >= mid) {\n return find(0, 0, grid, dis, mid, vis);\n } else return false;\n }\npublic:\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> dis(n + 1, vector<int>(m + 1, INT_MAX));\n findDistance(grid, dis);\n\n int low = 0, high = 1e9;\n int maxSafeFactor = INT_MIN;\n while(low <= high) {\n int mid = low + (high - low) / 2;\n if(predicate(grid, dis, mid)) {\n maxSafeFactor = max(maxSafeFactor, mid);\n low = mid + 1;\n } else high = mid - 1;\n }\n return maxSafeFactor;\n }\n};", "memory": "461311" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n inline bool isValid(int i, int j, int m, int n) {\n return i >= 0 && i < m && j >= 0 && j < n;\n }\n void updateSafeFactor(int i, int j, vector<vector<int>>& grid, vector<vector<int>> &safeFactor) {\n int m = grid.size();\n int n = grid[0].size();\n \n safeFactor[i][j] = 0;\n queue<pair<int, pair<int, int>>> pointQueue;\n pointQueue.push({0, {i, j}});\n const vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n while (!pointQueue.empty()) {\n auto [curSafeFactor, point] = pointQueue.front();\n auto [x, y] = point;\n // cout << x << \" \" << y << \" \" << curSafeFactor << \"\\n\";\n pointQueue.pop();\n for (auto &[dx, dy] : dirs) {\n if (!isValid(x + dx, y + dy, m, n)) {\n continue;\n }\n if (grid[x+dx][y+dy] == 1) {\n continue;\n }\n if (curSafeFactor + 1 < safeFactor[x + dx][y + dy]) {\n safeFactor[x + dx][y + dy] = curSafeFactor + 1;\n pointQueue.push({curSafeFactor + 1, {x + dx, y + dy}});\n }\n\n }\n }\n\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n int lo = 0;\n int hi = m + n;\n vector<vector<int>> safeFactor(m, vector<int>(n, m+n));\n\n for (int i = 0; i < m; i ++) {\n for (int j = 0; j < n; j ++) {\n if (grid[i][j] == 1) {\n updateSafeFactor(i, j, grid, safeFactor);\n }\n\n }\n }\n\n priority_queue<pair<int, pair<int, int>>> pointQueue;\n pointQueue.push({safeFactor[0][0], {0, 0}});\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n visited[0][0] = true;\n while (!pointQueue.empty()) {\n auto [curSafeFactor, point] = pointQueue.top();\n auto &[x, y] = point;\n pointQueue.pop();\n if (x == m - 1 && y == n - 1) {\n return curSafeFactor;\n }\n const vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n for (auto &[dx, dy] : dirs) {\n if (!isValid(x + dx, y + dy, m, n) || grid[x+dx][y+dy] == 1) {\n continue;\n }\n if (!visited[x+dx][y+dy]) {\n pointQueue.push({min(safeFactor[x + dx][y + dy], curSafeFactor), {x + dx, y + dy}});\n visited[x + dx][y + dy] = true;\n }\n }\n }\n\n return 0;\n }\n};", "memory": "465993" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n inline bool isValid(int i, int j, int m, int n) {\n return i >= 0 && i < m && j >= 0 && j < n;\n }\n void updateSafeFactor(int i, int j, vector<vector<int>>& grid, vector<vector<int>> &safeFactor) {\n int m = grid.size();\n int n = grid[0].size();\n \n safeFactor[i][j] = 0;\n queue<pair<int, pair<int, int>>> pointQueue;\n pointQueue.push({0, {i, j}});\n const vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n while (!pointQueue.empty()) {\n auto [curSafeFactor, point] = pointQueue.front();\n auto [x, y] = point;\n // cout << x << \" \" << y << \" \" << curSafeFactor << \"\\n\";\n pointQueue.pop();\n for (auto &[dx, dy] : dirs) {\n if (!isValid(x + dx, y + dy, m, n)) {\n continue;\n }\n if (grid[x+dx][y+dy] == 1) {\n continue;\n }\n if (curSafeFactor + 1 < safeFactor[x + dx][y + dy]) {\n safeFactor[x + dx][y + dy] = curSafeFactor + 1;\n pointQueue.push({curSafeFactor + 1, {x + dx, y + dy}});\n }\n\n }\n }\n\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n int lo = 0;\n int hi = m + n;\n vector<vector<int>> safeFactor(m, vector<int>(n, m+n));\n\n for (int i = 0; i < m; i ++) {\n for (int j = 0; j < n; j ++) {\n if (grid[i][j] == 1) {\n updateSafeFactor(i, j, grid, safeFactor);\n }\n\n }\n }\n\n priority_queue<pair<int, pair<int, int>>> pointQueue;\n pointQueue.push({safeFactor[0][0], {0, 0}});\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n visited[0][0] = true;\n while (!pointQueue.empty()) {\n auto [curSafeFactor, point] = pointQueue.top();\n auto &[x, y] = point;\n pointQueue.pop();\n // cout << curSafeFactor << \"\\n\";\n if (x == m - 1 && y == n - 1) {\n return curSafeFactor;\n }\n const vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n for (auto &[dx, dy] : dirs) {\n if (!isValid(x + dx, y + dy, m, n)) {\n continue;\n }\n if (!visited[x+dx][y+dy]) {\n pointQueue.push({min(safeFactor[x + dx][y + dy], curSafeFactor), {x + dx, y + dy}});\n visited[x + dx][y + dy] = true;\n }\n }\n }\n\n return 0;\n }\n};", "memory": "470676" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n typedef pair<int,int> pii;\n typedef vector<vector<int>> vvi;\n typedef vector<int> vi;\n\n vi rows = {-1, 0, 1, 0};\n vi cols = {0, 1, 0, -1};\n\n bool pathPossible(vvi grid, int reqSafeness){\n\n int n = grid.size();\n\n if(grid[0][0] < reqSafeness)\n return false;\n\n queue<pii> q;\n q.push({0,0});\n\n grid[0][0] = -1;\n\n while(!q.empty()){\n\n int i = q.front().first; \n int j = q.front().second; \n q.pop();\n\n if(i == n-1 && j == n-1)\n return true;\n\n for(int nbr = 0; nbr<4; nbr++){\n\n int i_ = i + rows[nbr];\n int j_ = j + cols[nbr];\n\n // out of. bound or already visited cells -->\n if( (i_ >= n) || (i_ < 0) || (j_ >= n)\n || (j_ < 0) || (grid[i_][j_] == -1) ) \n continue;\n\n if(grid[i_][j_] >= reqSafeness){\n\n q.push({i_, j_});\n grid[i_][j_] = -1;\n \n }\n\n }\n \n }\n \n return false;\n }\n\n\n\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n int n = grid.size();\n\n vvi visited(n, vi(n, false));\n\n // prepopulate distances from cells to nearest theifs -->\n queue<pii> q;\n\n // push theifs in queue -->\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n\n if(grid[i][j] == 1){\n \n q.push({i,j});\n visited[i][j] = true;\n grid[i][j] = 0;\n \n }\n \n }\n }\n\n // apply multi source BFS -->\n int lvl = 1;\n while(!q.empty()){\n\n int queSize = q.size();\n while(queSize--){\n\n int i = q.front().first; \n int j = q.front().second; \n q.pop();\n\n for(int nbr = 0; nbr<4; nbr++){\n\n int i_ = i + rows[nbr];\n int j_ = j + cols[nbr];\n\n // out of. bound or already visited cells -->\n if( (i_ >= n) || (i_ < 0) || (j_ >= n)\n || (j_ < 0) || (visited[i_][j_]) ) \n continue;\n\n // fill manhatten distance and mark cells visited -->\n grid[i_][j_] = lvl;\n visited[i_][j_] = true;\n\n // push in queue -->\n q.push({i_, j_});\n \n }\n \n }\n lvl++;\n }\n\n // Apply binary search -->\n int left = 0;\n int right = 1e9;\n\n int maxSafeness = INT_MIN;\n\n while(left <= right){\n\n int mid = (left + right)/2;\n\n if(pathPossible(grid, mid)){\n \n maxSafeness = max(maxSafeness, mid);\n left = mid + 1;\n }\n else{\n right = mid - 1;\n }\n \n }\n\n return maxSafeness;\n \n }\n};", "memory": "470676" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void bfs(vector<vector<int>>&vis, vector<vector<int>>&grid, int val){\n queue<pair<int,int>> q;\n int n = grid.size();\n int m = grid[0].size();\n\n if(grid[0][0] < val){\n return;\n }\n q.push({0,0});\n int dx[]= {-1,0,1,0};\n int dy[]= {0,1,0,-1};\n vis[0][0]= 1;\n while(!q.empty()){\n pair<int ,int > tt = q.front();\n q.pop();\n\n for(int k = 0 ; k<4 ; k++){\n int xx = tt.first + dx[k];\n int yy = tt.second + dy[k];\n\n if(xx>=0 && yy>=0 && xx<n && yy<m && grid[xx][yy]>=val ){\n if(!vis[xx][yy]){\n vis[xx][yy] = 1;\n q.push({xx,yy});\n }\n }\n }\n\n }\n }\n\n bool check(int val, vector<vector<int>>&grid){\n\n vector<vector<int>>vis(grid.size() , vector<int>(grid[0].size() , 0));\n bfs(vis,grid, val);\n\n return vis[grid.size() -1][grid[0].size() -1];\n }\n int maximumSafenessFactor(vector<vector<int>>& grid) {\n\n\n vector<vector<int>>dis1(grid.size() , vector<int>(grid[0].size() , 1e9));\n\n queue<pair<int,int>>q;\n int dx[] = {-1, 0, 1 ,0};\n int dy[] = {0,1,0 ,-1};\n int n = grid.size();\n int m = grid[0].size();\n for(int i = 0 ; i<n ; i++){\n for(int j = 0 ; j<m ; j++){\n if(grid[i][j]){ dis1[i][j] = 0;\n q.push({i,j});\n }\n }\n }\n\n while(!q.empty()){\n pair<int, int> tt = q.front();\n q.pop();\n\n for(int k = 0 ; k<4 ; k++){\n int xx = tt.first + dx[k];\n int yy = tt.second + dy[k];\n\n if(xx>=0 && yy>=0 && xx<n && yy<m && dis1[xx][yy] > 1 + dis1[tt.first][tt.second]){\n q.push({xx, yy});\n dis1[xx][yy] = 1+ dis1[tt.first][tt.second];\n }\n }\n }\n\n int lo = 0 ;\n int hi = 1e9;\n int ans ;\n while(lo<=hi){\n int mid = lo + (hi-lo)/2;\n\n if(check(mid,dis1)){\n ans = mid;\n lo = mid+1;\n }else{\n hi = mid -1;\n }\n }\n return ans;\n\n }\n \n};", "memory": "475358" }
2,914
<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>n x n</code>, where <code>(r, c)</code> represents:</p> <ul> <li>A cell containing a thief if <code>grid[r][c] = 1</code></li> <li>An empty cell if <code>grid[r][c] = 0</code></li> </ul> <p>You are initially positioned at cell <code>(0, 0)</code>. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.</p> <p>The <strong>safeness factor</strong> of a path on the grid is defined as the <strong>minimum</strong> manhattan distance from any cell in the path to any thief in the grid.</p> <p>Return <em>the <strong>maximum safeness factor</strong> of all paths leading to cell </em><code>(n - 1, n - 1)</code><em>.</em></p> <p>An <strong>adjacent</strong> cell of cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> and <code>(r - 1, c)</code> if it exists.</p> <p>The <strong>Manhattan distance</strong> between two cells <code>(a, b)</code> and <code>(x, y)</code> is equal to <code>|a - x| + |b - y|</code>, where <code>|val|</code> denotes the absolute value of val.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example1.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,1]] <strong>Output:</strong> 0 <strong>Explanation:</strong> All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example2.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,1],[0,0,0],[0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/07/02/example3.png" style="width: 362px; height: 242px;" /> <pre> <strong>Input:</strong> grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length == n &lt;= 400</code></li> <li><code>grid[i].length == n</code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li>There is at least one thief in the <code>grid</code>.</li> </ul>
3
{ "code": "typedef vector<int> vi;\ntypedef vector<vi> vvi;\ntypedef vector<pair<int,int>> vp;\ntypedef pair<int, int> cell;\nclass Solution {\npublic:\n // conditions:\n // 1. move in adjacent directions\n // 2. safeness factor is minimum manhattan dist to a thief\n // mahattan distance: |a - x| + |b - y|\n int m_dist(int a, int b, int x, int y) {\n return abs(a - x) + abs(b - y);\n }\n \n vp neighbors(int i, int j, int n) {\n // prioritize down and to the right\n vp neighbors;\n if (i < n-1)\n neighbors.push_back({i+1, j});\n if (j < n-1)\n neighbors.push_back({i, j+1});\n if (i > 0)\n neighbors.push_back({i-1, j});\n if (j > 0)\n neighbors.push_back({i, j-1});\n \n return neighbors;\n }\n \n \n void bfs(vvi& grid, vvi& safeness) {\n int n = grid.size();\n queue<cell> q;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j]) {\n q.push({i, j});\n safeness[i][j] = 0;\n }\n }\n }\n // bfs\n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n for (auto& [nx, ny]: neighbors(x, y, n)) {\n int newDist = safeness[x][y] + 1;\n if (newDist < safeness[nx][ny]) {\n safeness[nx][ny] = newDist;\n q.push({nx, ny});\n }\n }\n }\n \n }\n \n int widestPath(vvi& safeness) {\n int n = safeness.size();\n auto cmp = [&safeness](cell const& p1, cell const& p2) {\n return safeness[p1.first][p1.second] < safeness[p2.first][p2.second];\n };\n // max heap for nodes based on their safeness\n priority_queue<cell, vector<cell>, decltype(cmp)> pq(cmp);\n \n // start with 0,0\n vvi visited(n, vi(n, 0));\n // max safeness can only go down from 0,0\n int maxSafe = safeness[0][0];\n \n pq.push({0,0});\n while (!pq.empty()) {\n cell cell = pq.top();\n pq.pop();\n int x = cell.first;\n int y = cell.second;\n // already visited - skip\n if (visited[x][y]) continue;\n visited[x][y] = 1;\n // see if this reduces our max safety\n maxSafe = min(maxSafe, safeness[x][y]);\n if (x == n-1 && y == n-1) {\n // reached target\n return maxSafe;\n }\n for (auto& neighbor : neighbors(cell.first, cell.second, n)) {\n pq.push({neighbor.first, neighbor.second});\n }\n }\n \n return maxSafe;\n }\n \n int maximumSafenessFactor(vector<vector<int>>& grid) {\n int n = grid.size();\n // calculate max safe path between (0,0) and (n-1, n-1)\n // calculate safeness factors for every cell\n vvi safeness(n, vi(n, INT_MAX - 1000));\n queue<int> q;\n // bfs from each thief, updating manhattan distances\n bfs(grid, safeness);\n \n return widestPath(safeness);\n }\n};", "memory": "475358" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return (c >= '0') && (c <= '9');\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n std::stack<char> st;\n int carry = 0;\n for (int i = s.size() - 2; i > 0; i -= 2) {\n int old_val = s[i] - '0';\n int new_val = 2 * old_val + carry;\n carry = new_val / 10;\n new_val = new_val % 10;\n st.push(new_val + '0');\n // if (is_digit(s[i])) {\n // }\n }\n out << \"[\";\n if (carry > 0) {\n out << carry << \",\";\n }\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n out << st.top() << \"]\\n\";\n }\n out.flush();\n exit(0); \n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n int len=0;\n ListNode* temp=head;\n stack<int>st,st2;\n while(temp!=NULL){\nst.push(temp->val);\ntemp=temp->next;\n++len;\n }\n temp=head;\n int carry=0;\n\n while(!st.empty()){\nint num=st.top()*2+carry;\nst.pop();\ncarry=num/10;\nst2.push(num%10);\n\n\n }\n if(carry!=0){st2.push(carry);}\n \n \n while(!st2.empty()){\ntemp->val=st2.top();\nst2.pop();\nif(st2.size()==1&&temp->next==NULL){temp->next=new ListNode(0);}\ntemp=temp->next;\n }\n return head; }\n};", "memory": "11035" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\ninline bool is_digit(char c) {\n return (c >= '0') && (c <= '9');\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n std::stack<char> st;\n int carry = 0;\n for (int i = s.size() - 2; i > 0; i -= 2) {\n int old_val = s[i] - '0';\n int new_val = 2 * old_val + carry;\n carry = new_val / 10;\n new_val = new_val % 10;\n st.push(new_val + '0');\n // if (is_digit(s[i])) {\n // }\n }\n out << \"[\";\n if (carry > 0) {\n out << carry << \",\";\n }\n while (st.size() > 1) {\n out << st.top() << \",\";\n st.pop();\n }\n out << st.top() << \"]\\n\";\n }\n out.flush();\n exit(0); \n return true;\n}();\n\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n int len=0;\n ListNode* temp=head;\n stack<int>st,st2;\n while(temp!=NULL){\nst.push(temp->val);\ntemp=temp->next;\n++len;\n }\n temp=head;\n int carry=0;\n\n while(!st.empty()){\nint num=st.top()*2+carry;\nst.pop();\ncarry=num/10;\nst2.push(num%10);\n\n\n }\n if(carry!=0){st2.push(carry);}\n \n \n while(!st2.empty()){\ntemp->val=st2.top();\nst2.pop();\nif(st2.size()==1&&temp->next==NULL){temp->next=new ListNode(0);}\ntemp=temp->next;\n }\n return head; }\n};", "memory": "11035" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n ListNode* prev = 0;\n ListNode* cur = head;\n while (cur) {\n int v = cur->val * 2;\n\n cur->val = v % 10;\n\n if (10 <= v) {\n if (prev) {\n ++prev->val;\n } else {\n ListNode* new_head = new ListNode(1, head);\n head = new_head;\n }\n }\n\n prev = cur;\n cur = cur->next;\n }\n return head;\n }\n};\n", "memory": "12705" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* head)\n {\n ListNode* curr = head ;\n ListNode* prev = NULL ;\n ListNode* forward = NULL ;\n\n while(curr!=NULL)\n {\n forward = curr->next ;\n curr->next = prev ;\n prev = curr ;\n curr = forward ;\n }\n return prev ;\n }\n ListNode* doubleIt(ListNode* head) {\n ListNode* temp = reverse(head) ;\n ListNode* temp1 = temp ;\n int carry = 0 ; \n while(temp1!=NULL)\n {\n int val = temp1->val*2 + carry ;\n if(val>=10)\n temp1->val = val%10 , carry = 1 ;\n else\n temp1->val = val , carry = 0 ;\n if(carry && temp1->next==NULL)\n {\n ListNode* x = new ListNode(carry) ;\n temp1->next = x ;\n break ;\n }\n temp1 = temp1->next ;\n }\n return reverse(temp) ;\n }\n};", "memory": "12705" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\n\nclass Solution {\npublic:\n // Helper function to reverse the linked list\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n while (curr != nullptr) {\n ListNode* nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n }\n return prev;\n }\n \n ListNode* doubleIt(ListNode* head) {\n if (head == nullptr) return nullptr;\n\n // Step 1: Reverse the list to process from the least significant digit\n head = reverseList(head);\n\n // Step 2: Double each node's value and handle carry\n ListNode* curr = head;\n int carry = 0;\n while (curr != nullptr) {\n int newValue = curr->val * 2 + carry;\n curr->val = newValue % 10;\n carry = newValue / 10;\n if (curr->next == nullptr && carry > 0) {\n curr->next = new ListNode(carry);\n break;\n }\n curr = curr->next;\n }\n\n // Step 3: Reverse the list back to the original order\n return reverseList(head);\n }\n};", "memory": "14375" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n head = reverseList(head);\n ListNode* curr = head;\n int carry = 0;\n while (curr != nullptr) {\n int val = curr->val * 2 + carry; \n carry = val / 10; \n curr->val = val % 10; \n if (curr->next == nullptr && carry > 0) {\n curr->next = new ListNode(carry);\n break;\n }\n curr = curr->next;\n }\n head = reverseList(head);\n return head;\n }\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n \n while (curr != nullptr) {\n ListNode* next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n \n return prev;\n }\n};\n", "memory": "14375" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* curr)\n {\n ListNode* prev=NULL;\n ListNode* forward=NULL;\n while(curr!=NULL)\n {\n forward=curr->next;\n curr->next=prev;\n prev=curr;\n curr=forward;\n }\n return prev;\n }\n ListNode* doubleIt(ListNode* head) {\n // reverse Linklist\n ListNode* prev=reverse(head);\n // prev id head of revrse linked list\n int carry=0;\n ListNode* head1=prev;\n ListNode* h=prev;\n ListNode* p=NULL;\n while(h!=NULL)\n {\n int val = h->val*2 +carry;\n h->val= val%10;\n carry = val/10;\n p=h;\n h=h->next;\n }\n while(carry!=0)\n {\n p->next= new ListNode(carry%10);\n carry=carry/10;\n p=p->next;\n }\n\n //reverse the linklist\n ListNode* ans=reverse(head1);\n return ans;\n }\n};", "memory": "16045" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* head)\n {\n ListNode* curr = head ;\n ListNode* prev = NULL ;\n ListNode* forward = NULL ;\n\n while(curr!=NULL)\n {\n forward = curr->next ;\n curr->next = prev ;\n prev = curr ;\n curr = forward ;\n }\n return prev ;\n }\n ListNode* doubleIt(ListNode* head) {\n ListNode* temp = reverse(head) ;\n ListNode* temp1 = temp ;\n int carry = 0 ; \n while(temp1!=NULL)\n {\n int val = temp1->val*2 + carry ;\n if(val>=10)\n temp1->val = val%10 , carry = 1 ;\n else\n temp1->val = val , carry = 0 ;\n if(carry && temp1->next==NULL)\n {\n ListNode* x = new ListNode(carry) ;\n temp1->next = x ;\n break ;\n }\n temp1 = temp1->next ;\n }\n return reverse(temp) ;\n }\n};", "memory": "16045" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode* rev(ListNode* head) {\n ListNode* prev = 0;\n ListNode* cur = head;\n while (cur) {\n ListNode* next = cur->next;\n cur->next = prev;\n prev = cur;\n cur = next;\n }\n return prev;\n }\npublic:\n ListNode* doubleIt(ListNode* head) {\n head = rev(head);\n int r = 0;\n\n ListNode* cur = head;\n ListNode* prev = 0;\n while (cur) {\n int v = (cur->val << 1) + r;\n cur->val = v % 10;\n r = v / 10;\n prev = cur;\n cur = cur->next;\n }\n if (r) {\n ListNode* tail = new ListNode(r);\n prev->next = tail;\n }\n return rev(head);\n }\n};\n", "memory": "17715" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "static const int fastIO = [] {\n\tstd::ios_base::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);\n\treturn 0;\n}();\n\nclass Solution {\nprivate:\n\tint solve(ListNode* node) {\n\t\tif (!node) return 0;\n\t\tnode->val = node->val * 2 + solve(node->next);\n\t\tint carry = node->val / 10;\n\t\tnode->val %= 10;\n\t\treturn carry;\n\t}\n\npublic:\n\tListNode* doubleIt(ListNode* head) {\n\t\tint carry = solve(head);\n\t\tif (carry > 0)\n\t\t\thead = new ListNode(carry, head);\n\t\treturn head;\n\t}\n};", "memory": "27735" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n int rec(ListNode*& root,int&& count){\n if(root == NULL){\n return 0;\n }\n int tmp = rec(root->next,count+1) + root->val*2 ;\n root->val = tmp%10;\n tmp /=10;\n if(count == 0 && tmp != 0){\n ListNode* tmpNode = root;\n root = new ListNode(tmp,tmpNode);\n }\n \n\n return tmp;\n\n }\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n // rec(head,0);\n // return head;\n ListNode* tmp1 = head;\n ListNode* tmp2 = head;\n if(tmp1->next == NULL && tmp1->val == 0){\n return head;\n }\n int count = 0;\n char buff[10001];\n while(tmp1 != NULL){\n buff[count++] = tmp1->val;\n tmp1 = tmp1->next;\n }\n int size = count;\n int val = 0;\n while(count){\n val = ((buff[--count])*2 )+ val;\n buff[count] = val%10;\n cout << (int)buff[count]<<endl;\n val /= 10; \n }\n if(val > 0){\n head = new ListNode;\n head->next= tmp2;\n head->val = val;\n }\n int i = 0;\n while(tmp2 != NULL){\n tmp2->val = buff[i++];\n tmp2 = tmp2->next;\n }\n return head;\n }\n};", "memory": "29405" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n int rec(ListNode*& root,int&& count){\n if(root == NULL){\n return 0;\n }\n int tmp = rec(root->next,count+1) + root->val*2 ;\n root->val = tmp%10;\n tmp /=10;\n if(count == 0 && tmp != 0){\n ListNode* tmpNode = root;\n root = new ListNode(tmp,tmpNode);\n }\n \n\n return tmp;\n\n }\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n // rec(head,0);\n // return head;\n ListNode* tmp1 = head;\n ListNode* tmp2 = head;\n if(tmp1->next == NULL && tmp1->val == 0){\n return head;\n }\n int count = 0;\n char buff[10000];\n while(tmp1 != NULL){\n buff[count++] = tmp1->val;\n tmp1 = tmp1->next;\n }\n int size = count;\n int val = 0;\n while(count){\n val = ((buff[--count])*2 )+ val;\n buff[count] = val%10;\n cout << (int)buff[count]<<endl;\n val /= 10; \n }\n if(val > 0){\n head = new ListNode;\n head->next= tmp2;\n head->val = val;\n }\n int i = 0;\n while(tmp2 != NULL){\n tmp2->val = buff[i++];\n tmp2 = tmp2->next;\n }\n return head;\n }\n};", "memory": "29405" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n int rec(ListNode*& root,int&& count){\n if(root == NULL){\n return 0;\n }\n int tmp = rec(root->next,count+1) + root->val*2 ;\n root->val = tmp%10;\n tmp /=10;\n if(count == 0 && tmp != 0){\n ListNode* tmpNode = root;\n root = new ListNode(tmp,tmpNode);\n }\n \n\n return tmp;\n\n }\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n rec(head,0);\n return head;\n // ListNode* tmp1 = head;\n // ListNode* tmp2 = head;\n\n // int sum = 0;\n // while(tmp1 != NULL){\n // sum *= 10;\n // sum +=tmp1->val;\n // tmp1 = tmp1->next;\n // }\n // std::cout << sum << std::endl;\n // int size = 0;\n // sum *= 2;\n // int x = sum;\n // while(x != 0){\n // x /=10;\n // size++;\n // }\n // char rotsum[size] = {};\n // int i = 0\n // while(sum != 0){\n \n // r= sum%10;\n // sum /=10; \n // }\n // std::cout << rotsum << std::endl;\n // while(tmp2 != NULL){\n // tmp2->val = rotsum%10;\n // rotsum /=10;\n // if(tmp2->next == NULL && rotsum != 0){\n // tmp2->next = new ListNode;\n // tmp2->next->next = NULL;\n\n // }\n // tmp2 = tmp2->next;\n // }\n // return head;\n }\n};", "memory": "31075" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n int i = 0;\n string s = \"\";\n ListNode *temp = head;\n while (temp != NULL)\n {\n s += to_string(temp -> val);\n temp = temp -> next;\n } \n int c = 0;\n for (i = s.size()-1; i>=0; i--)\n {\n int t = (s[i]-'0')*2;\n s[i] = (t % 10 + c)+'0';\n t/=10;\n c = t;\n }\n if (c != 0) s.insert(0,to_string(c));\n temp = head;\n i = 0;\n while (temp -> next != NULL)\n {\n temp -> val = s[i++]-'0';\n temp = temp -> next; \n }\n temp -> val = s[i++]-'0';\n if (i != s.size())\n {\n ListNode *nn = (ListNode*)malloc(sizeof(ListNode));\n nn -> next = NULL;\n nn -> val = s[i]-'0';\n temp -> next = nn;\n }\n\n return head;\n }\n};", "memory": "32745" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n int i = 0;\n string s = \"\";\n ListNode *temp = head;\n while (temp != NULL)\n {\n s += to_string(temp -> val);\n temp = temp -> next;\n }\n \n int c = 0;\n for (i = s.size()-1; i>=0; i--)\n {\n int t = (s[i]-'0')*2;\n cout << t << ' ' << c <<'\\n';\n s[i] = (t % 10 + c)+'0';\n t/=10;\n c = t;\n }\n if (c != 0) s.insert(0,to_string(c));\n cout << s;\n temp = head;\n i = 0;\n while (temp -> next != NULL)\n {\n temp -> val = s[i++]-'0';\n temp = temp -> next; \n }\n temp -> val = s[i++]-'0';\n if (i != s.size())\n {\n ListNode *nn = (ListNode*)malloc(sizeof(ListNode));\n nn -> next = NULL;\n nn -> val = s[i]-'0';\n temp -> next = nn;\n }\n\n return head;\n }\n};", "memory": "34415" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n int i = 0;\n string s = \"\";\n ListNode *temp = head;\n while (temp != NULL)\n {\n s += to_string(temp -> val);\n temp = temp -> next;\n } \n int c = 0;\n for (i = s.size()-1; i>=0; i--)\n {\n int t = (s[i]-'0')*2;\n s[i] = (t % 10 + c)+'0';\n t/=10;\n c = t;\n }\n if (c != 0) s.insert(0,to_string(c));\n temp = head;\n i = 0;\n while (temp -> next != NULL)\n {\n temp -> val = s[i++]-'0';\n temp = temp -> next; \n }\n temp -> val = s[i++]-'0';\n if (i != s.size())\n {\n ListNode *nn = (ListNode*)malloc(sizeof(ListNode));\n nn -> next = NULL;\n nn -> val = s[i]-'0';\n temp -> next = nn;\n }\n\n return head;\n }\n};", "memory": "34415" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void helper(ListNode* h1,ListNode* h2,ListNode* &nh){\n if(h2->next == NULL){\n nh = h2;\n h2->next = h1;\n return;\n }\n helper(h1->next,h2->next,nh);\n h2->next = h1;\n }\n ListNode* reverseList(ListNode* head) {\n ListNode* nh = NULL;\n if(head == NULL || head->next == NULL) return head;\n ListNode* h1 = head;\n ListNode* h2 = head->next;\n helper(h1,h2,nh);\n head->next = NULL;\n return nh;\n \n }\n ListNode* doubleIt(ListNode* head) {\n string ans = \"\";\n ListNode* temp = head;\n string str = \"\";\n while(temp){\n ans+=to_string(temp->val);\n temp = temp->next;\n }\n cout<<ans;\n temp = head;\n int i = ans.length() - 1;\n while(temp){\n temp->val = (ans[i] - '0');\n i--;\n temp = temp->next;\n }\n temp = head;\n int c = 0;\n ListNode* ptr;\n while(temp){\n int x = temp->val;\n x*=2;\n temp->val = x%10 + c;\n c = x/10;\n if(temp && temp->next == NULL) ptr = temp;\n temp = temp->next;\n }\n\n if(c != 0){\n ListNode* q = new ListNode(c);\n ptr->next = q;\n }\n\n return reverseList(head);\n \n\n }\n};\n\n // long long x = 2*stol(ans);\n // str = to_string(x);\n // cout<<endl<<str;\n // ListNode* h = new ListNode(-1);\n // temp = h;\n // ListNode* curr;\n // for(int i = 0;i<str.length();i++){\n // int val = str[i] - '0';\n // ListNode* t = new ListNode(val);\n // temp->next = t;\n // temp = t;\n // }\n\n // return h->next;", "memory": "36085" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void helper(ListNode* h1,ListNode* h2,ListNode* &nh){\n if(h2->next == NULL){\n nh = h2;\n h2->next = h1;\n return;\n }\n helper(h1->next,h2->next,nh);\n h2->next = h1;\n }\n ListNode* reverseList(ListNode* head) {\n ListNode* nh = NULL;\n if(head == NULL || head->next == NULL) return head;\n ListNode* h1 = head;\n ListNode* h2 = head->next;\n helper(h1,h2,nh);\n head->next = NULL;\n return nh;\n \n }\n ListNode* doubleIt(ListNode* head) {\n string ans = \"\";\n ListNode* temp = head;\n while(temp){\n ans+=to_string(temp->val);\n temp = temp->next;\n }\n temp = head;\n int i = ans.length() - 1;\n while(temp){\n temp->val = (ans[i] - '0');\n i--;\n temp = temp->next;\n }\n temp = head;\n int c = 0;\n ListNode* ptr;\n while(temp){\n int x = temp->val;\n x*=2;\n temp->val = x%10 + c;\n c = x/10;\n if(temp && temp->next == NULL) ptr = temp;\n temp = temp->next;\n }\n\n if(c != 0){\n ListNode* q = new ListNode(c);\n ptr->next = q;\n }\n\n return reverseList(head);\n \n\n }\n};\n\n // long long x = 2*stol(ans);\n // str = to_string(x);\n // cout<<endl<<str;\n // ListNode* h = new ListNode(-1);\n // temp = h;\n // ListNode* curr;\n // for(int i = 0;i<str.length();i++){\n // int val = str[i] - '0';\n // ListNode* t = new ListNode(val);\n // temp->next = t;\n // temp = t;\n // }\n\n // return h->next;", "memory": "36085" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n \n ListNode *ptr = head;\n bool car = false;\n int count = 0 , i = 0;\n\n while(ptr != NULL) {\n count++;\n ptr = ptr->next;\n }\n ptr = head;\n vector<int> carry(count);\n while(ptr != NULL) {\n if(ptr != head) {\n carry[i++] = ((2 * ptr->val) / 10);\n }\n count++;\n ptr = ptr->next;\n }\n ptr = head;\n if(((2 * ptr->val) + carry[0]) / 10 >= 1) {\n ListNode *newn = (ListNode *)malloc(sizeof(struct ListNode));\n newn->val = ((2 * ptr->val) + carry[0]) / 10;\n newn->next = ptr;\n head = newn;\n car = true;\n }\n if(car) {\n ptr = head;\n ptr = ptr->next;\n }\n else {\n ptr = head;\n }\n i = 0;\n while(ptr != NULL) {\n ptr->val = ((ptr->val * 2) + carry[i]) % 10;\n i++;\n ptr = ptr->next;\n }\n\n return head;\n }\n};", "memory": "37755" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n string s;\n ListNode * cur = head;\n while(cur!=nullptr){\n s += to_string(cur->val);\n cur = cur->next;\n }\n\n string stmp;\n int carry = 0;\n for(int i=s.size()-1;i>=0;i--){\n int t = (s[i]-'0')*2;\n int tmp = carry + t;\n carry = tmp/10;\n stmp += to_string(tmp%10);\n }\n\n if(carry)\n stmp += to_string(carry);\n s = stmp;\n reverse(s.begin(),s.end());\n cout<<s<<endl;\n\n int cnt = 0;\n cur = head;\n auto pre = head;\n while(cur!=nullptr){\n cur->val = (int)(s[cnt] - '0');\n cnt++;\n pre = cur;\n cur = cur->next;\n }\n //cout<<cnt<<endl;\n cur = pre;\n while(cnt<s.size()){\n cur->next = new ListNode(s[cnt]-'0');\n cout<<s[cnt]-'0'<<endl;\n cur = cur->next;\n cnt++;\n }\n return head;\n }\n};", "memory": "39425" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n ListNode* reverse (ListNode* head){\n ListNode* prev=NULL;\n ListNode* curr=head;\n\n while(curr!=NULL){\n ListNode* nextNode=curr->next;\n curr->next=prev;\n prev=curr;\n curr=nextNode;\n }\n\n return prev;\n }\n\n\n ListNode* doubleIt(ListNode* head) {\n stack<int> st;\n int carry=0;\n\n ListNode* temp=head;\n while (temp != nullptr) {\n st.push(temp->val);\n temp = temp->next;\n }\n\n // Re-initialize temp to head to start modifying nodes\n temp = head;\n\n while(temp->next!=0){\n int totalSum=2*st.top() + carry;\n st.pop();\n int digit=totalSum%10;\n carry=totalSum/10;\n \n temp->val=digit;\n temp=temp->next;\n }\n\n //processing last node\n int totalSum=2*st.top() + carry;\n int digit=totalSum%10;\n carry=totalSum/10;\n \n temp->val=digit;\n\n if(carry!=0){\n ListNode *newNode=new ListNode(carry);\n temp->next=newNode;\n }\n head=reverse(head);\n return head;\n }\n};\n\n// class Solution {\n// private:\n// ListNode* solve(ListNode* &temp,string& s2) {\n// ListNode* curr = temp;\n// while(curr!=nullptr) {\n// curr->val = s2[0] - '0';\n// s2.erase(0,1);\n// curr = curr->next;\n// }\n// if(s2.size()!=0) {\n// curr = temp;\n// while(curr->next!=nullptr) {\n// curr = curr->next;\n// }\n// curr->next = new ListNode(s2[0] - '0');\n// }\n// return temp;\n// }\n// public:\n// ListNode* doubleIt(ListNode* head) {\n// if(head == nullptr) {\n// return head;\n// }\n// string s1;\n// ListNode* temp = head;\n// while(temp!=nullptr) {\n// s1 += to_string(temp->val);\n// temp = temp->next;\n// }\n// int number = stoi(s1)*2;\n// string s2 = to_string(number);\n// temp = head;\n// return solve(temp,s2);\n// }\n// };", "memory": "41095" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* current = head;\n ListNode* next = nullptr;\n\n while (current != nullptr) {\n next = current->next; \n current->next = prev; \n prev = current; \n current = next; \n }\n\n return prev; \n }\n\n ListNode* doubleIt(ListNode* head) {\n stack<int> sumStack;\n int carry = 0;\n\n ListNode* temp = head;\n\n while (temp != NULL) {\n sumStack.push(temp->val);\n temp = temp->next;\n }\n\n temp = head;\n \n while (!sumStack.empty()) {\n int Sum = (2 * sumStack.top()) + carry;\n sumStack.pop();\n carry = Sum / 10;\n temp->val = Sum % 10; \n temp = temp->next;\n }\n\n\n ListNode* newHead = reverseList(head);\n\n if (carry == 1) {\n ListNode* newNode = new ListNode(1);\n newNode->next = newHead;\n newHead = newNode;\n }\n\n return newHead;\n }\n};\n", "memory": "42765" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n std::deque<int> dq{};\n ListNode* tmp = head;\n while (tmp) {\n dq.push_back(tmp->val);\n tmp = tmp->next;\n }\n int point = 0;\n for (auto it = dq.end() - 1; it != dq.begin(); --it) {\n *it = 2 * *it + point;\n point = *it / 10;\n *it = *it % 10;\n }\n *dq.begin() = 2 * *dq.begin() + point;\n point = *dq.begin() / 10;\n *dq.begin() = *dq.begin() % 10;\n\n ListNode* dummy = new ListNode(1);\n dummy->next = head;\n for (const auto& it : dq) {\n head->val = it;\n head = head->next;\n }\n return point == 1 ? dummy : dummy->next;\n }\n};", "memory": "44435" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n std::deque<int> dq{};\n ListNode* tmp = head;\n while (tmp) {\n dq.push_back(tmp->val);\n tmp = tmp->next;\n }\n int point = 0;\n for (auto it = dq.end() - 1; it != dq.begin(); --it) {\n *it = 2 * *it + point;\n point = *it / 10;\n *it = *it % 10;\n }\n *dq.begin() = 2 * *dq.begin() + point;\n point = *dq.begin() / 10;\n *dq.begin() = *dq.begin() % 10;\n\n ListNode* dummy = new ListNode(1);\n dummy->next = head;\n for (const auto& it : dq) {\n head->val = it;\n head = head->next;\n }\n return point == 1 ? dummy : dummy->next;\n }\n};", "memory": "46105" }
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [1,8,9] <strong>Output:</strong> [3,7,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" /> <pre> <strong>Input:</strong> head = [9,9,9] <strong>Output:</strong> [1,9,9,8] <strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li> <li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li> <li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n // string s;\n // ListNode* temp=head;\n // while(temp!=NULL)\n // {\n // s+=char(temp->val+'0');\n // temp=temp->next;\n // }\n // long long a;\n // a=stoll(s);\n // a=a*2;\n // s=to_string(a);\n // temp = head;\n // for (int i = 0; i < s.length(); i++) \n // {\n // temp->val = s[i] - '0';\n // if (temp->next == NULL && i < s.length() - 1)\n // {\n // temp->next = new ListNode(0);\n // }\n // temp = temp->next;\n // }\n \n // return head;\n stack<int> s;\n ListNode* temp=head;\n while(temp!=NULL)\n {\n s.push(temp->val);\n temp=temp->next;\n }\n string a;\n int v=0;\n while(!s.empty())\n {\n int x=s.top();\n s.pop();\n if(x>4)\n {\n x=x*2;\n x=x-10+v;\n a+=char(x+'0');\n v=1;\n }\n else\n {\n x=x*2+v;\n a+=char(x+'0');\n v=0;\n }\n }\n string f;\n if(head->val>4)\n {\n f+='1';\n }\n reverse(a.begin(),a.end());\n f+=a;\n cout<<f;\n ListNode* newh;\n temp=head;\n for(int i=0;i<f.length();i++)\n {\n temp->val=f[i]-'0';\n if (temp->next == NULL && i < f.length() - 1) \n {\n temp->next = new ListNode(0); \n }\n temp = temp->next;\n }\n \n return head;\n }\n};", "memory": "47775" }