{"id":11324,"date":"2021-06-05T21:09:39","date_gmt":"2021-06-05T21:09:39","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11324"},"modified":"2021-06-05T21:09:45","modified_gmt":"2021-06-05T21:09:45","slug":"programming-problem-minimum-path-sum-2","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11324","title":{"rendered":"[Programming Problem] Minimum Path Sum"},"content":{"rendered":"<p>Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.<\/p>\n<p>Note: You can only move either down or right at any point in time.<\/p>\n<p>Example 1:<br \/>\nInput: grid = [[1,3,1],[1,5,1],[4,2,1]]<br \/>\nOutput: 7<br \/>\nExplanation: Because the path 1 \u2192 3 \u2192 1 \u2192 1 \u2192 1 minimizes the sum.<\/p>\n<p>Example 2:<br \/>\nInput: grid = [[1,2,3],[4,5,6]]<br \/>\nOutput: 12<\/p>\n<p>[<a href=\"https:\/\/leetcode.com\/problems\/minimum-path-sum\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<p>Memoize by simply storing the minimum path to a cell via the top or left cell.<\/p>\n<p>Input:<br \/>\n[[1, 3, 1],<br \/>\n [1, 5, 1],<br \/>\n [4, 2, 1]]<\/p>\n<p>Memoized:<br \/>\n[[ 1, 4, 5 ],<br \/>\n [ 2, 7, 6 ],<br \/>\n [ 6, 8, 7 ]]<\/p>\n<p>Solution will be the bottom right cell in the memoized array.<\/p>\n<pre lang=\"javascript\">\r\n\/**\r\n * @param {number[][]} grid\r\n * @return {number}\r\n *\/\r\nvar minPathSum = function(grid) {\r\n    let ret = new Array(grid.length).fill(0).map((x) => new Array(grid[0].length).fill(0));\r\n    for ( let i = 0 ; i < grid.length ; i++ ) {\r\n        for ( let j = 0 ; j < grid[0].length ; j++ ) {\r\n            if (i===0 &#038;&#038; j===0) ret[i][j] = grid[i][j];\r\n            else if (i===0) ret[i][j] = grid[i][j] + ret[i][j-1];\r\n            else if (j===0) ret[i][j] = grid[i][j] + ret[i-1][j];\r\n            else ret[i][j] = grid[i][j] + Math.min(ret[i-1][j], ret[i][j-1]);\r\n        }\r\n    }\r\n    return ret[grid.length-1][grid[0].length-1];\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Example 1: Input: grid = [[1,3,1],[1,5,1],[4,2,1]] Output: 7 Explanation: Because the path [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11324","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11324"}],"version-history":[{"count":2,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11324\/revisions"}],"predecessor-version":[{"id":11326,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11324\/revisions\/11326"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}