Posts

Showing posts from July, 2019

Alphabet Board Path - 407th LeetCode Solved Problem

Image
This is my 407th solved leetcode problem. Here it is:  https://leetcode.com/problems/alphabet-board-path/ On an alphabet board, we start at position  (0, 0) , corresponding to character  board[0][0] . Here,  board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"] , as shown in the diagram below. We may make the following moves: 'U'  moves our position up one row, if the position exists on the board; 'D'  moves our position down one row, if the position exists on the board; 'L'  moves our position left one column, if the position exists on the board; 'R'  moves our position right one column, if the position exists on the board; '!'  adds the character  board[r][c]  at our current position  (r, c)  to the answer. (Here, the only positions that exist on the board are positions with letters on them.) Return a sequence of moves that makes our answer equal to  target  in...

Smallest Subtree with all the Deepest Nodes

Image
This was an interesting problem that required three very clear steps to solve it in quadratic time. Here is the problem:  https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ Given a binary tree rooted at  root , the  depth  of each node is the shortest distance to the root. A node is  deepest  if it has the largest depth possible among any node in the  entire tree . The subtree of a node is that node, plus the set of all descendants of that node. Return the node with the largest depth such that it contains all the deepest nodes in its subtree. Example 1: Input: [3,5,1,6,2,0,8,null,null,7,4] Output: [2,7,4] Explanation: We return the node with value 2, colored in yellow in the diagram. The nodes colored in blue are the deepest nodes of the tree. The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree. The output "[2, 7, 4]" is a serialization of the subtree rooted at the nod...

Lesson in string parsing and Boolean expressions

Interesting problem by LeetCode:  https://leetcode.com/problems/parsing-a-boolean-expression/ Return the result of evaluating a given boolean  expression , represented as a string. An expression can either be: "t" , evaluating to  True ; "f" , evaluating to  False ; "!(expr)" , evaluating to the logical NOT of the inner expression  expr ; "&(expr1,expr2,...)" , evaluating to the logical AND of 2 or more inner expressions  expr1, expr2, ... ; "|(expr1,expr2,...)" , evaluating to the logical OR of 2 or more inner expressions  expr1, expr2, ... Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false Constraints: 1 <= expression.length <= 20000 expression[i]  consists of character...