Longest Consecutive Sequence

Find Longest Continuous Increasing Subarray

Algorithm

  • Create a variable holding current maximum length and a variable holding current length.
  • If current value minus 1 and then is equals to previous value, increase current length by 1.
  • otherwise, if current length is greater than current maximum length variable, then update this variable, and set the current length 1
  • at last return current maximum length

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

  • Let S[pos] be defined as the smallest integer that ends an increasing sequence of length pos + 1
  • Append current if it is greater than last element
  • replace smallest element that is greater than current if current is smaller or equal to last element

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

  • top to bottom
  • from top to bottom to get local length of consecutive path
  • use return value to pass the global maximum length

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子七咧,更是在濱河造成了極大的恐慌,老刑警劉巖宙攻,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尔店,死亡現(xiàn)場(chǎng)離奇詭異封孙,居然都是意外死亡韵吨,警方通過(guò)查閱死者的電腦和手機(jī)匿垄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)归粉,“玉大人椿疗,你說(shuō)我怎么就攤上這事≌到剑” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵芽狗,是天一觀的道長(zhǎng)绢掰。 經(jīng)常有香客問我,道長(zhǎng)童擎,這世上最難降的妖魔是什么滴劲? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮顾复,結(jié)果婚禮上班挖,老公的妹妹穿的比我還像新娘。我一直安慰自己芯砸,他們只是感情好萧芙,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布给梅。 她就那樣靜靜地躺著,像睡著了一般双揪。 火紅的嫁衣襯著肌膚如雪动羽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天渔期,我揣著相機(jī)與錄音运吓,去河邊找鬼。 笑死疯趟,一個(gè)胖子當(dāng)著我的面吹牛拘哨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播信峻,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼倦青,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了站欺?” 一聲冷哼從身側(cè)響起姨夹,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎矾策,沒想到半個(gè)月后磷账,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贾虽,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年逃糟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓬豁。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绰咽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出地粪,到底是詐尸還是另有隱情取募,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布蟆技,位于F島的核電站玩敏,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏质礼。R本人自食惡果不足惜旺聚,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望眶蕉。 院中可真熱鬧砰粹,春花似錦、人聲如沸造挽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至厘贼,卻和暖如春界酒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嘴秸。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工毁欣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人岳掐。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓凭疮,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親串述。 傳聞我的和親對(duì)象是個(gè)殘疾皇子执解,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容