Skip to content

Instantly share code, notes, and snippets.

View wohhie's full-sized avatar
👨‍💻
Focusing

Jewel Shamim wohhie

👨‍💻
Focusing
View GitHub Profile
@wohhie
wohhie / jump
Last active July 11, 2021 17:13
public boolean canJump(int[] nums) {
int len = nums.length - 1;
int reachableIndex = len;
for (int i = len; i >= 0; --i) {
if (i + nums[i] >= reachableIndex) {
reachableIndex = i;
}
}
private int sum_tree_data(Node node){
int x, y;
if (node != null){
x = sum_tree_data(node.left);
y = sum_tree_data(node.right);
return x + y + node.data;
}
return 0;
}
private int treeHeight(Node node){
int x, y;
if (node != null){
x = treeHeight(node.left);
y = treeHeight(node.right);
return (x > y) ? x + 1 : y + 1;
}
return 0;
}
private int count_who_have_both_children(Node node){
int x, y;
if (node != null){
x = count_who_have_both_children(node.left);
y = count_who_have_both_children(node.right);
if (node.left != null && node.right != null){
return x + y + 1;
}else {
return x + y;
}
private int count(Node node){
int x, y;
if (node != null){
x = count(node.left);
y = count(node.right);
return x + y +1;
}
return 0;
}
private void levelOrder(Node node){
// 1. print the root data
// 2. insert root address in queue
// 3. while queue is not empty
// a. get the address of the queue
// check left child has data
// i. print the left child data
// j. insert data of left child
// else check right child has data
// i. print the right child data
private void iterativeInOrder(Node node){
// we are going to the left most item
if (node == null) return;
// create an empty stack and push root to it.
Stack<Node> stack = new Stack<>();
while (node != null || !stack.isEmpty()){
// 1. inOrder(left_child) // put the data into stack
// 2. Print(node.data)
// 3. inOrder(right_child)
private void iterativePreOrder(Node node) {
if (node == null) return;
// create an empty stack and push root to it
Stack<Node> stack = new Stack<>();
while (node != null || !stack.isEmpty()){
if (node != null){
System.out.print(node.data + " ");
stack.push(node);
node = node.left;
public int findKthLargestNumber(int[] arr, int k) {
Queue<Integer> queue = new PriorityQueue<>((n1, n2) -> n1.compareTo(n2));
for(int n : arr){
queue.add(n);
if (queue.size() > k){
queue.poll();
}
}
return queue.poll();
}
/**
* Calculate two value sum if the list is unsorted
* ==========================================================
* @param arr
* @param target
*/
public List<List<Integer>> sum_Three_HashMap_unSorted(int[] arr, int target){
Set<List<Integer>> result = new HashSet<>();
Set<Integer> dups = new HashSet<>();
Map<Integer, Integer> visited = new HashMap<>();