Perfect number Problem

Problem Given an integer n, return true if n is a perfect number, otherwise return false. Definition A perfect number is a natural number that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. The first perfect numbers are 6, 28, 496, 8128 and all of them are in form $$ 2^{n-1}.(2^n - 1)$$ ...

Container With Most Water

Problem You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. ...

Count Complete Tree Nodes Problem

Problem Given the root of a complete binary tree, return the number of the nodes in the tree. Definition and Properties of Complete Binary Tree Complete Binary Tree Examples Example 1: 1 / \ 2 3 / \ / 4 5 6 ...

Longest Common Subsequence LCS 1 - Get Length

Problem Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings. ...

Lowest Common Ancestor of a Binary Tree

Problem Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Definition Lowest Common Ancestor Definition Examples Example 1: graph TD; 6; 6 --- 2; 6 --- 8; 2 --- 0; 2 --- 4; 8 --- 7; 8 --- 9; 4 --- 3; 4 --- 5; style 2 fill:#FF9933 style 8 fill:#FF9933 style 6 fill:#3399FF ...

Pascal’s Triangle 1 - Generate first n rows

Problem Given an integer numRows, return the first numRows of Pascal’s triangle. Examples Example 1: ...

Transpose of a Matrix

Transpose a Matrix Problem Given a 2D integer array matrix, return the transpose of matrix. The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix’s row and column indices. Example Example 1: $$ \begin{bmatrix} \colorbox{red} 1 & \colorbox{red} 2 & \colorbox{red}3 \\ \colorbox{green} 4 & \colorbox{green} 5 & \colorbox{green}6 \\ \colorbox{blue} 7 & \colorbox{blue} 8 & \colorbox{blue}9 \end{bmatrix} \Longrightarrow \begin{bmatrix} \colorbox{red} 1 & \colorbox{green} 4 & \colorbox{blue} 7 \\ \colorbox{red} 2 & \colorbox{green} 5 & \colorbox{blue} 8 \\ \colorbox{red} 3 & \colorbox{green} 6 & \colorbox{blue} 9 \end{bmatrix} $$ ...

Binary Tree Inorder Traversal

Problem Given a binary tree, write a non recursive or iterative algorithm for Inorder traversal. Inorder Traversal First, visit all the nodes in the left subtree Then the root node Visit all the nodes in the right subtree inorder(root->left) display(root->data) inorder(root->right) Example Example 1: 1 \ 2 / 3 ...

Generate Parentheses Problem

Problem Implement an algorithm to print all valid (i.e., properly opened and closed) combinations of n-pairs of parentheses. OR Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Follow up Make sure the returned list of strings are sorted. Examples Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] ...

Inorder Successor in Binary Search Tree

Problem Given a Binary Search tree, find the inorder successor of a node. Definition See the definition here. Examples Example 1: 10 / \ 5 30 / \ 22 35 ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy