🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 2 Min Lesezeit
0

Solving LeetCode's Number of Islands: A Deep Dive into DFS

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Introduction



The Number of Islands problem is a classic example of depth-first search (DFS) application in grid traversal. In this article, I'll walk you through my thought process and implementation strategy.



Let me explain my approach to the Number of Islands problem.






Problem Understanding



This problem can be categorized as a grid traversal pattern where we need to:




  1. Count the number of distinct islands

  2. Define an island as a group of connected '1's

  3. Handle the grid modification efficiently






Approach



I'll use DFS (Depth-First Search) for this problem because:




  1. It's a natural fit for exploring connected components

  2. We can modify the input grid to track visited cells

  3. It provides a clean recursive solution






Implementation Strategy



Let me break down the key components:




  1. Main Function:




  • Iterate through each cell in the grid

  • When we find a '1', increment our island counter

  • Start DFS exploration from that cell




  1. DFS Helper Function:


    • Check boundary conditions

    • Verify if current cell is part of an island

    • Mark visited cells

    • Explore in all four directions








Edge Cases




  • Empty grid

  • Single row/column grid

  • Grid with no islands

  • Grid with all islands






Complexity Analysis




  • Time: O(M × N) where M is rows and N is columns

  • Space: O(M × N) worst case for the recursion stack






Key Optimization



Instead of using a separate visited set, we modify the input grid directly by marking visited cells with a different value. This approach:




  1. Saves space

  2. Simplifies our logic

  3. Maintains the connected component property






My Implementation






CODE
/**
* @param {character[][]} grid
* @return {number}
*/

var numIslands = function (grid) {
// We start with a counter and grid size
var count = 0;
const y = grid.length;
const x = grid[0].length;
// Handles the recursive exploration
function dfs(i, j) {
if (i < 0 || i >= y || j < 0 || j >= x || grid[i][j] !== "1") return;
// Mark the cell as visited
grid[i][j] = "0";
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i + 1, j);
dfs(i, j - 1);
}
// Main loop to iterate through the grid
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === "1") {
count++;
dfs(i, j);
}
}
}
return count;
};

const grid = [
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"],
];

console.log(numIslands(grid));


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Solving LeetCode's Number of Islands: A Deep Dive into DFS

Thematisch verwandte Begriffe: Solving, LeetCodes, Number, Islands · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...