Tugas · algoritma sedang 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.
Tanda tangan
/**
* @param {string[]} grid
* @returns {number}
*/
function count_islands(grid)Contoh
count_islands(["##...", "##...", "..#..", "...##"]) // -> 3
count_islands([".#.", "###", ".#."]) // -> 1Ditambah 7 pengujian tersembunyi dan 2 pengujian input besar dengan batas waktu; inputnya tidak ditampilkan. Tugas diberikan kepada model dalam bahasa Inggris.
#1Anthropic Claude Sonnet 5.5selesai · 11 dari 11 pengujianrespons dalam 5.4 dtk · waktu eksekusi 140 md · $0.0093 ✓ count_islands(["##...", "##...", "..#..", "...##"])diharapkan 3 2 md ✓ count_islands([".#.", "###", ".#."])diharapkan 1 <1 md ✓ <1 md✓ 1 md✓ <1 md✓ <1 md✓ 2 md✓ 2 md✓ <1 md⏱ 101 md⏱ 28 mdKode
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; }#2SpaceXAI Grok 4.7selesai · 11 dari 11 pengujianrespons dalam 35 dtk · waktu eksekusi 217 md · $0.02 ✓ count_islands(["##...", "##...", "..#..", "...##"])diharapkan 3 3 md ✓ count_islands([".#.", "###", ".#."])diharapkan 1 2 md ✓ <1 md✓ <1 md✓ <1 md✓ <1 md✓ <1 md✓ 1 md✓ 1 md⏱ 171 md⏱ 35 mdKode
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; }#2OpenAI GPT-6 Lunaselesai · 11 dari 11 pengujianrespons dalam 3.7 dtk · waktu eksekusi 220 md · $0.0003 ✓ count_islands(["##...", "##...", "..#..", "...##"])diharapkan 3 2 md ✓ count_islands([".#.", "###", ".#."])diharapkan 1 <1 md ✓ <1 md✓ 1 md✓ <1 md✓ 1 md✓ <1 md✓ 3 md✓ <1 md⏱ 131 md⏱ 79 mdKode
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; }#2Qwen3.8 Max (0902)selesai · 11 dari 11 pengujianrespons dalam 26 dtk · waktu eksekusi 234 md · $0.0085 ✓ count_islands(["##...", "##...", "..#..", "...##"])diharapkan 3 3 md ✓ count_islands([".#.", "###", ".#."])diharapkan 1 1 md ✓ <1 md✓ <1 md✓ 1 md✓ <1 md✓ <1 md✓ 2 md✓ 2 md⏱ 134 md⏱ 87 mdKode
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; }
Biaya token: $0.04 · Cara babak dijalankan dan dinilai →