{"ScriptPreparationCode":null,"TestCases":[{"Name":"num1","Code":"/* output */\r\nconst L = true;\r\nconst W = false;\r\nconst world = [\r\n [L, W, L, W, L, L, W],\r\n [L, L, L, W, L, W, W],\r\n [W, W, W, W, L, L, W],\r\n [L, W, W, W, W, W, W],\r\n [L, W, W, W, L, W, W],\r\n [L, L, W, L, L, W, W],\r\n [L, L, L, L, W, W, W],\r\n [W, W, W, W, W, W, W],\r\n [W, W, W, W, W, W, L],\r\n [W, W, W, W, W, L, L],\r\n [W, W, W, W, L, L, W],\r\n [W, W, W, L, L, W, W],\r\n [W, W, L, L, W, W, W],\r\n [W, L, L, W, W, W, W],\r\n [L, L, W, W, W, W, W],\r\n];\r\nconst height = world.length;\r\nconst width = world[0].length;\r\n// Each island gets a number 1 or more.\r\n// Some islands are the same number.\r\nconst idMap = {};\r\nconst idCounts = {};\r\nconst totalCounts = {};\r\nlet maxIslandId = 0;\r\nfunction newId() {\r\n const id = \u002B\u002BmaxIslandId;\r\n idMap[id] = id;\r\n idCounts[id] = 0;\r\n totalCounts[id] = 0;\r\n return id;\r\n}\r\nconst cellIdMap = Array.from(new Array(height), () =\u003E new Array(width).fill(0));\r\nfunction getIdOfCell(y, x) {\r\n if (y \u003C 0 || x \u003C 0)\r\n return 0;\r\n return cellIdMap[y][x];\r\n}\r\n// Assign Ids to the grid.\r\nfor (let y = 0; y \u003C height; \u002B\u002By) {\r\n for (let x = 0; x \u003C width; \u002B\u002Bx) {\r\n if (world[y][x] === W)\r\n continue;\r\n const topId = getIdOfCell(y - 1, x);\r\n const leftId = getIdOfCell(y, x - 1);\r\n if (topId \u0026\u0026 leftId \u0026\u0026 topId !== leftId) {\r\n // Two islands become one. Top wins.\r\n idMap[leftId] = topId;\r\n }\r\n const id = topId || leftId || newId();\r\n cellIdMap[y][x] = id;\r\n // Count island.\r\n \u002B\u002BidCounts[id];\r\n }\r\n}\r\n// ID mapping lookup with memoization cache.\r\nconst finalIdMemo = {};\r\nfunction getMappedId(id) {\r\n if (id in finalIdMemo) {\r\n return finalIdMemo[id];\r\n }\r\n let mappedId = id;\r\n while (mappedId !== idMap[mappedId]) {\r\n mappedId = idMap[mappedId];\r\n if (mappedId in finalIdMemo) {\r\n mappedId = finalIdMemo[mappedId];\r\n break;\r\n }\r\n }\r\n const finalId = mappedId;\r\n // Store the mapping result for faster\r\n // future lookup.\r\n mappedId = id;\r\n do {\r\n finalIdMemo[mappedId] = finalId;\r\n mappedId = idMap[mappedId];\r\n } while (mappedId !== idMap[mappedId] \u0026\u0026 !(mappedId in finalIdMemo));\r\n return finalId;\r\n}\r\n// Total island counts\r\nfor (const id in totalCounts) {\r\n totalCounts[getMappedId(id)] \u002B= idCounts[id];\r\n}\r\n// Make presentation prettier\r\nfor (const id in totalCounts) {\r\n if (totalCounts[id] === 0) {\r\n delete totalCounts[id];\r\n }\r\n}\r\n","IsDeferred":false},{"Name":"num2","Code":"const L = true;\r\nconst W = false;\r\nconst world = [\r\n [L, W, L, W, L, L, W],\r\n [L, L, L, W, L, W, W],\r\n [W, W, W, W, L, L, W],\r\n [L, W, W, W, W, W, W],\r\n [L, W, W, W, L, W, W],\r\n [L, L, W, L, L, W, W],\r\n [L, L, L, L, W, W, W],\r\n [W, W, W, W, W, W, W],\r\n [W, W, W, W, W, W, L],\r\n [W, W, W, W, W, L, L],\r\n [W, W, W, W, L, L, W],\r\n [W, W, W, L, L, W, W],\r\n [W, W, L, L, W, W, W],\r\n [W, L, L, W, W, W, W],\r\n [L, L, W, W, W, W, W],\r\n];\r\nconst height = world.length;\r\nconst width = world[0].length;\r\n\r\nconst visitedSet = new Set();\r\nlet largestIsland = [];\r\nlet currentIsland = [];\r\nfunction traverseIsland(world, x, y) {\r\n var _a, _b, _c, _d;\r\n const toVisitStack = [[x, y]];\r\n const currentIsland = [];\r\n while (toVisitStack.length) {\r\n const [currentX, currentY] = toVisitStack.shift();\r\n currentIsland.push([currentX, currentY]);\r\n if (currentX === 0 || currentY === 0 || currentY === width - 1 || currentX === height - 1) {\r\n return [];\r\n }\r\n if (!visitedSet.has(\u0060${currentX},${currentY \u002B 1}\u0060) \u0026\u0026 ((_a = world === null || world === void 0 ? void 0 : world[currentX]) === null || _a === void 0 ? void 0 : _a[currentY \u002B 1])) {\r\n toVisitStack.push([currentX, currentY \u002B 1]);\r\n }\r\n if (!visitedSet.has(\u0060${currentX},${currentY - 1}\u0060) \u0026\u0026 ((_b = world === null || world === void 0 ? void 0 : world[currentX]) === null || _b === void 0 ? void 0 : _b[currentY - 1])) {\r\n toVisitStack.push([currentX, currentY \u002B 1]);\r\n }\r\n if (!visitedSet.has(\u0060${currentX \u002B 1},${currentY}\u0060) \u0026\u0026 ((_c = world === null || world === void 0 ? void 0 : world[currentX \u002B 1]) === null || _c === void 0 ? void 0 : _c[currentY])) {\r\n toVisitStack.push([currentX, currentY \u002B 1]);\r\n }\r\n if (!visitedSet.has(\u0060${currentX - 1},${currentY}\u0060) \u0026\u0026 ((_d = world === null || world === void 0 ? void 0 : world[currentX - 1]) === null || _d === void 0 ? void 0 : _d[currentY])) {\r\n toVisitStack.push([currentX, currentY \u002B 1]);\r\n }\r\n visitedSet.add(\u0060${currentX},${currentY \u002B 1}\u0060);\r\n visitedSet.add(\u0060${currentX},${currentY - 1}\u0060);\r\n visitedSet.add(\u0060${currentX \u002B 1},${currentY}\u0060);\r\n visitedSet.add(\u0060${currentX - 1},${currentY}\u0060);\r\n }\r\n return currentIsland;\r\n}\r\nfor (let x = 1; x \u003C height - 1; x\u002B\u002B) {\r\n for (let y = 1; y \u003C width - 1; y\u002B\u002B) {\r\n const hasVisited = visitedSet.has(\u0060${x},${y}\u0060);\r\n visitedSet.add(\u0060${x},${y}\u0060);\r\n if (!hasVisited) {\r\n if (world[x][y]) {\r\n const currentIsland = traverseIsland(world, x, y);\r\n if (currentIsland.length \u003E largestIsland.length) {\r\n largestIsland = currentIsland;\r\n }\r\n }\r\n }\r\n }\r\n}","IsDeferred":false}]}