1009. Complement of Base 10 Integer

https://leetcode.com/problems/complement-of-base-10-integer/

class Solution {
    public int bitwiseComplement(int N) {
    String x = Integer.toBinaryString(N);
        StringBuilder out = new StringBuilder();
        for (int i = 0; i <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">&#65279;</span>&lt; x.length(); i++){
            if(x.charAt(i) == &#039;0&#039;)
                out.append(&quot;1&quot;);
            else out.append(&quot;0&quot;);
        }
        return Integer.parseInt(out.toString(), 2);
}}

Better Solution

230. Kth Smallest Element in a BST

230. Kth Smallest Element in a BST

import java.util.PriorityQueue;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
 		static PriorityQueue p;

	public int kthSmallest(TreeNode root, int k) {
        p = new PriorityQueue();
		traverse(root);
		while (k !=1) {
			p.remove();
			k--;
		}
return p.peek();
	}

	private void traverse(TreeNode root) {
		if (root == null)
			return;
		traverse(root.right);
		if(root != null)p.add(root.val);
		 traverse(root.left);
	}
}

329. Longest Increasing Path in a Matrix

329. Longest Increasing Path in a Matrix

 

class Solution {
	public int longestIncreasingPath(int[][] matrix) {
         if (matrix.length == 0) return 0;
		int max = Integer.MIN_VALUE;
		int [][] mem = new int[matrix.length][matrix[0].length];
		for (int i = 0; i < matrix.length; i++)
			Arrays.fill(mem[i], 0);
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				max = Math.max(max,helper(i, j, mem,matrix));
			}
		}

		return max;
	}


	public int helper(int x, int y, int[][] mem, int[][] matrix) {
		if (mem[x][y] != 0) {
			return mem[x][y];
		}
		int[] dx = { 0, 1, -1, 0 };
		int[] dy = { 1, 0, 0, -1 };

		for (int i = 0; i = 0 && newX = 0 && newY  matrix[x][y]) {
				mem[x][y] = Math.max(mem[x][y], helper(newX, newY,mem, matrix));
			}
		}
		return ++mem[x][y];
	}
}

1122. Relative Sort Array

1122. Relative Sort Array

class Solution {
    	public int[] relativeSortArray(int[] arr1, int[] arr2) {
		HashMap h = new HashMap();
		for (int i = 0; i < arr2.length; i++) {
			h.put(i, arr2[i]);
		}
		// queue to keep the new list in ascending order
		PriorityQueue rem = new PriorityQueue();
		int existing = 0;
		for (int i = 0; i < arr1.length; i++) {
			if (null != getKeyFromValue(h, arr1[i])) {
				arr1[i] = getKeyFromValue(h, arr1[i]);
				existing++;
			} else {
				rem.add(arr1[i]);
               arr1[i] = Integer.MAX_VALUE;

			}
		}
		Arrays.sort(arr1);
		for (int i = 0; i < existing; i++) {
			arr1[i] = h.get(arr1[i]);
		}
		for (int i = existing; i < arr1.length; i++) {
			arr1[i] = rem.poll();
		}
		return arr1;
	}

	public static Integer getKeyFromValue(HashMap hm, int value) {
		for (Integer o : hm.keySet()) {
			if (((Integer) hm.get(o)).intValue() == value) {
				return o;
			}
		}
		return null;
	}
}

1185. Day of the Week

1185. Day of the Week

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Solution {
	  	public String dayOfTheWeek(int day, int month, int year) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(year + "-" + ((month <= 9) ? ("0" + month) : ("" + month)) + "-" + ((day <= 9) ? ("0" + day) : ("" + day)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int d = date.getDay();
		String dayString = null;
		switch (d) {
		case 1:
			dayString = "Monday";
			break;
		case 2:
			dayString = "Tuesday";
			break;
		case 3:
			dayString = "Wednesday";
			break;
		case 4:
			dayString = "Thursday";
			break;
		case 5:
			dayString = "Friday";
			break;
		case 6:
			dayString = "Saturday";
			break;
		case 0:
			dayString = "Sunday";
			break;
		default:
			dayString = "Invalid day";
			break;
		}

		return dayString;
	}
}