LeetCode – Shortest Word Distance (Java)

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example, Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”]. Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = “makes”, word2 = “coding”, return 1. Java Solution public int … Read more

LeetCode – Largest BST Subtree (Java)

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Java Solution class Wrapper{ int size; int lower, upper; boolean isBST;   public Wrapper(){ lower = Integer.MAX_VALUE; upper = Integer.MIN_VALUE; isBST = false; size = 0; } } public … Read more

LeetCode – Combination Sum IV (Java)

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Java Solution This problem is similar to Coin Change. It’s a typical dynamic programming problem. public int combinationSum4(int[] nums, int target) { if(nums==null || nums.length==0) return 0;   int[] dp … Read more

LeetCode – Wiggle Subsequence (Java)

Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order. Java Solution The problem is converted to finding the turning point. When nums[i] … Read more

LeetCode – Product of Array Except Self (Java)

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Java Solution 1 public int[] productExceptSelf(int[] nums) { int[] result = … Read more

Implement a Queue using an Array in Java

There following Java code shows how to implement a queue without using any extra data structures in Java. We can implement a queue by using an array. import java.lang.reflect.Array; import java.util.Arrays;   public class Queue<E> {   E[] arr; int head = -1; int tail = -1; int size;   public Queue(Class<E> c, int size) … Read more

LeetCode – Lowest Common Ancestor of a Binary Tree (Java)

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Java Solution 1 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root==null) return null;   if(root==p || root==q) return root;   TreeNode l = lowestCommonAncestor(root.left, p, q); TreeNode r = lowestCommonAncestor(root.right, p, q);   if(l!=null&&r!=null){ return root; … Read more

LeetCode – Lowest Common Ancestor of a Binary Search Tree (Java)

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. Analysis This problem can be solved by using BST property, i.e., left < parent < right for each node. There are 3 cases to handle. Java Solution 1 – Recursive public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, … Read more