Algorithm | Learn for Master
  • Leetcode Group Shifted Strings

    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:

    Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence.

    For example, given: [“abc”, “bcd”, “acef”, “xyz”, “az”, “ba”, “a”, “z”],

    Note: For the return value, each inner list’s elements must follow the lexicographic order.

    A better example:

    [“eqdf”, “qcpr”]

    ((‘q’ –

    [Read More...]
  • leetcode paint fence

    There is a fence with n posts, each post can be painted with one of the k colors.

    You have to paint all the posts such that no more than two adjacent fence posts have the same color.

    Return the total number of ways you can paint the fence.

    n and k are non-negative integers.

    There are many solutions online, but the following is the easiest to understand. Thanks the author!

    w(n) number of ways to paint n posts

    p(n) color of the nth post

    w(n) consists of two cases:

    1.p(n) == p(n –

    [Read More...]
  • Leetcode Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

    For example,
    Given [100, 4, 200, 1, 3, 2],
    The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

    Your algorithm should run in O(n) complexity.


    We can use a hashset to record all the elements in the array. Then for each number in the array, we expand to both left and right and record the length of the expansion. 

    See the following Java code:


    [Read More...]
  • Longest Common Subsequence

    A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. It is a set of characters that appear in left-to-right order, but not necessarily consecutively.

    Longest common subsequence (LCS) of 2 sequences is a subsequence, with maximal length, which is common to both the sequences.

    Problem definition of Longest Common Subsequence

    1. Given the first sequence which contains (m) symbols X = (x1, x2, x3, …, xm)
    2. Given the second sequence which contains (n) symbols Y = (y1,
    [Read More...]
  • Leetcode Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S.

    A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).

    Here is an example:
    S = “rabbbit”, T = “rabbit”

    Return 3.

    Java solution

    Here are three solutions to solve this problem.


    [Read More...]
  • LeetCode Serialize and Deserialize Binary Tree (Java)

    Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

    Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

    For example, you may serialize the following tree

    as “[1,2,3,null,null,4,5]”,

    [Read More...]
  • LeetCode Patching Array

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n]inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.

    Example 1:
    nums = [1, 3], n = 6
    Return 1.

    Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
    Now if we add/patch 2 to nums, the combinations are: [1],

    [Read More...]
  • Leetcode Sliding Window Maximum

    Maximum Element Sliding Window

    Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see thek numbers in the window. Each time the sliding window moves right by one position.

    For example,
    Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

    Therefore, return the max sliding window as [3,3,5,5,6,7].

    You may assume k is always valid, ie: 1 ≤ k ≤ input array’s size for non-empty array.

    [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.

    Example 1:
    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].

    Example 2:
    Given the list [1,[4,[6]]],

    By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

    [Read More...]
  • LeetCode Shuffle an Array (Java)

    Shuffle a set of numbers without duplicates.


    Java Solution


    [Read More...]
Page 1 of 712345...Last »