LeetCode – Closest Binary Search Tree Value (Java)

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Java Solution 1 – Recursion Recursively traverse down the root. When target is less than root, go left; when target is greater than root, go right. public class Solution { int goal; double … Read more

LeetCode – Flatten Nested List Iterator (Java)

Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list — whose elements may also be integers or other lists. For example, given the list [[1,1],2,[1,1]], by calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. … Read more

LeetCode – Top K Frequent Elements (Java)

Given an array of integers, write a method to return the k most frequent elements. Java Solution 1 – Heap Time complexity is O(n*log(k)). Note that heap is often used to reduce time complexity from n*log(n) (see solution 3) to n*log(k). public List<Integer> topKFrequent(int[] nums, int k) { //count the frequency for each element HashMap<Integer, … Read more

LeetCode – Group Shifted Strings (Java)

Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence: “abc” -> “bcd” -> … -> “xyz”. Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence, return: … Read more

LeetCode – Longest Increasing Path in a Matrix (Java)

Given an integer matrix, get the length of the longest increasing path. https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Java Solution 1 – Naive DFS public int longestIncreasingPath(int[][] matrix) { int[] max = new int[1]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { dfs(matrix, i, j, max, 1); } … Read more

LeetCode – Moving Average from Data Stream (Java)

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Java Solution This problem is solved by using a queue. class MovingAverage { double sum; int size; LinkedList<Integer> list;   /** Initialize your data structure here. */ public MovingAverage(int size) { this.list = new LinkedList<>(); … Read more

LeetCode – Nim Game (Java)

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. Analysis Java … Read more

LeetCode – Binary Tree Vertical Order Traversal (Java)

Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column). Java Solution 1 For each node, its left child’s degree is -1 and is right child’s degree is +1. We can do a level order traversal and save the degree information. public List<List<Integer>> verticalOrder(TreeNode … Read more