Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find Island 2
(version: 0)
Comparing performance of:
num1 vs num2
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
num1
/* output */ const L = true; const W = false; const world = [ [L, W, L, W, L, L, W], [L, L, L, W, L, W, W], [W, W, W, W, L, L, W], [L, W, W, W, W, W, W], [L, W, W, W, L, W, W], [L, L, W, L, L, W, W], [L, L, L, L, W, W, W], [W, W, W, W, W, W, W], [W, W, W, W, W, W, L], [W, W, W, W, W, L, L], [W, W, W, W, L, L, W], [W, W, W, L, L, W, W], [W, W, L, L, W, W, W], [W, L, L, W, W, W, W], [L, L, W, W, W, W, W], ]; const height = world.length; const width = world[0].length; // Each island gets a number 1 or more. // Some islands are the same number. const idMap = {}; const idCounts = {}; const totalCounts = {}; let maxIslandId = 0; function newId() { const id = ++maxIslandId; idMap[id] = id; idCounts[id] = 0; totalCounts[id] = 0; return id; } const cellIdMap = Array.from(new Array(height), () => new Array(width).fill(0)); function getIdOfCell(y, x) { if (y < 0 || x < 0) return 0; return cellIdMap[y][x]; } // Assign Ids to the grid. for (let y = 0; y < height; ++y) { for (let x = 0; x < width; ++x) { if (world[y][x] === W) continue; const topId = getIdOfCell(y - 1, x); const leftId = getIdOfCell(y, x - 1); if (topId && leftId && topId !== leftId) { // Two islands become one. Top wins. idMap[leftId] = topId; } const id = topId || leftId || newId(); cellIdMap[y][x] = id; // Count island. ++idCounts[id]; } } // ID mapping lookup with memoization cache. const finalIdMemo = {}; function getMappedId(id) { if (id in finalIdMemo) { return finalIdMemo[id]; } let mappedId = id; while (mappedId !== idMap[mappedId]) { mappedId = idMap[mappedId]; if (mappedId in finalIdMemo) { mappedId = finalIdMemo[mappedId]; break; } } const finalId = mappedId; // Store the mapping result for faster // future lookup. mappedId = id; do { finalIdMemo[mappedId] = finalId; mappedId = idMap[mappedId]; } while (mappedId !== idMap[mappedId] && !(mappedId in finalIdMemo)); return finalId; } // Total island counts for (const id in totalCounts) { totalCounts[getMappedId(id)] += idCounts[id]; } // Make presentation prettier for (const id in totalCounts) { if (totalCounts[id] === 0) { delete totalCounts[id]; } }
num2
const L = true; const W = false; const world = [ [L, W, L, W, L, L, W], [L, L, L, W, L, W, W], [W, W, W, W, L, L, W], [L, W, W, W, W, W, W], [L, W, W, W, L, W, W], [L, L, W, L, L, W, W], [L, L, L, L, W, W, W], [W, W, W, W, W, W, W], [W, W, W, W, W, W, L], [W, W, W, W, W, L, L], [W, W, W, W, L, L, W], [W, W, W, L, L, W, W], [W, W, L, L, W, W, W], [W, L, L, W, W, W, W], [L, L, W, W, W, W, W], ]; const height = world.length; const width = world[0].length; const visitedSet = new Set(); let largestIsland = []; let currentIsland = []; function traverseIsland(world, x, y) { var _a, _b, _c, _d; const toVisitStack = [[x, y]]; const currentIsland = []; while (toVisitStack.length) { const [currentX, currentY] = toVisitStack.shift(); currentIsland.push([currentX, currentY]); if (currentX === 0 || currentY === 0 || currentY === width - 1 || currentX === height - 1) { return []; } if (!visitedSet.has(`${currentX},${currentY + 1}`) && ((_a = world === null || world === void 0 ? void 0 : world[currentX]) === null || _a === void 0 ? void 0 : _a[currentY + 1])) { toVisitStack.push([currentX, currentY + 1]); } if (!visitedSet.has(`${currentX},${currentY - 1}`) && ((_b = world === null || world === void 0 ? void 0 : world[currentX]) === null || _b === void 0 ? void 0 : _b[currentY - 1])) { toVisitStack.push([currentX, currentY + 1]); } if (!visitedSet.has(`${currentX + 1},${currentY}`) && ((_c = world === null || world === void 0 ? void 0 : world[currentX + 1]) === null || _c === void 0 ? void 0 : _c[currentY])) { toVisitStack.push([currentX, currentY + 1]); } if (!visitedSet.has(`${currentX - 1},${currentY}`) && ((_d = world === null || world === void 0 ? void 0 : world[currentX - 1]) === null || _d === void 0 ? void 0 : _d[currentY])) { toVisitStack.push([currentX, currentY + 1]); } visitedSet.add(`${currentX},${currentY + 1}`); visitedSet.add(`${currentX},${currentY - 1}`); visitedSet.add(`${currentX + 1},${currentY}`); visitedSet.add(`${currentX - 1},${currentY}`); } return currentIsland; } for (let x = 1; x < height - 1; x++) { for (let y = 1; y < width - 1; y++) { const hasVisited = visitedSet.has(`${x},${y}`); visitedSet.add(`${x},${y}`); if (!hasVisited) { if (world[x][y]) { const currentIsland = traverseIsland(world, x, y); if (currentIsland.length > largestIsland.length) { largestIsland = currentIsland; } } } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
num1
num2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I can see that there is an example of a JavaScript code snippet, but it seems to be incomplete and not executable on its own. However, I'll try to identify the problem with the code. The code appears to be implementing a flood fill algorithm to find the largest island in a given 2D grid (represented by `world`). The function `traverseIsland` is used to explore the islands, but it seems to have some logical errors and inconsistencies. For example: * The function checks if the current position is at the boundary of the grid, but then adds the neighboring positions to the stack without checking their validity. * The function returns an empty array when it encounters a boundary, which doesn't make sense in this context. To fix these issues, I would need more information about the desired behavior and constraints. However, here's a simplified version of the code that fixes some of the logical errors: ```javascript function traverseIsland(world, x, y) { const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; let islands = []; function dfs(x, y) { if (x < 0 || y < 0 || x >= world.length || y >= world[0].length || !world[x][y]) return; const val = world[x][y]; world[x][y] = 0; // Mark as visited islands.push([x, y]); for (const [dx, dy] of directions) { dfs(x + dx, y + dy); } } dfs(x, y); return islands; } function largestIsland(world) { const visitedSet = new Set(); let largestIslandSize = 0; function traverseIsland(x, y) { if (x < 0 || y < 0 || x >= world.length || y >= world[0].length) return 0; if (!world[x][y]) return 0; const key = `${x},${y}`; if (visitedSet.has(key)) return 0; visitedSet.add(key); let size = 1; for (const [dx, dy] of [[-1, 0], [1, 0], [0, -1], [0, 1]]) { size += traverseIsland(x + dx, y + dy); } return size; } for (let x = 0; x < world.length; x++) { for (let y = 0; y < world[0].length; y++) { if (!world[x][y]) continue; const islandSize = traverseIsland(x, y); largestIslandSize = Math.max(largestIslandSize, islandSize); } } return largestIslandSize; } ``` You can use this code as a starting point and adapt it to your specific requirements.
Related benchmarks:
point in triangle
point in triangle
The fastest way to find the distance between two points
Testando
Haversine performance
Comments
Confirm delete:
Do you really want to delete benchmark?