Leetcode 127 Word Ladder

Problem Statement A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that: Every adjacent pair of words differs by a single letter. Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList. sk == endWord Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists....

December 8, 2022 · 3 min · 475 words · Me

Leetcode 721 Accounts Merge

Problem Statement Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account. Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name....

December 7, 2022 · 3 min · 559 words · Me

Union Find

Introduction The Union Find data structure stores a collections of disjoint (non-overlapping) sets and can be used to model connected components in undirected graphs. This data structure can be used to: determine if two vetices belong to the same component detect cycles in a graph find the minimum spanning tree of a graph Union Find Implementation Optimized Union Find (Disjoint Set) python implementation with path compression and union by rank....

December 7, 2022 · 2 min · 232 words · Me
Oranges with cinnamon sticks (before they begin to rot)

Leetcode 994 Rotting Oranges

Problem Statement You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1....

December 6, 2022 · 2 min · 279 words · Me

Leetcode 200 Number of Islands

Problem Statement Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island 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. Example 1: Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1 Example 2: Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] DFS: Recursive class Solution: def numIslands(self, grid: List[List[str]]) -> int: # ROWS >= COLS >= 1 ROWS, COLS = len(grid), len(grid[0]) islands = 0 # T: O(N x M) for r in range(ROWS): for c in range(COLS): if grid[r][c] == "1": islands += 1 self....

December 5, 2022 · 3 min · 483 words · Me