1046. Last Stone Weight

1046. Last Stone Weight

class Solution {
      public int lastStoneWeight(int[] stones) {
    	PriorityQueue queue = new PriorityQueue(new Comparator() {                                                                                      @Override 
    		public int compare(Integer o1, Integer o2) {       
    		return - Integer.compare(o1,o2); } });
    
    	for (int i = 0; i = 2) {
    		int x = queue.poll();
    		int y = queue.poll();
    		if (x == y) continue;
    		if ( x <= y && x != y) queue.offer(y-x);
    		else if( y 0 ?queue.poll():0;
}}

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;
	}
}