<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Woolaf&#39;s Techscope on Medium]]></title>
        <description><![CDATA[Stories by Woolaf&#39;s Techscope on Medium]]></description>
        <link>https://medium.com/@Woolaf?source=rss-fa3504ec9c17------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3LGq1CvT9k4Mmfr3</url>
            <title>Stories by Woolaf&amp;#39;s Techscope on Medium</title>
            <link>https://medium.com/@Woolaf?source=rss-fa3504ec9c17------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:56:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Woolaf/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[DSA][Graph] Course Schedule II]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-course-schedule-ii-0b584f01b6ec?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/0b584f01b6ec</guid>
            <category><![CDATA[kahns-algorithm]]></category>
            <category><![CDATA[adjacency-list]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[topological-sort]]></category>
            <category><![CDATA[graph]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Sat, 21 Mar 2026 07:59:46 GMT</pubDate>
            <atom:updated>2026-03-21T07:59:46.094Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/course-schedule-ii/description/">Leetcode 210</a></p><p>There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you <strong>must</strong> take course bi first if you want to take course ai.</p><ul><li>For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.</li></ul><p>Return <em>the ordering of courses you should take to finish all courses</em>. If there are many valid answers, return <strong>any</strong> of them. If it is impossible to finish all courses, return <strong>an empty array</strong>.</p><p><strong>[What can I ask?]<br>- </strong>Can an empty prerequisites list be given as input? YES<br>- Can there be duplicate prerequisite pairs? NO</p><p><strong>[Key Idea]<br>- </strong>Store course dependencies as an <strong>adjacency list</strong> and find a valid course ordering using <strong>BFS-based topological sort</strong>.</p><p><strong>[Solution 1] BFS (Topological Sort, Kahn’s Algorithm)</strong></p><pre>class Solution:<br>    def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -&gt; List[int]:<br>        indegree = [0] * numCourses<br>        crsMap   = [[] for i in range(numCourses)]<br>        crsOrder = []<br><br>        for crs, pre in prerequisites:<br>            indegree[crs] += 1<br>            crsMap[pre].append(crs)<br>        <br>        q = deque()<br>        for i in range(numCourses):<br>            if indegree[i] == 0:<br>                q.append(i)<br>        <br>        finish = 0<br>        while q:<br>            preCrs = q.popleft()<br>            finish += 1<br>            crsOrder.append(preCrs)<br>            for crs in crsMap[preCrs]:<br>                indegree[crs] -= 1<br>                if indegree[crs] == 0:<br>                    q.append(crs)<br>        <br>        if finish != numCourses:<br>            return []<br>        return crsOrder</pre><p>This problem is an extension of the classic <a href="https://medium.com/@Woolaf/dsa-graph-course-schedule-fd961c7cd85c"><strong>Course Schedule</strong></a><strong> </strong>problem. The key difference is that instead of just checking whether all courses can be finished, we need to <strong>return the actual ordering</strong>.</p><p>The idea is to model course dependencies as a <strong>directed graph</strong> and find a valid ordering using <strong>BFS-based topological sort (Kahn’s Algorithm)</strong>.</p><p>We define indegree[i] as the number of prerequisites course i requires. A course with indegree == 0 has no remaining prerequisites, meaning it can be taken immediately — so we use these as our <strong>BFS starting points</strong>.</p><p>From there, we repeatedly:</p><ol><li>Pop a course from the queue and append it to the result</li><li>Decrement the indegree of all courses that depend on it</li><li>If any course’s indegree drops to 0, push it onto the queue</li></ol><p>If the number of completed courses equals numCourses, there are no cycles and we return the ordering. Otherwise, a cycle exists and we return an empty array.</p><p>⏱️ Time Complexity<br>Let V be the number of courses (nodes) and E be the number of prerequisite relationships (edges). Since we visit every course (V) exactly once and check each connected edge (E) exactly once, the time complexity is O(V+E).</p><p>🧠 Space Complexity<br>Since we store V nodes and E edges in the adjacency list, the space complexity is O(V+E).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0b584f01b6ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Course Schedule]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-course-schedule-fd961c7cd85c?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd961c7cd85c</guid>
            <category><![CDATA[topological-sort]]></category>
            <category><![CDATA[fb]]></category>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Sun, 15 Mar 2026 08:58:02 GMT</pubDate>
            <atom:updated>2026-03-15T08:58:02.188Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/course-schedule/description/">Leetcode 207</a></p><p>There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you <strong>must</strong> take course bi first if you want to take course ai.</p><ul><li>For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.</li></ul><p>Return true if you can finish all courses. Otherwise, return false.</p><p><strong>[What can I ask?]<br>- </strong>Can an empty prerequisites list be given as input? If so, should we return True? YES</p><p><strong>[Key Idea]<br>- </strong>Store the prerequisite relationships between courses in an adjacency list, then traverse the graph using DFS/BFS to detect whether a cycle exists.</p><p>To take course A, you must first complete its prerequisite course B — this relationship can be represented as B → A. When this relationship is mapped out for all courses, being able to take every course means the resulting graph contains no cycles.</p><p>There are two common ways to store a graph: an adjacency matrix and an adjacency list. An adjacency matrix is straightforward to implement, but requires space proportional to the square of the number of nodes. An adjacency list, on the other hand, only stores the edges that actually exist, making it more space-efficient. For problems like this one, where the number of nodes and edges can be large, an adjacency list is the better choice.</p><p>Since graphs can be traversed using either DFS or BFS, we will solve this problem in two ways — Solution 1 uses DFS to detect cycles, and Solution 2 uses BFS via Topological Sort.</p><p><strong>[Solution 1] DFS</strong></p><pre>class Solution:<br>    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -&gt; bool:<br>        graph = defaultdict(list)<br>        visited = set()<br><br>        for n, p in prerequisites:<br>            graph[p].append(n)<br><br>        def hasCycle(prevC, nextC, visit):<br>            if nextC in visited:<br>                return False<br><br>            if nextC in visit:<br>                return True<br><br>            visit.add(nextC)<br>            for nei in graph[nextC]:<br>                if hasCycle(nextC, nei, visit):<br>                    return True<br><br>            visit.remove(nextC)<br>            visited.add(nextC)<br><br>            return False<br><br>        for i in range(numCourses):<br>            for j in graph[i]:<br>                if hasCycle(i, j, set()):<br>                    return False<br><br>        return True</pre><p>To detect a cycle using DFS, we use two sets. visit stores the courses currently on the DFS path, and visited stores the courses that have already been confirmed to have no cycles. The DFS traversal process is as follows:</p><ol><li>If the current course is in visit, we have encountered a node that was already visited on the current path — a cycle exists. Return True.</li><li>If the current course is in visited, this node was already confirmed to be cycle-free in a previous traversal. Return False.</li><li>Otherwise, add the current course to visit and recursively traverse all connected courses.</li><li>Once the recursive traversal is complete, we are leaving the current path — remove the current course from visit and add it to visited.</li></ol><p>By repeating this process for all courses, we can determine whether the graph contains a cycle.</p><p><strong>[Solution 2] BFS (Topological Sort, Kahn’s Algorithm)</strong></p><pre>class Solution:<br>    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -&gt; bool:<br>        indegree = [0] * numCourses<br>        adj = [[] for i in range(numCourses)]<br>        <br>        for src, dst in prerequisites:<br>            indegree[dst] += 1<br>            adj[src].append(dst)<br><br>        q = deque()<br>        for n in range(numCourses):<br>            if indegree[n] == 0:<br>                q.append(n)<br>        <br>        finish = 0<br>        while q:<br>            node = q.popleft()<br>            finish += 1<br>            for nei in adj[node]:<br>                indegree[nei] -= 1<br>                if indegree[nei] == 0:<br>                    q.append(nei)<br>                <br>        return finish == numCourses</pre><p>The same problem can also be solved using BFS, and this approach is known as Topological Sort. Topological Sort is the process of ordering tasks with dependencies according to their precedence relationships. Here, in-degree refers to the number of edges coming into a node — in other words, the number of prerequisites for a given course. The BFS traversal process is as follows:</p><ol><li>Add all nodes with an in-degree of 0 — courses with no prerequisites — to the queue.</li><li>Pop a node from the queue and decrease the in-degree of all nodes it points to by 1.</li><li>If any node’s in-degree becomes 0, add it to the queue.</li><li>If all nodes have been processed, there is no cycle. If the number of processed nodes is less than the total number of nodes, a cycle exists.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*PFaYVzxS8Qvl7aQG0PMVAQ.png" /></figure><p>⏱️ Time Complexity<br>Let V be the number of courses (nodes) and E be the number of prerequisite relationships (edges). Since we visit every course (V) exactly once and check each connected edge (E) exactly once, the time complexity is O(V+E).</p><p>🧠 Space Complexity<br>Since we store V nodes and E edges in the adjacency list, the space complexity is O(V+E).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd961c7cd85c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Surrounded Regions]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-surrounded-regions-be16ca3f64cc?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/be16ca3f64cc</guid>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[fb]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Sun, 01 Mar 2026 05:13:05 GMT</pubDate>
            <atom:updated>2026-03-01T05:13:05.829Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/surrounded-regions/description/">Leetcode 130</a></p><p>You are given an m x n matrix board containing <strong>letters</strong> &#39;X&#39; and &#39;O&#39;, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p><ul><li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally or vertically.</li><li><strong>Region</strong>: To form a region <strong>connect every</strong> &#39;O&#39; cell.</li><li><strong>Surround</strong>: A region is surrounded if none of the &#39;O&#39; cells in that region are on the edge of the board. Such regions are <strong>completely enclosed </strong>by &#39;X&#39; cells.</li></ul><p>To capture a <strong>surrounded region</strong>, replace all &#39;O&#39;s with &#39;X&#39;s <strong>in-place</strong> within the original board. You do not need to return anything.</p><p><strong>[What can I ask?]<br>- </strong>Can the board be empty? <strong>NO<br></strong>- Does the board only contain ‘O’ and ‘X’? <strong>NO</strong></p><p><strong>[Key Idea]<br>- </strong>Start BFS from the border cells and mark all connected ‘O’s with a temporary character. Then flip only the unmarked ‘O’s to ‘X’.</p><p><strong>[Solution 1] Breadth First Search</strong></p><pre>class Solution:<br>    def solve(self, board: List[List[str]]) -&gt; None:<br>        ROWS, COLS = len(board), len(board[0])<br>        noReverse = deque()<br><br>        direction = [(0, 1), (1, 0), (0, -1), (-1, 0)]<br>        for i in range(ROWS):<br>            for j in range(COLS):<br>                if i == 0 or j == 0 or i == ROWS - 1 or j == COLS - 1:<br>                    if board[i][j] == &quot;O&quot;:<br>                        board[i][j] = &quot;-&quot;<br>                        noReverse.append((i, j))<br>        <br>        def bfs():<br>            while noReverse:<br>                x, y = noReverse.popleft()<br>                for i in range(4):<br>                    dx = x + direction[i][0]<br>                    dy = y + direction[i][1]<br>                    if 0 &lt;= dx &lt; ROWS and 0 &lt;= dy &lt; COLS:<br>                        if board[dx][dy] == &quot;O&quot;:<br>                            board[dx][dy] = &quot;-&quot;<br>                            noReverse.append((dx, dy))<br>        <br>        bfs()<br><br>        for i in range(ROWS):<br>            for j in range(COLS):<br>                if board[i][j] == &quot;O&quot;:<br>                    board[i][j] = &quot;X&quot;<br>                elif board[i][j] == &quot;-&quot;:<br>                    board[i][j] = &quot;O&quot;</pre><p>This problem is similar in approach to the <a href="https://medium.com/@Woolaf/dsa-graph-pacific-atlantic-water-flow-f7637418c6b8"><strong>Pacific Atlantic Water Flow</strong></a> problem. In that problem, the goal was to find cells that are <em>reachable</em> from the outside (the oceans). In contrast, this problem requires identifying regions that are <em>not reachable</em> from the border.</p><p>Directly checking for regions that cannot be reached from the outside can be complex. Instead, the approach can be reversed: <strong>first mark all regions that are reachable from the border, and then process the remaining cells</strong>.</p><p>A region that is not completely surrounded by ‘X’ has at least one ‘O’ connected to the border. Therefore, any ‘O’ connected to the border should not be flipped. To achieve this, BFS or DFS is performed starting from all border ‘O’s. All connected ‘O’s are expanded and temporarily marked with a special character (e.g., ‘-’) to distinguish them as safe regions.</p><p>After the traversal, any remaining ‘O’s are fully surrounded by ‘X’ and can be flipped to ‘X’. Finally, the temporary characters are reverted back to ‘O’, resulting in a board that satisfies the problem’s requirements.</p><p>⏱️ Time Complexity<br>Each cell in the grid is checked once, and during BFS, each cell is visited at most once. Therefore, the overall time complexity is <strong>O(m × n)</strong>.</p><p>🧠 Space Complexity<br>A deque is used to store the coordinates of &#39;O&#39; cells. In the worst case (for example, when all cells are &#39;O&#39;), the space complexity is <strong>O(m × n)</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be16ca3f64cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Pacific Atlantic Water Flow]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-pacific-atlantic-water-flow-f7637418c6b8?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7637418c6b8</guid>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Fri, 27 Feb 2026 08:16:36 GMT</pubDate>
            <atom:updated>2026-02-27T08:16:36.536Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/pacific-atlantic-water-flow/description/">Leetcode 417</a></p><p>There is an m x n rectangular island that borders both the <strong>Pacific Ocean</strong> and <strong>Atlantic Ocean</strong>. The <strong>Pacific Ocean</strong> touches the island&#39;s left and top edges, and the <strong>Atlantic Ocean</strong> touches the island&#39;s right and bottom edges.</p><p>The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the <strong>height above sea level</strong> of the cell at coordinate (r, c).</p><p>The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell’s height is <strong>less than or equal to</strong> the current cell’s height. Water can flow from any cell adjacent to an ocean into the ocean.</p><p>Return <em>a </em><strong><em>2D list</em></strong><em> of grid coordinates </em>result<em> where </em>result[i] = [ri, ci]<em> denotes that rain water can flow from cell </em>(ri, ci)<em> to </em><strong><em>both</em></strong><em> the Pacific and Atlantic oceans</em>.</p><p><strong>[What can I ask?]<br>- </strong>Is it possible to have an empty heights array? <strong>NO<br></strong>- Does the height of a cell stay the same when the water flows from it? <strong>NO</strong></p><p><strong>[Key Idea]<br>- </strong>Use DFS to reverse-trace the cells that are reachable from the edges to both oceans and store them.</p><p><strong>[Solution 1] Depth First Search</strong></p><pre>class Solution:<br>    def pacificAtlantic(self, heights: List[List[int]]) -&gt; List[List[int]]:<br>        ROWS, COLS = len(heights), len(heights[0])<br>        pac, atl = set(), set()<br><br>        def dfs(r, c, visit, prevHeight):<br>            if ((r, c) in visit or<br>                min(r, c) &lt; 0 or r &gt;= ROWS or c &gt;= COLS or<br>                heights[r][c] &lt; prevHeight):<br>                return<br>                <br>            visit.add((r, c))<br>            dfs(r + 1, c, visit, heights[r][c])<br>            dfs(r, c - 1, visit, heights[r][c])<br>            dfs(r - 1, c, visit, heights[r][c])<br>            dfs(r, c + 1, visit, heights[r][c])<br><br>        for c in range(COLS):<br>            dfs(0, c, pac, heights[0][c])<br>            dfs(ROWS - 1, c, atl, heights[ROWS - 1][c])<br><br>        for r in range(ROWS):<br>            dfs(r, 0, pac, heights[r][0])<br>            dfs(r, COLS - 1, atl, heights[r][COLS - 1])<br><br>        res = []<br>        for r in range(ROWS):<br>            for c in range(COLS):<br>                if (r, c) in pac and (r, c) in atl:<br>                    res.append([r, c])<br>        return res</pre><p>To solve this problem using a brute force approach, we would need to repeatedly check for each cell whether water can flow in four directions (up, down, left, right), based on whether the neighboring cell’s height is less than or equal to the current cell’s height. However, by repeating this process, if cell A is already determined to not be able to reach the Pacific Ocean, but during the process of checking cell B, we pass through cell A, we would end up rechecking whether cell A can reach the Pacific Ocean. This leads to inefficient exploration.</p><p>Therefore, to avoid redundant exploration, we can use DFS (Depth-First Search) or BFS (Breadth-First Search) to visit the cells that water can flow to, while keeping track of the cells that have already been visited. This allows for an efficient solution.</p><p>First, we can start the DFS from the first row (ROW) and the first column (COL), which are directly connected to the Pacific Ocean. Similarly, we can start the DFS from the last row and the last column, which are directly connected to the Atlantic Ocean. The DFS function checks whether water can flow from each cell. If it can (i.e., the current height is greater than or equal to the previous height), the cell is added to the visited cells list, and DFS is called again in all four directions. After repeating this process for the pac set and the atl set, the final result will be the coordinates that appear in both sets.</p><p>⏱️ Time Complexity<br>We check each cell in the grid once, and each cell is visited at most once during the DFS process. Therefore, the overall time complexity is O(m × n).</p><p>🧠 Space Complexity<br>We store the reachable coordinates in a set, so in the worst case, the space complexity is O(m × n).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7637418c6b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Rotting Oranges]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-rotting-oranges-754827afa612?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/754827afa612</guid>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[breadth-first-search]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Wed, 18 Feb 2026 08:04:00 GMT</pubDate>
            <atom:updated>2026-02-18T08:04:00.959Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/rotting-oranges/description/">Leetcode 994</a></p><p>You are given an m x n grid where each cell can have one of three values:</p><ul><li>0 representing an empty cell,</li><li>1 representing a fresh orange, or</li><li>2 representing a rotten orange.</li></ul><p>Every minute, any fresh orange that is <strong>4-directionally adjacent</strong> to a rotten orange becomes rotten.</p><p>Return <em>the minimum number of minutes that must elapse until no cell has a fresh orange</em>. If <em>this is impossible, return</em> -1.</p><p><strong>[What can I ask?]<br>- </strong>Can the grid be empty? NO</p><p><strong>[Key Idea]<br>- </strong>Perform a multi-source BFS starting from all rotten oranges and simulate the spread level by level.</p><p><strong>[Solution 1] Breadth First Search</strong></p><pre>class Solution:<br>    def orangesRotting(self, grid: List[List[int]]) -&gt; int:<br>        ROW, COL = len(grid), len(grid[0])<br>        q = deque()<br>        fresh = 0<br><br>        for i in range(ROW):<br>            for j in range(COL):<br>                if grid[i][j] == 1:<br>                    fresh += 1<br>                if grid[i][j] == 2:<br>                    q.append((i, j))<br>        <br>        if fresh == 0:<br>            return 0<br><br>        minutes = 0<br>        direction = [(0, 1), (1, 0), (0, -1), (-1, 0)]<br><br>        while q and fresh &gt; 0:<br>            for _ in range(len(q)):<br>                x, y = q.popleft()<br>                for dx, dy in direction:<br>                    nx, ny = x + dx, y + dy<br>                    if 0 &lt;= nx &lt; ROW and 0 &lt;= ny &lt; COL and grid[nx][ny] == 1:<br>                        grid[nx][ny] = 2<br>                        fresh -= 1<br>                        q.append((nx, ny))<br>            minutes += 1<br><br>        return minutes if fresh == 0 else -1</pre><p>This problem can be approached using multi-source BFS, similar to <a href="https://medium.com/@Woolaf/dsa-graph-max-area-of-island-4312eaaa1be2">[Walls and Gates]</a>, where each Rotten Orange (2) serves as a starting point (source) to calculate the minimum time (minutes) required to reach all Fresh Oranges (1).</p><p>First, we put all rotten orange positions into a queue. Then, we process the queue by taking coordinates one by one and moving in the four directions (up, down, left, right). If we reach a Fresh Orange (1), we update it to Rotten (2) and add it to the queue so that the infection can continue in the next level.</p><p>Since one BFS level represents all oranges that get infected simultaneously, we increment minutes by 1 after processing each level.</p><p>BFS is a shortest-path algorithm, so the first time a fresh orange is visited is guaranteed to be the earliest moment it gets infected. Therefore, the order in which nodes are dequeued naturally guarantees the minimum time.</p><p>⏱️ Time Complexity<br>Every cell in the grid is visited at most once during the BFS. Therefore, the overall time complexity is <strong>O(m × n)</strong></p><p>🧠 Space Complexity<br>We do not use an additional visited array because we update the grid itself to track visited cells. However, the queue can hold up to O(m × n) elements in the worst case, so the space complexity is <strong>O(m × n)</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=754827afa612" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Walls and Gates]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-max-area-of-island-4312eaaa1be2?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/4312eaaa1be2</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[breadth-first-search]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Tue, 17 Feb 2026 13:10:31 GMT</pubDate>
            <atom:updated>2026-02-18T08:01:34.772Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/walls-and-gates/description/">Leetcode 286</a></p><p>You are given an m × n grid rooms initialized with these three possible values:</p><ul><li>-1 A wall or an obstacle.</li><li>0 A gate.</li><li>INF Infinity means an empty room. We use the value 2³¹ — 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.</li></ul><p>Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.</p><p><strong>[What can I ask?]<br>- </strong>Can the rooms grid be empty? YES</p><p><strong>[Key Idea]<br>- </strong>Perform <strong>multi-source BFS</strong> from all gates, updating the distance of each reachable empty room (INF) to the nearest gate.</p><p><strong>[Solution 1] Breadth First Search</strong></p><pre>class Solution:<br>    def wallsAndGates(self, rooms: List[List[int]]) -&gt; None:<br>        if not rooms:<br>            return<br>            <br>        INF = 2147483647<br>        ROW, COL = len(rooms), len(rooms[0])<br>        directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]<br><br>        q = deque()<br><br>        for i in range(ROW):<br>            for j in range(COL):<br>                if rooms[i][j] == 0:<br>                    q.append((i, j))<br><br>        while q:<br>            x, y = q.popleft()<br>            for dx, dy in directions:<br>                nx, ny = x + dx, y + dy<br>                if 0 &lt;= nx &lt; ROW and 0 &lt;= ny &lt; COL and rooms[nx][ny] == INF:<br>                    rooms[nx][ny] = rooms[x][y] + 1<br>                    q.append((nx, ny))</pre><p>This problem is about finding the <strong>shortest distance from each empty room (INF) to the nearest gate (0)</strong>. To solve it efficiently, we can use a <strong>multi-source BFS</strong> approach.</p><p><strong>Multi-source BFS</strong> means running BFS starting from multiple sources at the same time.</p><ul><li>First, add all gates (0) into a <strong>queue</strong>.</li><li>Then, repeatedly dequeue a position and explore its <strong>up, down, left, and right</strong> neighbors.</li><li>If a neighbor is an empty room (INF), update its distance to <strong>current distance + 1</strong> and enqueue it.</li><li>This way, all gates simultaneously propagate their distances, allowing us to efficiently fill in the shortest distances.</li></ul><p>While we could also use <strong>DFS</strong> starting from each gate to recursively update distances, DFS <strong>does not guarantee shortest paths</strong> because it explores deeply first.</p><ul><li>Some rooms may be revisited multiple times, leading to <strong>unnecessary computations</strong> and potential <strong>stack overflow</strong>.</li><li>In contrast, BFS ensures that the <strong>first time a room is visited, it is the shortest distance</strong>, so no additional calculations or comparisons are needed.</li></ul><p>⏱️ Time Complexity<br>Each cell is visited at most once during the BFS. Therefore, the overall time complexity is <strong>O(m × n)</strong>, where m and n are the grid dimensions.</p><p>🧠 Space Complexity<br>We do not use an extra visited array; instead, we update the input grid in place. Therefore, the additional space complexity is <strong>O(1)</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4312eaaa1be2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Max Area of Island]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-max-area-of-island-440b5a13e072?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/440b5a13e072</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[graph]]></category>
            <category><![CDATA[dfs]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Mon, 16 Feb 2026 09:24:56 GMT</pubDate>
            <atom:updated>2026-02-16T09:24:56.215Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/max-area-of-island/description/">Leetcode 695</a></p><p>You are given an m x n binary matrix grid. An island is a group of 1&#39;s (representing land) connected <strong>4-directionally</strong> (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.</p><p>The <strong>area</strong> of an island is the number of cells with a value 1 in the island.</p><p>Return <em>the maximum </em><strong><em>area</em></strong><em> of an island in </em>grid. If there is no island, return 0.</p><p><strong>[What can I ask?]<br>- </strong>Is it possible for the grid to be empty? NO</p><p><strong>[Key Idea]<br>- </strong>Traverse the grid, and when a cell with value 1 is found, perform a DFS starting from that cell. Explore all connected 1s in all four directions, accumulate the area, and mark the visited cells as 0 to avoid revisiting them.</p><p><strong>[Solution 1] Depth First Search</strong></p><pre>class Solution:<br>    def maxAreaOfIsland(self, grid: List[List[int]]) -&gt; int:<br>        ROW, COL = len(grid), len(grid[0])<br><br>        def dfs(x, y):<br>            if min(x, y) &lt; 0 or x &gt;= ROW or y &gt;= COL or grid[x][y] == 0:<br>                return 0<br>            <br>            area = 1<br>            grid[x][y] = 0<br>            <br>            area += dfs(x, y + 1)<br>            area += dfs(x + 1, y)<br>            area += dfs(x, y - 1)<br>            area += dfs(x - 1, y)<br>            <br>            return area<br>        <br>        res = 0<br>        for i in range(ROW):<br>            for j in range(COL):<br>                if grid[i][j]:<br>                    res = max(res, dfs(i, j))</pre><p>This problem is about finding the <strong>maximum area of an island</strong> in a given grid. The grid consists of 1s representing land and 0s representing water. The goal is to find and calculate the area of the largest island, where an island is defined as a group of 1s connected in all four directions (up, down, left, and right).</p><p>When a 1 is found in the grid, it indicates the start of a new island, so the area starts with a value of 1. Then, using DFS or BFS, we explore all connected 1s in the grid, recursively (or with a queue in BFS), to find the entire island and accumulate its area. After visiting a cell, we mark it as 0 to ensure it is not visited again.</p><p>Each DFS or BFS call explores one entire island and returns the area of that island. By continually updating the maximum area found, we can calculate the area of the largest island in the grid.</p><p>⏱️ Time Complexity<br>The entire grid is traversed once, and each cell is visited at most once during the DFS/BFS process. Therefore, the overall time complexity is <strong>O(m × n)</strong>, where m is the number of rows and n is the number of columns.</p><p>🧠 Space Complexity<br>No additional visited array is used to track the visited cells. Instead, the grid itself is modified in place to mark visited cells. Therefore, the additional space complexity is <strong>O(1)</strong>. However, in the worst case, the recursion stack for DFS may require <strong>O(m × n)</strong> space if the entire grid is a single island.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=440b5a13e072" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Graph] Number of Islands]]></title>
            <link>https://medium.com/@Woolaf/dsa-graph-number-of-islands-050fce98f046?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/050fce98f046</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[dfs]]></category>
            <category><![CDATA[graph]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Sun, 01 Feb 2026 07:46:30 GMT</pubDate>
            <atom:updated>2026-02-01T07:46:30.122Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/number-of-islands/description/">Leetcode 200</a></p><p>Given an m x n 2D binary grid grid which represents a map of &#39;1&#39;s (land) and &#39;0&#39;s (water), return <em>the number of islands</em>.</p><p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p><p><strong>[What can I ask?]<br>- </strong>Is it possible for the grid to be empty? NO</p><p><strong>[Key Idea]<br>- </strong>Iterate through the grid, and whenever we encounter a cell with value 1, start a DFS from that cell. Using DFS, explore all land cells connected horizontally and vertically, and mark each visited cell as 0.</p><p><strong>[Solution 1] DFS</strong></p><pre>class Solution:<br>    def numIslands(self, grid: List[List[str]]) -&gt; int:<br>        ROW, COL = len(grid), len(grid[0])<br><br>        def dfs(x, y):<br>            if x &lt; 0 or x &gt;= ROW or y &lt; 0 or y &gt;= COL:<br>                return<br>            if grid[x][y] == &quot;0&quot;:<br>                return<br>            <br>            grid[x][y] = &quot;0&quot;<br>            dfs(x, y + 1)<br>            dfs(x + 1, y)<br>            dfs(x, y - 1)<br>            dfs(x - 1, y)<br>        <br>        cnt = 0<br>        for i in range(ROW):<br>            for j in range(COL):<br>                if grid[i][j] == &quot;1&quot;:<br>                    cnt += 1<br>                    dfs(i, j)<br>        <br>        return cnt</pre><p>This problem can be solved using graph traversal algorithms such as <strong>DFS</strong> or <strong>BFS</strong>.</p><p>We iterate through the grid, and when we encounter a cell with value 1, it means we have found an unvisited part of a new island, so we increment the island count. Then, we use DFS or BFS to explore all land cells connected to that cell <strong>horizontally and vertically</strong>, marking each visited cell as 0 to avoid counting it again.</p><p>By doing this, all cells belonging to the same island are processed in a single traversal. If we later encounter another 1 while continuing to scan the grid, it indicates the discovery of a new, independent island, and we increment the count again.</p><p>⏱️ Time Complexity<br>We scan every cell in the grid, and each cell is visited <strong>at most once</strong> during the DFS or BFS traversal. Therefore, the overall time complexity is <strong>O(m × n)</strong>.</p><p>🧠 Space Complexity<br>Instead of using an additional visited array, we modify the input grid itself to mark visited cells. Thus, the <strong>extra space complexity is O(1)</strong>. However, if DFS is implemented recursively, the recursion call stack may take up to <strong>O(m × n)</strong> space in the worst case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=050fce98f046" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Backtracking] N-Queens]]></title>
            <link>https://medium.com/@Woolaf/dsa-backtracking-n-queens-36119c41a3ac?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/36119c41a3ac</guid>
            <category><![CDATA[n-queens-problem]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[backtracking]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Sat, 31 Jan 2026 11:19:24 GMT</pubDate>
            <atom:updated>2026-01-31T11:24:29.168Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/n-queens/description/">Leetcode 51</a></p><p>The <strong>n-queens</strong> puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.<br> Given an integer n, return <em>all distinct solutions to the </em><strong><em>n-queens puzzle</em></strong>. You may return the answer in <strong>any order</strong>.<br> Each solution contains a distinct board configuration of the n-queens’ placement, where &#39;Q&#39; and &#39;.&#39; both indicate a queen and an empty space, respectively.</p><p><strong>[What can I ask?]<br>- </strong>Does a valid solution always exist? If no solution exists, should we return an empty list? YES</p><p><strong>[Key Idea]<br>- </strong>Use backtracking to place the pieces after checking for conflicts between queens.</p><p><strong>[Solution 1] Backtracking</strong></p><pre>class Solution:<br>    def solveNQueens(self, n: int) -&gt; List[List[str]]:<br>        res, tmp = [], []<br>        <br>        def coordToStr(res):<br>            strRes = []<br>            for coord in res:<br>                tmpRes = []<br>                for i, j in coord:<br>                    tmpRes.append(&quot;.&quot; * j + &quot;Q&quot; + &quot;.&quot; * (n - j - 1))<br>                strRes.append(tmpRes)<br>            return strRes<br><br>        def noAttack(x, y):<br>            for i, j in tmp:<br>                if i == x or j == y:<br>                    return False<br>                <br>                if abs((i - x) / (j - y)) == 1:<br>                    return False<br>            return True<br><br>        def dfs(cnt,i):<br>            if cnt == n:<br>                res.append(tmp.copy())<br>                return<br>            <br>            for j in range(n):<br>                if noAttack(i, j):<br>                    tmp.append((i, j))<br>                    dfs(cnt + 1, i + 1)<br>                    tmp.pop()<br>            return<br>        <br>        for j in range(n):<br>            tmp.append((0, j))<br>            dfs(1, 1)<br>            tmp.pop()<br>        <br>        return coordToStr(res)</pre><p>This solution is a straightforward backtracking approach that directly implements the problem conditions without any additional optimizations.</p><p>In the N-Queens problem, no two queens can be placed in the same row, so the search proceeds by placing exactly one queen in each row. For the current row, we iterate through columns 0 to n−1 and check whether a queen can be placed in that position. If the position is valid, we proceed to the next row using DFS.</p><p>During the DFS process, we check whether the new queen conflicts with any previously placed queens. To do this, we iterate over all existing queen positions and verify three conditions:</p><ol><li>Whether it is in the same row.</li><li>Whether it is in the same column.</li><li>Whether the absolute difference between the rows equals the absolute difference between the columns (i.e., whether they are on the same diagonal).</li></ol><p>If any of these conditions are true, the queen cannot be placed at that position. This process continues recursively. When the number of placed queens (cnt) reaches n, it means all n queens have been placed validly, so we store the current list of coordinates as a solution.</p><p>After that, we backtrack by removing the most recently added coordinate and continue searching the next column in the same row.</p><p>Once all possibilities have been explored, the stored coordinate solutions are converted into a board representation for each row, using &quot;Q&quot; for a queen and &quot;.&quot; for an empty space, and returned as the final result.</p><p><strong>[Solution 2] Backtracking + Optimization</strong></p><pre>class Solution:<br>    def solveNQueens(self, n: int) -&gt; List[List[str]]:<br>        res = []<br>        board = [-1] * n # board[row] = col<br><br>        cols = set()<br>        diag1 = set() # row - col<br>        diag2= set() # row + col<br><br>        def dfs(row):<br>            if row == n:<br>                res.append(<br>                    [&quot;.&quot; * board[i] + &quot;Q&quot; + &quot;.&quot; * (n - board[i] - 1) for i in range(n)]<br>                )<br>                return<br>            <br>            for col in range(n):<br>                if col in cols or (row - col) in diag1 or (row + col) in diag2:<br>                    continue<br><br>                board[row] = col<br>                cols.add(col)<br>                diag1.add(row - col)<br>                diag2.add(row + col)<br><br>                dfs(row + 1)<br><br>                cols.remove(col)<br>                diag1.remove(row - col)<br>                diag2.remove(row + col)<br>        <br>        dfs(0)<br>        <br>        return res</pre><p>In the first solution, to check for conflicts, we iterate through all previously placed queens each time. This raises several questions:</p><ul><li>Can we determine conflicts more efficiently without iterating through all queens every time?</li><li>Since in the DFS structure each queen is always placed in a different row, is it necessary to check the row?</li><li>Is there a faster way to detect diagonal conflicts?</li></ul><p>Based on these observations, we can use set data structures to determine queen conflicts in <strong>O(1)</strong> time.</p><p>First, since the DFS always proceeds to the next row, there is no possibility of placing multiple queens in the same row. Therefore, conflict checks only need to consider columns and the two diagonals.</p><p>For column conflicts, instead of iterating over all placed queens’ column coordinates, we can store the columns that are already used in a set and simply check whether the current column exists in the set. This allows column conflict checks in <strong>O(1)</strong> time.</p><p>Diagonal conflicts can be optimized similarly. Two queens are on a diagonal with slope +1 if row1 − row2 = col1 − col2, row1 − col1 = row2 − col2 which can be rewritten as row − col being the same for both queens. For a diagonal with slope −1, row1 − row2 = -col1 + col2, row1 + col1 = row2 + col2 implies row + col is the same. By representing each diagonal using row − col and row + col and storing the used values in sets, diagonal conflicts can also be checked in <strong>O(1)</strong> time.</p><p>Finally, the process of converting coordinates into a string-based board can also be simplified. By storing the column index for each row as board[row] = col, we can generate each row of the board directly with &quot;.&quot; * board[row] + &quot;Q&quot; + &quot;.&quot; * (n - board[row] - 1) without any additional iteration over coordinates.</p><p>⏱️ Time Complexity<br>Since DFS is performed n times and in each row we choose columns such that no two queens share the same column, the number of available columns decreases as we go deeper. This structure is similar to a permutation search, and in the worst case, the time complexity is <strong>O(n!)</strong>.</p><p>🧠 Space Complexity<br>The set structures and the board array each require at most n space. However, the final result res stores all possible board configurations, each of size n × n. Therefore, the overall space complexity is <strong>O(n²)</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36119c41a3ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[DSA][Backtracking] Letter Combinations of a Phone Number]]></title>
            <link>https://medium.com/@Woolaf/dsa-backtracking-letter-combinations-of-a-phone-number-657366d9b406?source=rss-fa3504ec9c17------2</link>
            <guid isPermaLink="false">https://medium.com/p/657366d9b406</guid>
            <category><![CDATA[iteration]]></category>
            <category><![CDATA[backtracking]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[dfs]]></category>
            <dc:creator><![CDATA[Woolaf's Techscope]]></dc:creator>
            <pubDate>Sun, 11 Jan 2026 10:59:47 GMT</pubDate>
            <atom:updated>2026-01-11T11:00:02.622Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/">Leetcode 17</a></p><p>Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>. A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p><p><strong>[What can I ask?]<br>- </strong>Can the input digits be given? NO</p><p><strong>[Key Idea]<br>- </strong>Extend the combinations by appending the letters corresponding to the next digit to the strings generated so far.</p><p><strong>[Solution 1] Backtracking</strong></p><pre>class Solution:<br>    def letterCombinations(self, digits: str) -&gt; List[str]:<br>        res = []<br>        digitToChar = {<br>            &quot;2&quot;: &quot;abc&quot;,<br>            &quot;3&quot;: &quot;def&quot;,<br>            &quot;4&quot;: &quot;ghi&quot;,<br>            &quot;5&quot;: &quot;jkl&quot;,<br>            &quot;6&quot;: &quot;mno&quot;,<br>            &quot;7&quot;: &quot;qprs&quot;,<br>            &quot;8&quot;: &quot;tuv&quot;,<br>            &quot;9&quot;: &quot;wxyz&quot;,<br>        }<br><br>        def backtrack(i, curStr):<br>            if len(curStr) == len(digits):<br>                res.append(curStr)<br>                return<br>            for c in digitToChar[digits[i]]:<br>                backtrack(i + 1, curStr + c)<br><br>        if digits:<br>            backtrack(0, &quot;&quot;)<br><br>        return res</pre><p>This problem requires generating all possible string combinations by choosing one letter for each digit in ‘digits’.</p><p>The key question is how to extend the combinations by appending the letters of the next digit to the strings generated so far. There are two main ways to implement this: backtracking (DFS) and iteration (BFS).</p><p>In the backtracking approach, we start from the first digit in ‘digits’ and select one of the letters mapped to that digit to add to the current string. We then move on to the next digit and recursively repeat the same process.</p><p>When the length of the current string matches the length of ‘digits’, it is added as a complete combination to the result. After the recursive call finishes, we revert to the previous state (backtrack) and try the next letter, exploring all possible combinations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*V3v-i1kiwGGAD9Vy" /></figure><p><strong>[Solution 2] Iteration</strong></p><pre>class Solution:<br>    def letterCombinations(self, digits: str) -&gt; List[str]:<br>        if not digits:<br>            return []<br><br>        res = [&quot;&quot;]<br>        digitToChar = {<br>            &quot;2&quot;: &quot;abc&quot;,<br>            &quot;3&quot;: &quot;def&quot;,<br>            &quot;4&quot;: &quot;ghi&quot;,<br>            &quot;5&quot;: &quot;jkl&quot;,<br>            &quot;6&quot;: &quot;mno&quot;,<br>            &quot;7&quot;: &quot;qprs&quot;,<br>            &quot;8&quot;: &quot;tuv&quot;,<br>            &quot;9&quot;: &quot;wxyz&quot;,<br>        }<br><br>        for digit in digits:<br>            tmp = []<br>            for curStr in res:<br>                for c in digitToChar[digit]:<br>                    tmp.append(curStr + c)<br>            res = tmp<br>        return res</pre><p>It is also possible to implement this problem without recursion, using only iteration. In this approach, at each step, a new array is created to extend the current combinations.</p><p>Starting with ‘res = [“”]’ represents the initial state where no letters have been chosen yet. Then, for each digit in ‘digits’ in order, all strings in ‘res’ are combined with every letter mapped to that digit to form new strings.</p><p>The newly generated strings are collected in a temporary array ‘tmp’, and ‘res’ is updated to ‘tmp’, which allows us to manage the combinations without mixing strings from previous and current steps. By extending each step sequentially in this way, all possible combinations can be generated without recursion or backtracking.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/0*IResCz9XCoS0eBdV" /></figure><p>⏱️ Time Complexity<br>If the length of `digits` is n, each digit can map to up to 4 letters, so the total number of possible combinations is 4ⁿ. Creating each combination requires copying a string of length n, so generating a single combination takes O(n) time. Therefore, the overall time complexity is O(n × 4ⁿ).</p><p>🧠 Space Complexity<br>The final result array contains 4ⁿ strings, each of length n, so the space complexity is also O(n × 4ⁿ).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=657366d9b406" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>