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 < 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 < 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 < grid.length; i++){
	for (int j = 0; j < 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);
}

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

 

Sherlock and Anagrams

Sherlock and Anagrams

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the sherlockAndAnagrams function below.
    static int sherlockAndAnagrams(String s) {
        ArrayList subStrings = new ArrayList();
        for(int i = 1; i <= s.length(); i++){
            if(( i%s.length()) == i){
                subStrings.addAll(generateSubstring(s,i));
            }
        }

        int palindroms=0;
        for (int i = 0; i < subStrings.size(); i++){
            for (int j = i+1; j < subStrings.size(); j++){
                if (isPalindrom(subStrings.get(i),subStrings.get(j)))
                palindroms++;
            }
        }
    return palindroms;
    }
    static boolean isPalindrom(String a, String b){
        if(a.length() != b.length()) return false;
        char [] ac = a.toCharArray();
        char [] bc = b.toCharArray();
        Arrays.sort(ac);
        Arrays.sort(bc);
        for(int i = 0; i < ac.length; i++){
            if(ac[i] != bc[i]) return false;
        }
        return true;
    }
    static List generateSubstring (String a, int b){
        List out = new ArrayList();
        for (int i = 0; i < a.length() - b +1; i++){
                out.add(a.substring(i,i+b));
        }
        return out;
    }
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int q = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int qItr = 0; qItr < q; qItr++) {
            String s = scanner.nextLine();

            int result = sherlockAndAnagrams(s);

            bufferedWriter.write(String.valueOf(result));
            bufferedWriter.newLine();
        }

        bufferedWriter.close();

        scanner.close();
    }
}

Hash Tables: Ransom Note

Tables: Ransom Note

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the checkMagazine function below.
    static void checkMagazine(String[] magazine, String[] note) {
        HashMap magazineMap = new HashMap();
        for(int i = 0;  i < magazine.length; i++){
            if (magazineMap.get(magazine[i]) == null)
            magazineMap.put(magazine[i],1);
            else magazineMap.put(magazine[i], magazineMap.get(magazine[i])+1);
        }
        for (int i = 0; i < note.length; i++){
            if(magazineMap.get(note[i]) == null || magazineMap.get(note[i]) == 0 ) {
               System.out.println("No"); 
               return;
            }
            else magazineMap.put(note[i], magazineMap.get(note[i])-1);
        }
        System.out.println("Yes");
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] mn = scanner.nextLine().split(" ");

        int m = Integer.parseInt(mn[0]);

        int n = Integer.parseInt(mn[1]);

        String[] magazine = new String[m];

        String[] magazineItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < m; i++) {
            String magazineItem = magazineItems[i];
            magazine[i] = magazineItem;
        }

        String[] note = new String[n];

        String[] noteItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            String noteItem = noteItems[i];
            note[i] = noteItem;
        }

        checkMagazine(magazine, note);

        scanner.close();
    }
}