# 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

# 1137. N-th Tribonacci Number

1137. N-th Tribonacci Number

```class Solution {
public int tribonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else if (n == 2)
return 1;
int t0 = 0;
int t1 = 1;
int t2 = 1;
for (int i = 3; i <= n; i++) {
int tmpT1 = t1;
int tmpT2 = t2;
t2 = t0 + t1 + t2;
t1 = tmpT2;
t0 = tmpT1;
}
return t2;
}

}
```

# 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);
traverse(root.left);
}
}
```

# 1089. Duplicate Zeros

1089. Duplicate Zeros

```class Solution {
public void duplicateZeros(int[] arr) {
ArrayList rem = new ArrayList();
for (int i = 0; i < arr.length; i++) {

if (arr[i] == 0) {
arr[i] = rem.get(0);
rem.remove(0);

}else {
arr[i] = rem.get(0);
rem.remove(0);
}
}

}

}
```

# 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 {
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;
}
}
```

# 1160. Find Words That Can Be Formed by Characters

1160. Find Words That Can Be Formed by Characters

```class Solution {
public int countCharacters(String[] words, String chars) {
HashMap freq = new HashMap();
for (int i = 0; i < chars.length(); i++) {
freq.put(chars.charAt(i), freq.getOrDefault(chars.charAt(i), 0) + 1);
}
int count = 0;
for (int i = 0; i < words.length; i++) {
HashMap freq1 = new HashMap(freq);
boolean charsNotFound = false;
for (int j = 0; j  0)
freq1.put(words[i].charAt(j), freq1.get(words[i].charAt(j)) - 1);
else {
charsNotFound = true;
break;
}
}
if (!charsNotFound)
count += words[i].length();
}
return count;
}
}
```

# 1051. Height Checker

1051. Height Checker

```class Solution {
public int heightChecker(int[] heights) {
int[] original = new int[heights.length];
original = heights.clone();
Arrays.sort(heights);
int count = 0;
for (int i = 0; i < heights.length; i++) {
if (heights[i] != original[i])count++;
}
return count;
}
}
```