{"id":11873,"date":"2025-01-26T22:19:09","date_gmt":"2025-01-26T22:19:09","guid":{"rendered":"https:\/\/www.softwareeverydayblog.com\/?p=11873"},"modified":"2025-01-26T23:35:01","modified_gmt":"2025-01-26T23:35:01","slug":"programming-problem-max-area-of-island","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=11873","title":{"rendered":"[Programming Problem] Max Area of Island"},"content":{"rendered":"<p>[<a href=\"https:\/\/leetcode.com\/problems\/max-area-of-island\/description\/\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<p>Algorithm:<\/p>\n<ul>\n<li>Loop through universe. Looks for any island element `1` and run recursive algo on it.<\/li>\n<li>Recurse: Basically, move top, bottom, left, right. Make sure you change the island element to `currIslandId` so that you don&#8217;t end up in an infinite loop. Return 1 + recurse(top) + recurse(bottom) + recurse(left) + recurse(right).<\/li>\n<li>Return the max area you&#8217;ve seen up-to now.<\/li>\n<li>At worst, you will loop through the grid twice (imagine the entire grid being 1&#8217;s). Time complexity is O(MN)<\/li>\n<\/ul>\n<pre lang=\"javascript\">\r\n\/**\r\n * @param {number[][]} grid\r\n * @return {number}\r\n *\/\r\nvar maxAreaOfIsland = function(grid) {\r\n    let currIslandId = 2;\r\n    let currMaxArea = 0;\r\n\r\n    for (let i = 0 ; i < grid.length ; i++ ) {\r\n        for ( let j = 0 ; j < grid[0].length ; j++ ) {\r\n            if (grid[i][j] === 1) {\r\n                const tmpMaxArea = maxAreaRecursive(grid, currIslandId, i, j);\r\n                currMaxArea = Math.max(currMaxArea, tmpMaxArea)\r\n                currIslandId++;\r\n            }\r\n        }\r\n    }\r\n\r\n    return currMaxArea\r\n};\r\n\r\nvar maxAreaRecursive = function(grid, currIslandId, i, j) {\r\n    if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length)\r\n        return 0;\r\n\r\n    if (grid[i][j] === 0 || grid[i][j] === currIslandId) return 0;\r\n\r\n    grid[i][j] = currIslandId;\r\n\r\n    return 1 + \r\n        maxAreaRecursive(grid, currIslandId, i+1, j) + \r\n        maxAreaRecursive(grid, currIslandId, i-1, j) + \r\n        maxAreaRecursive(grid, currIslandId, i, j+1) + \r\n        maxAreaRecursive(grid, currIslandId, i, j-1);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Problem Link] Algorithm: Loop through universe. Looks for any island element `1` and run recursive algo on it. Recurse: Basically, move top, bottom, left, right. Make sure you change the island element to `currIslandId` so that you don&#8217;t end up in an infinite loop. Return 1 + recurse(top) + recurse(bottom) + recurse(left) + recurse(right). Return [&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-11873","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11873","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=11873"}],"version-history":[{"count":3,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11873\/revisions"}],"predecessor-version":[{"id":11877,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/11873\/revisions\/11877"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}