Title: #1368 - Minimum Cost to Make at Least One Valid Path in a Grid - LeetCode JavaScript Solutions
Open Graph Title: #1368 - Minimum Cost to Make at Least One Valid Path in a Grid - LeetCode JavaScript Solutions
X Title: #1368 - Minimum Cost to Make at Least One Valid Path in a Grid - LeetCode JavaScript Solutions
Description: Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign...
Open Graph Description: Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign...
X Description: Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/minimum-cost-to-make-at-least-one-valid-path-in-a-grid
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/minimum-cost-to-make-at-least-one-valid-path-in-a-grid","name":"#1368 - Minimum Cost to Make at Least One Valid Path in a Grid - LeetCode JavaScript Solutions","description":"Given an m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar minCost = function(grid) {\n const queue = [[0, 0]];\n const directions = [[0, 1, 1], [0, -1, 2], [1, 0, 3], [-1, 0, 4]];\n const bfs = grid.map(r => r.map(_ => Infinity));\n bfs[0][0] = 0;\n\n while (queue.length > 0) {\n const [x, y] = queue.shift();\n for (const [dx, dy, value] of directions) {\n const [cX, cY] = [x + dx, y + dy];\n if (grid[cX]?.[cY]) {\n const updatedValue = bfs[x][y] + (grid[x][y] !== value);\n if (updatedValue < bfs[cX][cY]) {\n bfs[cX][cY] = updatedValue;\n queue.push([cX, cY]);\n }\n }\n }\n }\n\n return bfs.at(-1).at(-1);\n};","keywords":"LeetCode 1368, #1368 - Minimum Cost to Make at Least One Valid Path in a Grid, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/minimum-cost-to-make-at-least-one-valid-path-in-a-grid |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js |
| Array | https://leetcodejavascript.com/tags/array |
| Matrix | https://leetcodejavascript.com/tags/matrix |
| Breadth-First Search | https://leetcodejavascript.com/tags/breadth-first-search |
| Heap Priority Queue | https://leetcodejavascript.com/tags/heap-priority-queue |
| Graph | https://leetcodejavascript.com/tags/graph |
| Shortest Path | https://leetcodejavascript.com/tags/shortest-path |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow