原创 Sub Array Sum to 0

Using a hashmap to save the sum and index pair, initialize the map to <0, -1> sum the integers in the array, if we meet

原创 Subarray Sum Closest

Step 1. Calculate the prefix sum for each position in the array. Step 2. Sort the prefix sum array. Step 3. Compare eve

原创 Leetcode 264. Ugly Number II

An approach is to check every number if it is an ugly number, until we meet the nth ugly number. An efficient way is to

原创 Java Arrays.sort() on objects

class Checker implements Comparator<Player>{ @Override public int compare(Player p1, Player p2 ) { if (

原创 Leetcode 85. Maximal Rectangle

/** * Scan row by row. * Calculate the histogram for each row. e.g. for row 2 the histogram is 2, 0, 2, 1, 1 * Calcu

原创 Leetcode 146. LRU Cache

public class LRUCache { // double linked list private class Node { Node prev; Node next;

原创 O(n^1/2) isPrime

/** O(n^0.5) solution * Only need to consider if n can be divided by numbers from 2 to sqrt(n). * If n

原创 Build a min heap for a given a given array

public class Solution { public void heapify(int[] A) { // only heapify the parent nodes from 0 to A.length-

原创 Leetcode 208. Implement Trie (Prefix Tree)

public class Trie { public static class TrieNode { private final int N = 26; private

原创 Leetcode 257. Binary Tree Paths

DFS class Solution: """ @param root: the root of the binary tree @return: all root-to-leaf paths """

原创 Leetcode 165. Compare Version Numbers

class Solution: """ @param version1: the first given number @param version2: the second given number @

原创 Leetcode 191. Number of 1 Bits

""" Using bit operator AND(&) to compare the number with 1, if the last bit of the number is 1 which means we found a

原创 Leetcode 565. Array Nesting

""" max = Max(current_max, max) Use a flag array to mark if current node has been visited before. If it is visited, th

原创 Leetcode 347. Top K Frequent Elements

Data structure used, hash table and min heap. A hashmap to save every element along with its frequency for the input da

原创 Leetcode 414. Third Maximum Number

public class Solution { public int thirdMax(int[] nums) { // Hashset to remove all duplicate elements