Find the Second Largest Number in an Array

Problem Given an integer array, find the second largest number in the array. Solution The optimal time is linear to the length of the array N. We can iterate over the array, whenever the largest elements get updated, its current value becomes the second largest. public static int getSecondLargest(int[] arr){ int first = Integer.MIN_VALUE; int … Read more

LeetCode – Reorder List (Java)

Given a singly linked list L: L0→L1→ … →Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… For example, given {1,2,3,4}, reorder it to {1,4,2,3}. You must do this in-place without altering the nodes’ values. Java Solution Because the problem requires “in-place” operations, we can only change their pointers, not creating a new list. This problem can be solved … Read more

Leetcode – Longest Palindromic Substring (Java)

Finding the longest palindromic substring is a classic problem of coding interview. This post summarizes 3 different solutions for this problem. 1. Dynamic Programming Let s be the input string, i and j are two indices of the string. Define a 2-dimension array “table” and let table[i][j] denote whether a substring from i to j … Read more

Algorithm Problem Classification

An algorithm problem contains 3 parts: input, output and solution/algorithm. The input can be an array, string, matrix, tree, linked list, graph, etc. The algorithm solution can be dynamic programming, binary search, BFS, DFS, or topological sort. The solution can also be a data structure, such as a stack, queue/dequeue, hash set, tree set, hash … Read more

LeetCode – Expressive Words (Java)

Sometimes people repeat letters to represent extra feeling, such as “hello” -> “heeellooo”, “hi” -> “hiiii”. In these strings like “heeellooo”, we have groups of adjacent letters that are all the same: “h”, “eee”, “ll”, “ooo”. For some given string S, a query word is stretchy if it can be made to be equal to … Read more

LeetCode – Find And Replace in String (Java)

To some string S, we will perform some replacement operations that replace groups of letters with new ones (not necessarily the same size). Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original … Read more

LeetCode – Wiggle Sort (Java)

Given an unsorted array nums, reorder it in-place such that nums[0] < nums[1] > nums[2] < nums[3].... Example: Input: nums = [3,5,2,1,6,4] Output: One possible answer is [3,5,1,6,2,4] Java Solution public void wiggleSort(int[] nums) { if (nums == null || nums.length <= 1) { return; }   for (int i = 1; i < nums.length; ... Read more

LeetCode – Missing Ranges (Java)

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99, Output: [“2”, “4->49”, “51->74”, “76->99”] Java Solution public List<String> findMissingRanges(int[] nums, int lower, int upper) { List<String> … Read more

LeetCode – Partition Labels (Java)

A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts. For example: Input: S = “ababfeefhijkh” Output: [4,4,5] Explanation: The partition is “abab”, … Read more

LeetCode – LRU Cache (Java)

Design and implement a data structure for Least Recently Used (LRU) cache, which supports get and put. Analysis The key to solve this problem is using a double linked list which enables us to quickly move nodes. The LRU cache is a hash table of keys and double linked nodes. The hash table makes the … Read more

LeetCode – Palindrome Partitioning (Java)

Problem Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = “aab”, Return [ [“aa”,”b”], [“a”,”a”,”b”] ] 1. Depth-first Search public ArrayList<ArrayList<String>> partition(String s) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();   if (s == null || s.length() … Read more