{"id":1129,"date":"2019-10-06T18:33:22","date_gmt":"2019-10-06T22:33:22","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=1129"},"modified":"2021-10-12T19:35:31","modified_gmt":"2021-10-12T23:35:31","slug":"largest-product-ii","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=1129","title":{"rendered":"Largest Product II"},"content":{"rendered":"\r\n<p>You are given a 2 dimensional array. Compute products of length n consecutive digits along the rows and along the columns. Find the largest product in all rows and the largest product in all columns.<\/p>\r\n<p>For the 2 dimensional array below, where n = 2, the result is:<\/p>\r\n<p>max row is: 81, starting index 3, 5<br \/>max col is: 72, starting index 2, 6<\/p>\r\n\r\n\r\n\r\n<pre> [1, 2, 4, 6, 1, 7, 5] <br \/> [1, 2, 4, 5, 9, 2, 7]<br \/> [9, 4, 0, 1, 7, 4, 8]<br \/> [1, 9, 5, 6, 5, 9, 9]<br \/> [7, 5, 9, 7, 7, 4, 6] <\/pre>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let grid = [\r\n  [1, 2, 4, 6, 1, 7, 5],\r\n  [1, 2, 4, 5, 9, 2, 7],\r\n  [9, 4, 0, 1, 7, 4, 8],\r\n  [1, 9, 5, 6, 5, 9, 9],\r\n  [7, 5, 9, 7, 7, 4, 6]\r\n]\r\n\r\nlet numCount = 3\r\n\r\nfunction getMaxByRow(grid, row, col, numCount) {\r\n  let max = 0\r\n  let r = 0\r\n  let c = 0\r\n  let product = grid[row][col]\r\n\r\n  for (var j = 1; j &lt; numCount; j++) {\r\n    product *= grid[row][col + j]\r\n  }\r\n\r\n  if (product &gt; max) {\r\n    max = product\r\n    r = row\r\n    c = col\r\n  }\r\n\r\n  return { max, r, c }\r\n}\r\n\r\nfunction getMaxByCol(grid, row, col, numCount) {\r\n  let max = 0\r\n  let r = 0\r\n  let c = 0\r\n  let product = grid[row][col]\r\n\r\n  for (let j = 1; j &lt; numCount; j += 1) {\r\n    product *= grid[row + j][col]\r\n  }\r\n\r\n  if (product &gt; max) {\r\n    max = product\r\n    r = row\r\n    c = col\r\n  }\r\n\r\n  return { max, r, c }\r\n}\r\n\r\nfunction getMax(grid, numCount) {\r\n  let maxRow = 0\r\n  let maxRowRow = 0\r\n  let maxRowCol = 0\r\n  let maxCol = 0\r\n  let maxColRow = 0\r\n  let maxColCol = 0\r\n\r\n  let numRows = grid.length\r\n  let numCols = grid[0].length\r\n\r\n  let rowsToIterate = (numRows + 1 - numCount)\r\n  let colsToIterate = (numCols + 1 - numCount)\r\n\r\n  for (let row = 0; row &lt; numRows; row++) {\r\n    for (let col = 0; col &lt; numCols; col++) {\r\n\r\n      if (row &lt; rowsToIterate) {\r\n        let { max, r, c } = getMaxByCol(grid, row, col, numCount)\r\n\r\n        if (max &gt; maxCol) {\r\n          maxCol = max\r\n          maxColRow = r\r\n          maxColCol = c\r\n        }\r\n      }\r\n\r\n      if (col &lt; colsToIterate) {\r\n        let { max, r, c } = getMaxByRow(grid, row, col, numCount)\r\n\r\n        if (max &gt; maxRow) {\r\n          maxRow = max\r\n          maxRowRow = r\r\n          maxRowCol = c\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  console.log(<code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">max row is: ${maxRow} ${maxRowRow}, ${maxRowCol}<\/code>)\r\n  console.log(<code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">max col is: ${maxCol} ${maxColRow}, ${maxColCol}<\/code>)\r\n}\r\n\r\ngetMax(grid, numCount)\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2 dimensional array. Compute products of length n consecutive digits along the rows and along the columns. Find the largest product in all rows and the largest product in all columns. For the 2 dimensional array below, where n = 2, the result is: max row is: 81, starting index 3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1129"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1129"}],"version-history":[{"count":9,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1129\/revisions"}],"predecessor-version":[{"id":1326,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1129\/revisions\/1326"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}