## Quicksort Array in Java

Quicksort is a divide and conquer algorithm. It first divides a large list into two smaller sub-lists and then recursively sort the two sub-lists. If we want to sort an array without any extra space, quicksort is a good option. On average, time complexity is O(n log(n)). The basic step of sorting an array are … Read more

## Top 10 Algorithms for Coding Interview

This post summarizes the common subjects in coding interviews, including 1) String/Array/Matrix, 2) Linked List, 3) Tree, 4) Heap, 5) Graph, 6) Sorting, 7) Dynamic Programming, 8) Bit Manipulation, 9) Combinations and Permutations, and 10) Math. It is not alway easy to put a problem in one category, because the problem may belong to multiple … Read more

## LeetCode Solution – Sort a linked list using insertion sort in Java

Insertion Sort List: Sort a linked list using insertion sort. This is my accepted answer for LeetCode problem – Sort a linked list using insertion sort in Java. It is a complete program. Before coding for that, here is an example of insertion sort from wiki. You can get an idea of what is insertion … Read more

## LeetCode – Sort List (Java)

LeetCode – Sort List: Sort a linked list in O(n log n) time using constant space complexity. Analysis If this problem does not have the constant space limitation, we can easily sort using a sorting method from Java SDK. With the constant space limitation, we need to do some pointer manipulation. Break the list to … Read more

## Iteration vs. Recursion in Java

1. Recursion Consider the factorial function: n!=n*(n-1)*(n-2)*…*1 There are many ways to compute factorials. One way is that n! is equal to n*(n-1)!. Therefore the program can be directly written as: Program 1: int factorial (int n) { if (n == 1) { return 1; } else { return n*factorial(n-1); } }int factorial (int n) … Read more

## LeetCode – K Empty Slots (Java)

Given a garden with N slots. In each slot, there is a flower. Each of the N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will stay blooming since then. Given an array of numbers from 1 to N. Each number in … Read more

## LeetCode – My Calendar II (Java)

Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking. Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start

## LeetCode – Next Closest Time (Java)

Given a time represented in the format “HH:MM”, form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused. You may assume the given input string is always valid. For example, “01:34”, “12:09” are all valid. “1:34”, “12:9” are all invalid. Example 1: … Read more

## LeetCode – Sentence Screen Fitting (Java)

Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen. Note: A word cannot be split into two lines. The order of words in the sentence must remain unchanged. Two consecutive words in a line must … Read more

## LeetCode – Repeated String Match (Java)

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1. For example, with A = “abcd” and B = “cdabcdab”. Return 3, because by repeating A three times (â€œabcdabcdabcdâ€), B is a substring of … Read more

## LeetCode – Minimum Cost to Hire K Workers (Java)

There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group. When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in … Read more

## LeetCode – Permutation in String (Java)

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string. For example: Input: s1 = “ab” s2 = “eidbaooo” Output: True Explanation: s2 contains one permutation of s1 (“ba”). Java … Read more

## Evaluate math expression with plus, minus and parentheses (java)

Given a string of math expression, such as 1-(2+3), evaluate the value. The expression contains only digits, +, – and parentheses. Java Solution import java.util.ArrayList; import java.util.Stack;   public class ExpressionEvaluator { static class Node { Boolean isPositive; Integer value; ArrayList<Node> list;   public Node(boolean isPositive, Integer value) { this.isPositive = isPositive; this.value = value; … Read more

## LeetCode – Backspace String Compare (Java)

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = “ab#c”, T = “ad#c” Output: true Explanation: Both S and T become “ac”. Example 2: Input: S = “a##c”, T = “#a#c” Output: true Explanation: Both … Read more

## Convert BST to Sorted Doubly Linked List (Java)

Let each node’s left child be the next smaller node and each node’s right child be the next bigger node. Node prev = null; Node head = null;   public Node treeToDoublyList(Node root) { if (root == null) { return null; }   helper(root);   head.left = prev; prev.right = head;   return head; } … Read more