Posts

Showing posts from September, 2021

Performing a BFS on all directions in a Binary Tree

Image
I've tried this problem before pre-pandemic and failed to realize that it requires something not very common: to perform a BFS on a binary tree across all directions. The "across all directions" means that it is necessary to create the link node -> parent. This can be done using a simple DFS storing the info in a hash table. Then once you find the node related to K (another DFS), then perform a BFS going left, right, parent. All the code together should run in linear time, with a constant 3 (O(3N)). Code is below, cheers, ACC. Closest Leaf in a Binary Tree - LeetCode Given the  root  of a binary tree where every node has  a unique value  and a target integer  k , return  the value of the  nearest leaf node  to the target  k  in the tree . Nearest to a leaf  means the least number of edges traveled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.   Example 1: Input: root = [1,3,2], k = 1 Output: 2 Explanation: Eit

On a non-recursive solution

Image
Problem was this one:  Smallest Greater Multiple Made of Two Digits - LeetCode Given three integers,  k ,  digit1 , and  digit2 , you want to find the  smallest  integer that is: Larger  than  k , A  multiple  of  k , and Comprised of  only  the digits  digit1  and/or  digit2 . Return  the  smallest  such integer. If no such integer exists or the integer exceeds the limit of a signed 32-bit integer ( 2 31  - 1 ), return  -1 .   Example 1: Input: k = 2, digit1 = 0, digit2 = 2 Output: 20 Explanation: 20 is the first integer larger than 2, a multiple of 2, and comprised of only the digits 0 and/or 2. Example 2: Input: k = 3, digit1 = 4, digit2 = 2 Output: 24 Explanation: 24 is the first integer larger than 3, a multiple of 3, and comprised of only the digits 4 and/or 2. Example 3: Input: k = 2, digit1 = 0, digit2 = 0 Output: -1 Explanation: No integer meets the requirements so return -1.   Constraints: 1 <= k <= 1000 0 <= digit1 <= 9 0 <= digit2 <= 9 Accepted 173

Pragmatism

Image
Pragmatism means laser focusing on what you're trying to accomplish, and get it done. This problem for example can be solved probably in O(N^3) or O(N^2), but if O(N^4) is easy to implement, and the limits allow it, why not? Cheers, ACC. Count Special Quadruplets - LeetCode

900 LeetCode Problems Solved

Image
Achieving 900th LeetCode problems solved. LeetCoding is a hobby of mine. For this problem, it is a simple Breadth-First-Search (BFS). Helps that no two farmlands are connected. Some calculation necessary to finding the extremities of the farmland. Code is down below, cheers, ACC. Find All Groups of Farmland - LeetCode 1992. Find All Groups of Farmland Medium 57 1 Add to List Share You are given a  0-indexed   m x n  binary matrix  land  where a  0  represents a hectare of forested land and a  1  represents a hectare of farmland. To keep the land organized, there are designated rectangular areas of hectares that consist  entirely  of farmland. These rectangular areas are called  groups . No two groups are adjacent, meaning farmland in one group is  not  four-directionally adjacent to another farmland in a different group. land  can be represented by a coordinate system where the top left corner of  land  is  (0, 0)  and the bottom right corner of  land  is  (m-1, n-1) . Find the coordin