Posts

Showing posts with the label Leetcode

Validate Binary Search Tree

Image
Given the   root   of a binary tree,   determine if it is a valid binary search tree (BST) . A  valid BST  is defined as follows: The left subtree of a node contains only nodes with keys  less than  the node's key. The right subtree of a node contains only nodes with keys  greater than  the node's key. Both the left and right subtrees must also be binary search trees. Example 1: Input: root = [2,1,3] Output: true Example 2: Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The root node's value is 5 but its right child's value is 4.   Constraints: The number of nodes in the tree is in the range  [1, 10 4 ] . -2 31  <= Node.val <= 2 31  - 1 Solution: /**  * Definition for a binary tree node.  * public class TreeNode {  *     int val;  *     TreeNode left;  *     TreeNode right;  *     TreeNode() {}  *     TreeNode(int val) { this.val =

Squares of a Sorted Array

Given an integer array  nums  sorted in  non-decreasing  order, return  an array of  the squares of each number  sorted in non-decreasing order . Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1 <= nums.length <=  10 4 -10 4  <= nums[i] <= 10 4 nums  is sorted in  non-decreasing  order. Solution: class Solution {     public int[] sortedSquares(int[] nums) {                  for(int i=0;i<nums.length;i++){             nums[i] = nums[i] * nums[i];         }                  Arrays.sort(nums);                  return nums;              } } Try it on Leetcode Here, in this problem they mentioned that we need to return the output in a  non-decreasing (i.e., i

Smallest Subtree with all the Deepest Nodes

Given the   root   of a binary tree, the depth of each node is   the shortest distance to the root . Return  the smallest subtree  such that it contains  all the deepest nodes  in the original tree. A node is called  the deepest  if it has the largest depth possible among any node in the entire tree. The  subtree  of a node is tree consisting of that node, plus the set of all descendants of that node. Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4] Output: [2,7,4] Example 2: Input: root = [1] Output: [1] Explanation: The root is the deepest node in the tree. Example 3: Input: root = [0,1,3,null,2] Output: [2] Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.   Constraints: The number of nodes in the tree will be in the range  [1, 500] . 0 <= Node.val <= 500 The values of the nodes in the tree a

Valid Mountain Array

Given an array of integers  arr , return   true  if and only if it is a valid mountain array . Recall that arr is a mountain array if and only if: arr.length >= 3 There exists some  i  with  0 < i < arr.length - 1  such that: arr[0] < arr[1] < ... < arr[i - 1] < A[i] arr[i] > arr[i + 1] > ... > arr[arr.length - 1]   Example 1: Input: arr = [2,1] Output: false Example 2: Input: arr = [3,5,5] Output: false Example 3: Input: arr = [0,3,2,1] Output: true   Constraints: 1 <= arr.length <= 10 4 0 <= arr[i] <= 10 4 Solution: class Solution {     public boolean validMountainArray(int[] arr) {         int n = arr.length, i = 0, j = n - 1;         while (i+1  < n && arr[i] < arr[i + 1]) i++;         while (j > 0 && arr[j - 1] >

The kth Factor of n

Given two positive integers   n   and   k . A factor of an integer  n  is defined as an integer  i  where  n % i == 0 . Consider a list of all factors of  n  sorted in  ascending order , return  the  kth  factor  in this list or return  -1  if  n  has less than  k  factors. Example 1: Input: n = 12, k = 3 Output: 3 Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3. Example 2: Input: n = 7, k = 2 Output: 7 Explanation: Factors list is [1, 7], the 2nd factor is 7. Example 3: Input: n = 4, k = 4 Output: -1 Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1. Example 4: Input: n = 1, k = 1 Output: 1 Explanation: Factors list is [1], the 1st factor is 1. Example 5: Input: n = 1000, k = 3 Output: 4 Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000]. Constraints: 1 <= k <= n <= 1000 Solution: class

Longest Mountain in Array

  Let's call any (contiguous) subarray B (of A) a   mountain   if the following properties hold: B.length >= 3 There exists some  0 < i < B.length - 1  such that  B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array  A  of integers, return the length of the longest  mountain .  Return  0  if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in  O(1)  space? Try it on Leetcode   class Solution {     public int longestMountain(int[] A) {         int

132 Pattern

  Given an array of   n   integers   nums , a   132 pattern   is a subsequence of three integers   nums[i] ,   nums[j]   and   nums[k]   such that   i < j < k   and   nums[i] < nums[k] < nums[j] . Return  true  if there is a  132 pattern  in  nums , otherwise, return  false . Follow up:  The  O(n^2)  is trivial, could you come up with the  O(n logn)  or the  O(n)  solution?   Example 1: Input: nums = [1,2,3,4] Output: false Explanation: There is no 132 pattern in the sequence. Example 2: Input: nums = [3,1,4,2] Output: true Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: nums = [-1,3,2,0] Output: true Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].   Constraints: n == nums.length 1 <= n <= 10 4 -10 9  <= nums[i] <= 10 9 Try it on Leetcode class Solution { public boolean fin