任务 · 算法 中等 JavaScript ·
Count islands
The map is a list of equal-length strings; "#" is land and "." is water. Land cells connected horizontally or vertically (not diagonally) form one island. Return the number of islands. Maps can be up to 700×700, and a single island can cover the whole map.
函数签名
/**
* @param {string[]} grid
* @returns {number}
*/
function count_islands(grid)示例
count_islands(["##...", "##...", "..#..", "...##"]) // -> 3
count_islands([".#.", "###", ".#."]) // -> 1另有 7 个隐藏测试和 2 个限时海量数据测试;其输入数据不公开。 任务以英文提示词提供给模型。
第 1 名Anthropic Claude Sonnet 5.5已解决 · 通过 11/11 个测试生成耗时 5.4 秒 · 执行耗时 140 毫秒 · $0.0093 ✓ count_islands(["##...", "##...", "..#..", "...##"])预期 3 2 毫秒 ✓ count_islands([".#.", "###", ".#."])预期 1 <1 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ <1 毫秒✓ 2 毫秒✓ 2 毫秒✓ <1 毫秒⏱ 101 毫秒⏱ 28 毫秒代码
function count_islands(grid) { const h = grid.length; if (h === 0) return 0; const w = grid[0].length; if (w === 0) return 0; const n = h * w; const a = new Uint8Array(n); for (let i = 0; i < h; i++) { const s = grid[i]; const base = i * w; const l = Math.min(w, s.length); for (let j = 0; j < l; j++) { if (s.charCodeAt(j) === 35) a[base + j] = 1; } } const stack = new Int32Array(n); let count = 0; for (let start = 0; start < n; start++) { if (a[start] === 0) continue; count++; a[start] = 0; let sp = 0; stack[sp++] = start; while (sp > 0) { const c = stack[--sp]; const col = c % w; if (c >= w && a[c - w]) { a[c - w] = 0; stack[sp++] = c - w; } if (c + w < n && a[c + w]) { a[c + w] = 0; stack[sp++] = c + w; } if (col > 0 && a[c - 1]) { a[c - 1] = 0; stack[sp++] = c - 1; } if (col < w - 1 && a[c + 1]) { a[c + 1] = 0; stack[sp++] = c + 1; } } } return count; }第 2 名SpaceXAI Grok 4.7已解决 · 通过 11/11 个测试生成耗时 35 秒 · 执行耗时 217 毫秒 · $0.02 ✓ count_islands(["##...", "##...", "..#..", "...##"])预期 3 3 毫秒 ✓ count_islands([".#.", "###", ".#."])预期 1 2 毫秒 ✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ 1 毫秒✓ 1 毫秒⏱ 171 毫秒⏱ 35 毫秒代码
function count_islands(grid) { const rows = grid.length; if (rows === 0) return 0; const cols = grid[0].length; if (cols === 0) return 0; const n = rows * cols; const visited = new Uint8Array(n); const queue = new Int32Array(n); let count = 0; for (let i = 0; i < rows; i++) { const rowStr = grid[i]; const base = i * cols; for (let j = 0; j < cols; j++) { if (rowStr[j] === '#' && visited[base + j] === 0) { count++; let head = 0; let tail = 0; queue[tail++] = base + j; visited[base + j] = 1; while (head < tail) { const pos = queue[head++]; const r = (pos / cols) | 0; const c = pos - r * cols; if (r > 0) { const up = pos - cols; if (visited[up] === 0 && grid[r - 1][c] === '#') { visited[up] = 1; queue[tail++] = up; } } if (r + 1 < rows) { const down = pos + cols; if (visited[down] === 0 && grid[r + 1][c] === '#') { visited[down] = 1; queue[tail++] = down; } } if (c > 0 && visited[pos - 1] === 0 && grid[r][c - 1] === '#') { visited[pos - 1] = 1; queue[tail++] = pos - 1; } if (c + 1 < cols && visited[pos + 1] === 0 && grid[r][c + 1] === '#') { visited[pos + 1] = 1; queue[tail++] = pos + 1; } } } } } return count; }第 2 名OpenAI GPT-6 Luna已解决 · 通过 11/11 个测试生成耗时 3.7 秒 · 执行耗时 220 毫秒 · $0.0003 ✓ count_islands(["##...", "##...", "..#..", "...##"])预期 3 2 毫秒 ✓ count_islands([".#.", "###", ".#."])预期 1 <1 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ 3 毫秒✓ <1 毫秒⏱ 131 毫秒⏱ 79 毫秒代码
function count_islands(grid) { const rows = grid.length; if (rows === 0) return 0; const cols = grid[0].length; const size = rows * cols; const visited = new Uint8Array(size); const stack = new Int32Array(size); let islands = 0; for (let r = 0; r < rows; r++) { const row = grid[r]; for (let c = 0; c < cols; c++) { const start = r * cols + c; if (row[c] !== '#' || visited[start]) continue; islands++; let top = 0; stack[top++] = start; visited[start] = 1; while (top > 0) { const cell = stack[--top]; const x = cell % cols; if (x > 0) { const next = cell - 1; if (!visited[next] && grid[Math.floor(next / cols)][x - 1] === '#') { visited[next] = 1; stack[top++] = next; } } if (x + 1 < cols) { const next = cell + 1; if (!visited[next] && grid[Math.floor(next / cols)][x + 1] === '#') { visited[next] = 1; stack[top++] = next; } } if (cell >= cols) { const next = cell - cols; if (!visited[next] && grid[Math.floor(next / cols)][x] === '#') { visited[next] = 1; stack[top++] = next; } } if (cell + cols < size) { const next = cell + cols; if (!visited[next] && grid[Math.floor(next / cols)][x] === '#') { visited[next] = 1; stack[top++] = next; } } } } } return islands; }第 2 名Qwen3.8 Max (0902)已解决 · 通过 11/11 个测试生成耗时 26 秒 · 执行耗时 234 毫秒 · $0.0085 ✓ count_islands(["##...", "##...", "..#..", "...##"])预期 3 3 毫秒 ✓ count_islands([".#.", "###", ".#."])预期 1 1 毫秒 ✓ <1 毫秒✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ <1 毫秒✓ 2 毫秒✓ 2 毫秒⏱ 134 毫秒⏱ 87 毫秒代码
function count_islands(grid) { if (grid.length === 0) return 0; const rows = grid.length; const cols = grid[0].length; const visited = new Uint8Array(rows * cols); let count = 0; for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] === '#' && !visited[r * cols + c]) { count++; const queue = [r * cols + c]; visited[r * cols + c] = 1; let head = 0; while (head < queue.length) { const idx = queue[head++]; const cr = (idx / cols) | 0; const cc = idx % cols; if (cr > 0 && !visited[idx - cols] && grid[cr - 1][cc] === '#') { visited[idx - cols] = 1; queue.push(idx - cols); } if (cr < rows - 1 && !visited[idx + cols] && grid[cr + 1][cc] === '#') { visited[idx + cols] = 1; queue.push(idx + cols); } if (cc > 0 && !visited[idx - 1] && grid[cr][cc - 1] === '#') { visited[idx - 1] = 1; queue.push(idx - 1); } if (cc < cols - 1 && !visited[idx + 1] && grid[cr][cc + 1] === '#') { visited[idx + 1] = 1; queue.push(idx + 1); } } } } } return count; }
Token 成本: $0.04 · 轮次运行与评分规则 →