{"id":"6b093ddfbd4755deb67ee08c","slug":"number-of-islands","title":"Liczba wysp","summary":"Liczymy spójne obszary lądu na mapie z siatki znaków. Przeszukiwanie grafu (BFS/DFS) po kratkach – i pułapka głębokiej rekurencji na dużej mapie.","servedLang":"pl","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"medium","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["graphs","bfs","dfs","matrix","flood-fill","union-find"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** Number of 4-directionally connected groups of '1' cells. */\nfunction countIslands(grid: string[]): number {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":8,"tests":"test('one big island', () => {\n  expect(countIslands(['11110', '11010', '11000', '00000'])).toBe(1);\n});\n\ntest('several islands', () => {\n  expect(countIslands(['11000', '11000', '00100', '00011'])).toBe(3);\n});\n\ntest('diagonals do not connect', () => {\n  expect(countIslands(['101', '010', '101'])).toBe(5);\n});\n\ntest('a ring with an islet inside', () => {\n  expect(countIslands(['11111', '10001', '10101', '10001', '11111'])).toBe(2);\n});\n\ntest('empty grids and water only', () => {\n  expect(countIslands([])).toBe(0);\n  expect(countIslands([''])).toBe(0);\n  expect(countIslands(['000', '000'])).toBe(0);\n});\n\ntest('single cells and single rows', () => {\n  expect(countIslands(['1'])).toBe(1);\n  expect(countIslands(['1011101'])).toBe(3);\n  expect(countIslands(['1', '1', '0', '1'])).toBe(2);\n});\n\ntest('a winding snake is one island', () => {\n  expect(countIslands(['11111', '00001', '11111', '10000', '11111'])).toBe(1);\n});\n\ntest('performance: 300 × 300 all land and a checkerboard', () => {\n  const n = 300;\n  const land = Array.from({ length: n }, () => '1'.repeat(n));\n  expect(countIslands(land)).toBe(1);\n  const board = Array.from({ length: n }, (_, r) => (r % 2 ? '01' : '10').repeat(n / 2));\n  expect(countIslands(board)).toBe((n * n) / 2);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Number of 4-directionally connected groups of '1' cells.\n * @param {string[]} grid rows of '0'/'1'\n * @returns {number}\n */\nfunction countIslands(grid) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":8,"tests":"test('one big island', () => {\n  expect(countIslands(['11110', '11010', '11000', '00000'])).toBe(1);\n});\n\ntest('several islands', () => {\n  expect(countIslands(['11000', '11000', '00100', '00011'])).toBe(3);\n});\n\ntest('diagonals do not connect', () => {\n  expect(countIslands(['101', '010', '101'])).toBe(5);\n});\n\ntest('a ring with an islet inside', () => {\n  expect(countIslands(['11111', '10001', '10101', '10001', '11111'])).toBe(2);\n});\n\ntest('empty grids and water only', () => {\n  expect(countIslands([])).toBe(0);\n  expect(countIslands([''])).toBe(0);\n  expect(countIslands(['000', '000'])).toBe(0);\n});\n\ntest('single cells and single rows', () => {\n  expect(countIslands(['1'])).toBe(1);\n  expect(countIslands(['1011101'])).toBe(3);\n  expect(countIslands(['1', '1', '0', '1'])).toBe(2);\n});\n\ntest('a winding snake is one island', () => {\n  expect(countIslands(['11111', '00001', '11111', '10000', '11111'])).toBe(1);\n});\n\ntest('performance: 300 × 300 all land and a checkerboard', () => {\n  const n = 300;\n  const land = Array.from({ length: n }, () => '1'.repeat(n));\n  expect(countIslands(land)).toBe(1);\n  const board = Array.from({ length: n }, (_, r) => (r % 2 ? '01' : '10').repeat(n / 2));\n  expect(countIslands(board)).toBe((n * n) / 2);\n});\n"},{"lang":"python","runner":"browser","starter":"def count_islands(grid: list[str]) -> int:\n    \"\"\"Number of 4-directionally connected groups of '1' cells.\"\"\"\n    raise NotImplementedError\n","testCount":8,"tests":"def check(grid, expected):\n    got = count_islands(grid)\n    assert got == expected, f\"count_islands({grid[:5]}): expected {expected}, got {got}\"\n\n\ndef test_one_big_island():\n    check([\"11110\", \"11010\", \"11000\", \"00000\"], 1)\n\n\ndef test_several_islands():\n    check([\"11000\", \"11000\", \"00100\", \"00011\"], 3)\n\n\ndef test_diagonals_do_not_connect():\n    check([\"101\", \"010\", \"101\"], 5)\n\n\ndef test_ring_with_an_islet_inside():\n    check([\"11111\", \"10001\", \"10101\", \"10001\", \"11111\"], 2)\n\n\ndef test_empty_grids_and_water_only():\n    check([], 0)\n    check([\"\"], 0)\n    check([\"000\", \"000\"], 0)\n\n\ndef test_single_cells_and_single_rows():\n    check([\"1\"], 1)\n    check([\"1011101\"], 3)\n    check([\"1\", \"1\", \"0\", \"1\"], 2)\n\n\ndef test_winding_snake_is_one_island():\n    check([\"11111\", \"00001\", \"11111\", \"10000\", \"11111\"], 1)\n\n\ndef test_performance_300x300():\n    n = 300\n    check([\"1\" * n] * n, 1)\n    board = [(\"01\" if r % 2 else \"10\") * (n // 2) for r in range(n)]\n    check(board, n * n // 2)\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>Number of 4-directionally connected groups of '1' cells.</summary>\n    public static int CountIslands(string[] grid)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":8},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** Number of 4-directionally connected groups of '1' cells. */\n    static int countIslands(String[] grid) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":8},{"lang":"go","runner":"judge0","starter":"package main\n\n// CountIslands returns the number of 4-directionally connected groups of '1' cells.\nfunc CountIslands(grid []string) int {\n\tpanic(\"not implemented\")\n}\n","testCount":8}],"questionCount":null,"xp":20,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:55.102Z","requestedLang":"pl","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Mapa z drona to siatka kratek: "},{"type":"text","text":"'1'","marks":[{"type":"code"}]},{"type":"text","text":" oznacza ląd, "},{"type":"text","text":"'0'","marks":[{"type":"code"}]},{"type":"text","text":" – wodę. "},{"type":"text","text":"Wyspa","marks":[{"type":"bold"}]},{"type":"text","text":" to maksymalny obszar lądu, w którym z każdej kratki da się dojść do każdej innej, chodząc w górę, w dół, w lewo i w prawo (po skosie się nie liczy). Ile wysp jest na mapie?"}]},{"type":"heading","attrs":{"level":2,"id":"zadanie"},"content":[{"type":"text","text":"Zadanie"}]},{"type":"paragraph","content":[{"type":"text","text":"Funkcja dostaje siatkę jako listę napisów jednakowej długości (każdy napis to wiersz) i zwraca liczbę wysp. Wejście nie może zostać zmienione (napisy i tak są niezmienne – potrzebna jest własna tablica odwiedzin)."}]},{"type":"paragraph","content":[{"type":"text","text":"Sygnatury: "},{"type":"text","text":"countIslands(grid)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"count_islands(grid)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.CountIslands","marks":[{"type":"code"}]},{"type":"text","text":" (C#, "},{"type":"text","text":"string[]","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"Solution.countIslands","marks":[{"type":"code"}]},{"type":"text","text":" (Java, "},{"type":"text","text":"String[]","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"CountIslands","marks":[{"type":"code"}]},{"type":"text","text":" (Go, "},{"type":"text","text":"[]string","marks":[{"type":"code"}]},{"type":"text","text":")."}]},{"type":"heading","attrs":{"level":2,"id":"przyklady"},"content":[{"type":"text","text":"Przykłady"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Siatka"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Wynik"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"11110\", \"11010\", \"11000\", \"00000\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"1","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"11000\", \"11000\", \"00100\", \"00011\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"3","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"101\", \"010\", \"101\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"5","marks":[{"type":"code"}]},{"type":"text","text":" – skosy nie łączą"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"11111\", \"10001\", \"10101\", \"10001\", \"11111\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"2","marks":[{"type":"code"}]},{"type":"text","text":" – pierścień i wysepka w środku"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0","marks":[{"type":"code"}]}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"ograniczenia"},"content":[{"type":"text","text":"Ograniczenia"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"0 ≤ wiersze, kolumny ≤ 300","marks":[{"type":"code"}]},{"type":"text","text":" (do 90 000 kratek)."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Jedna wyspa może zajmować całą mapę – rekurencyjne DFS zejdzie wtedy na głębokość dziesiątek tysięcy wywołań."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"oczekiwana-zlozonosc"},"content":[{"type":"text","text":"Oczekiwana złożoność"}]},{"type":"paragraph","content":[{"type":"text","text":"Czas "},{"type":"text","text":"O(R · C)","marks":[{"type":"bold"}]},{"type":"text","text":", pamięć "},{"type":"text","text":"O(R · C)","marks":[{"type":"bold"}]},{"type":"text","text":" na tablicę odwiedzin i kolejkę/stos."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/number-of-islands/","title":"LeetCode 200 – Number of Islands","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:55.103Z","updatedAt":"2026-11-27T08:22:55.103Z"}