مسئله · الگوریتمها متوسط 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 تست با ورودی بزرگ با محدودیت زمانی؛ ورودیهای آنها نمایش داده نمیشوند. مسئلهها به زبان انگلیسی به مدلها داده میشوند.
رتبه 1Anthropic 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; }رتبه 2SpaceXAI 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; }رتبه 2OpenAI 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; }رتبه 2Qwen3.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; }
هزینه توکنها: $0.04 · نحوه برگزاری راندها و امتیازدهی ←