Interview Questions

Longest Consecutive Sequence

2016-03-24  本文已影响56人  宋翰要长肉

Find Longest Continuous Increasing Subarray

Algorithm

Code

public int findLongestSubarray(int[] nums) {
    int maxLength = 0;
    int length = 0;
    for (int i = 0; i < nums.length; i++) {
        if (i == 0 || nums[i] == nums[i - 1] + 1) {
            length++;
        } else {
            if (length > maxLength) {                maxLength = length;
            }
            length = 1;
        }
    }
    if (length > maxLength) {
        maxLength = length;
    }
    return maxLength;
}

Find Longest Continuous Increasing Subsequence

Algorithm

Code

public int lengthOfLIS(int[] nums) {
    if (nums == null || nums.length == 0) {
        return 0;
    }
    // tailTable[i] is defined as index of smallest integer that ends an increasing sequence of length i + 1.
    int[] tailTable = new int[nums.length];
    // parent[i] is defined as index of predecessor of element with index i
    int[] parent = new int[nums.length];
    Arrays.fill(parent, -1);
    int len = 1;
    for (int i = 1; i < nums.length; i++) {
        if (nums[i] > nums[tailTable[len - 1]]) {
        // if current integer > last element in LIS, append this integer to the end of LIS
            tailTable[len] = i;
            parent[i] = tailTable[len - 1]; // current integer's predecessor is last element of previous LIS
            len++; // increase length of LIS
        } else {
        // otherwise, find index of smallest integer in LIS, the integer >= current integer    
            int index = ceilIndex(tailTable, nums, len - 1, nums[i]);
            parent[i] = parent[tailTable[index]]; // current integer's predecessor is predecessor of replaced integer
            tailTable[index] = i; // replace
        }
    }

    int index = tailTable[len - 1];
    // print LIS
    Stack<Integer> stack = new Stack<Integer>();
    while (index != -1) {
        stack.push(index);
        index = parent[index];
    }
    while (!stack.isEmpty()) {
        System.out.print(nums[stack.pop()] + " ");
    }
    System.out.println();
    return len;
}

/**
 * find index of smallest integer in tailTable, the integer >= target
 */
private int ceilIndex(int[] tailTable, int[] nums, int end, int target) {
    int start = 0;
    while (start + 1 < end) {
        int mid = start + (end - start) / 2;
        if (nums[tailTable[mid]] >= target) {
            end = mid;
        } else {
            start = mid;
        }
    }
    if (target <= nums[tailTable[start]]) {
        return start;
    } else {
        return end;
    }
}

298. Binary Tree Longest Consecutive Sequence

Algorithm

Code

public int longestConsecutive(TreeNode root) {
    if (root == null) {
        return 0;
    }
    return helper(root, 0, root.val - 1);
}

private int helper(TreeNode root, int length, int preVal) {
    if (root == null) {
        return length;
    }
    int newLength = (root.val == preVal + 1)? length + 1: 1;
    int leftLength = helper(root.left, newLength, root.val);
    int rightLength = helper(root.right, newLength, root.val);
    return Math.max(newLength, Math.max(leftLength, rightLength));
}
private int maxLen = 0;
public int longestConsecutive(TreeNode root) {
    helper(root);
    return maxLen;
}

private int helper(TreeNode root) {
    if (root == null) {
        return 0;
    }
    int leftLength = helper(root.left);
    int rightLength = helper(root.right);
    int curLeft = (root.left != null && root.val + 1 == root.left.val)? leftLength + 1: 1;
    int curRight = (root.right != null && root.val + 1 == root.right.val)? rightLength + 1: 1;
    int curLength = Math.max(curLeft, curRight);
    if (curLength > maxLen) {
        maxLen = curLength;
    }
    return curLength;
}

Find Longest Continuous Increasing Subsequence in DAG

上一篇下一篇

猜你喜欢

热点阅读