Articles

  • Best Time to Buy and Sell Stock IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions.

    Read More »

  • 123 - Best Time to Buy and Sell Stock III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions.

    Read More »

  • 122 - Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    Read More »

  • 121 - Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

    Read More »

  • Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS.

    Read More »

  • 139 - Word Break

    Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words.

    Read More »

  • 55 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index.

    Read More »

  • 70 - Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

    Read More »

  • 63 - Unique Paths II

    Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid.

    Read More »

  • 62 - Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below). How many possible unique paths are there?

    Read More »

  • 64 - Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

    Read More »

  • Backpack II

    Given n items with size Ai and value Vi, and a backpack with size m. What’s the maximum value can you put into the backpack?

    Read More »

  • Backpack

    Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

    Read More »

  • 120 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

    Read More »

  • 60 - Permutation Sequence

    Given n and k, return the k-th permutation sequence. Reference

    Read More »

  • 40 - Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination.

    Read More »

  • 39 - Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times.

    Read More »

  • 77 - Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 … n.

    Read More »

  • 95 - Unique Binary Search Trees II

    Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.

    Read More »

  • 47 - Permutations II

    Given a list of numbers with duplicate number in it. Find all unique permutations.

    Read More »

  • 46 - Permutations

    Given a list of numbers, return all possible permutations.

    Read More »

  • 90 - Subsets II

    Given a list of numbers that may has duplicate numbers, return all possible subsets.

    Read More »

  • 78 - Subsets

    Given a set of distinct integers, return all possible subsets.

    Read More »

  • 20 - Valid parentheses

    Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

    Read More »

  • Unique Characters

    Implement an algorithm to determine if a string has all unique characters.

    Read More »

  • Singleton

    Singleton is a most widely used design pattern. If a class has and only has one instance at every moment, we call this design as singleton. For example, for class Mouse (not a animal mouse), we should design it in singleton. You job is to implement a getInstance method for given class, return the same instance of this class every time you call this method.

    Read More »

  • 7 - Reverse Integer

    Reverse digits of an integer.

    Read More »

  • 131 - Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

    Read More »

  • 305 - Number of Islands II

    Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y means that you can change the grid matrix[A[i].x][A[i].y] from sea to island. Return how many island are there in the matrix after each operator.

    Read More »

  • 200 - Number of Islands

    Given a boolean 2D matrix, find the number of islands.

    Read More »