938. Range Sum of BST

938. Range Sum of BST

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int rangeSumBST(TreeNode root, int L, int R) {
        int out = 0;
        out = rangeSumBSTHelper(root,L,R);
        return out;
    }
    public static int rangeSumBSTHelper(TreeNode root, int L, int R){
        if(root == null) return 0;
        int total = 0;
        if( root.val >= L && root.val <= R)
          total+= root.val;
        
        if (root.right != null)
         total+= rangeSumBSTHelper(root.right,L,R);
         if (root.left != null)   
        total+=rangeSumBSTHelper(root.left,L,R);
        
        return total;
    }
}

872. Leaf-Similar Trees

872. Leaf-Similar Trees

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
      ArrayList tree1 = new ArrayList();
      ArrayList tree2 = new ArrayList(); 
        traverse(root1, tree1);
        traverse(root2, tree2);
        
        if (tree1.size() != tree2.size()) return false;
        for (int i = 0; i < tree1.size(); i++){
            if (tree1.get(i) != tree2.get(i)) return false;
        }
        return true;
    }
public static void traverse (TreeNode root, ArrayList tree){
    
    if( root.right == null && root.left == null){
        tree.add(root.val);
        return;
    }
    if (root.right != null) traverse (root.right, tree);
    if (root.left != null) traverse (root.left, tree);
}    
    
}

807. Max Increase to Keep City Skyline

807. Max Increase to Keep City Skyline

class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
     int[] rowMax = new int[grid.length];
     int[] colMax = new int[grid[0].length];

for (int i = 0; i &lt; grid.length; i++){
	int max = grid[i][0];
	for (int j = 1; j  max ){
			max = grid[i][j];
               }
		}
	rowMax[i] = max;

                                           }

for (int j = 0 ; j &lt; grid[0].length; j++){
	int max = grid[0][j];
	for (int i = 0; i  max ){
 max = grid[i][j];
				    }
 }
	colMax[j] = max;
}
int ans = 0;
for (int i = 0; i &lt; grid.length; i++){
	for (int j = 0; j &lt; grid[i].length; j++){
		int min = Math.min(rowMax[i],colMax[j]);
if(grid[i][j] <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>&lt; min){
    int add = min - grid[i][j];
        ans+= add;
    }
					}
				}
return ans;
}

}

Note:
Write colMax and rowMax in only one loop, instead of two loops

int[] rowMaxes = new int[N];
int[] colMaxes = new int[N];

for (int r = 0; r < N; ++r)
for (int c = 0; c < N; ++c) {
rowMaxes[r] = Math.max(rowMaxes[r], grid[r][c]);
colMaxes[c] = Math.max(colMaxes[c], grid[r][c]);
}

 

String-3 > sumNumbers

String-3 > sumNumbers

public int sumNumbers(String str) {
  return sumNumbersHelper(str,0);
}

public int sumNumbersHelper(String str, int index){
  if (index >= str.length()) return 0;
  
  int currentInt = 0;
  if(Character.isDigit(str.charAt(index))){
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(str.charAt(index));
    index++;
    while(index < str.length() && Character.isDigit(str.charAt(index))){
      stringBuilder.append(str.charAt(index));
      index++;
    }
    currentInt = Integer.parseInt(stringBuilder.toString());
  } 
  return currentInt +sumNumbersHelper(str, index+1);
}

350. Intersection of Two Arrays II

350. Intersection of Two Arrays II

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
       Arrays.sort(nums1);
       Arrays.sort(nums2);
        int i = 0;
        int j = 0;
        ArrayList intersection = new ArrayList();
        while  (i < nums1.length && j < nums2.length){
            if(nums1[i] == nums2[j]){
                intersection.add(nums1[i]);
                i++;
                j++;
            }else if (nums1[i] < nums2[j]){
                i++;
            } else if (nums2[j] < nums1[i]){
                j++;
            }
        }
        int[] out = new int[intersection.size()];
        for(int k = 0; k < intersection.size(); k++)
            out[k] = intersection.get(k);
      return out;  
        }
    
    
    }

654. Maximum Binary Tree

654. Maximum Binary Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return getMaximumNodeHelper(nums,0,nums.length - 1, new TreeNode(0));
    }

    public static TreeNode getMaximumNodeHelper(int[] nums, int start, int end, TreeNode n){
        if(start == end && start  end) return null;

        int maxValue = Integer.MIN_VALUE;
        int maxIndex = 0;

        for (int i = start; i  maxValue){
                maxValue = nums[i];
                maxIndex = i;
            }
        }
         n.val = maxValue;
        ///
        int maxValueRight = Integer.MIN_VALUE;
        int maxIndexRight = maxIndex+1;
        for (int i = maxIndex+1; i  maxValueRight){
                maxValueRight = nums[i];
                maxIndexRight = i;
            }
        }
        n.right = getMaximumNodeHelper(nums,maxIndex+1,end,new TreeNode(maxValueRight));
        ////
        int maxValueLeft = Integer.MIN_VALUE;
        int maxIndexLeft = maxIndex+1;
        for (int i = start; i  maxValueLeft){
                maxValueLeft = nums[i];
                maxIndexLeft = i;
            }
        }
        n.left =  getMaximumNodeHelper(nums,start,maxIndex-1,new TreeNode(maxValueLeft));

        return n;
    }
}

Alternative better Solution