Posts

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

Spiral Matrix II

Image
Given a positive integer   n , generate an   n x n   matrix   filled with elements from   1   to   n 2   in spiral order. Example 1: Input: n = 3 Output: [[1,2,3],[8,9,4],[7,6,5]] Example 2: Input: n = 1 Output: [[1]]   Constraints: 1 <= n <= 20 Solution: class Solution {     public int[][] generateMatrix(int n) {         int[][] matrix = new int[n][n];         int top = 0, bottom = n - 1, left = 0, right = n - 1;         int val = 1;         while (val <= n * n) {             for (int i = left; i <= right; i++)                  matrix[top][i] = val++;              top++;             for (int i = top; i <= bottom; i++)                 matrix[i][right] = val++;             right--;             for (int i = right; i >= left; i--)                 matrix[bottom][i] = val++;              botto

Maximum Depth of Binary Tree

Image
Given the   root   of a binary tree, return   its maximum depth . A binary tree's  maximum depth  is the number of nodes along the longest path from the root node down to the farthest leaf node. Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Example 3: Input: root = [] Output: 0 Example 4: Input: root = [0] Output: 1 Constraints: The number of nodes in the tree is in the range  [0, 10 4 ] . -100 <= Node.val <= 100 Solution 1: /**  * Definition for a binary tree node.  * public class TreeNode {  *     int val;  *     TreeNode left;  *     TreeNode right;  *     TreeNode() {}  *     TreeNode(int val) { this.val = val; }  *     TreeNode(int val, TreeNode left, TreeNode right) {  *         this.val = val;  *         this.left = left;  *         this.right = right;  *     }  * }  */ class Solu