[算法]-Longest Increasing Subsequence

看微软笔试题遇到的。

Longest Increasing Subsequence(LIS) means a sequence containing some elements in another sequence by the same order, and the values of elements keeps increasing.For example, LIS of {2,1,4,2,3,7,4,6} is {1,2,3,4,6}, and its LIS length is 5.Considering an array with N elements , what is the lowest time and space complexity to get the length of LIS?

算法一:动态规划

#include <iostream>
#include <vector>
using namespace std;
#define SIZE 8
int s[SIZE]= {2,1,4,2,3,7,4,6};       // sequence
int length[SIZE];  // 第 x 格的值為 s[0...x] 的 LIS 長度

void LIS()
{
    // 初始化。每一個數字本身就是長度為一的 LIS。
    for (int i=0; i<SIZE; i++) length[i] = 1;

    for (int i=0; i<SIZE; i++)
        // 找出 s[i] 後面能接上哪些數字,
        // 若是可以接,長度就增加。
        for (int j=i+1; j<SIZE; j++)
            if (s[i] < s[j])
                length[j] = max(length[j], length[i] + 1);

    // length[] 之中最大的值即為 LIS 的長度。
    int n = 0;
    for (int i=0; i<SIZE; i++)
        n = max(n, length[i]);
    cout << "LIS的長度是" << n;
}

int main(void)
{
    LIS();
    system("pause");
    return 0;
}

算法二:

#include <iostream>
#include <vector>
using namespace std;
#define SIZE 8
int s[SIZE]= {2,1,4,2,3,7,4,6};       // sequence
int b[SIZE];

// num为要查找的数,k是范围上限
// 二分查找大于num的最小值,并返回其位置
int bSearch(int num, int k)
{
    int low=1, high=k;
    while(low<=high)
    {
        int mid=(low+high)/2;
        if(num>=b[mid])
            low=mid+1;
        else
            high=mid-1;
    }
    return low;
}; 

void LIS()
{
    int low = 1, high = SIZE;
    int k = 1;
    b[1] = s[1];
    for(int i=2; i<=SIZE; ++i)
    {
        if(s[i]>=b[k])
            b[++k] = s[i];
        else
        {
            int pos = bSearch(s[i], k);
            b[pos] = s[i];
        }
    }
    printf("%d", k);
}

int main(void)
{
    LIS();
    system("pause");
    return 0;
}
时间: 2024-09-20 04:12:08

[算法]-Longest Increasing Subsequence的相关文章

[LeetCode] Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] i

LCS (Longest Common Subsequence) 字符串最长公共子串算法

LCS (Longest Common Subsequence) 算法用于找出两个字符串最长公共子串. 算法原理: (1) 将两个字符串分别以行和列组成矩阵.(2) 计算每个节点行列字符是否相同,如相同则为 1.(3) 通过找出值为 1 的最长对角线即可得到最长公共子串. 人 民 共 和 时 代中 0, 0, 0, 0, 0, 0华 0, 0, 0, 0, 0, 0人 1, 0, 0, 0, 0, 0民 0, 1, 0, 0, 0, 0共 0, 0, 1, 0, 0, 0和 0, 0, 0, 1

UVa 10405:Longest Common Subsequence,最长公共子序列模板题

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 [原题] Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of

UVa 10405 Longest Common Subsequence (DP&amp;amp;LCS)

10405 - Longest Common Subsequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 Sequence 1: Sequence 2: Given two sequences of characters, print

POJ 2533 Longest Ordered Subsequence

Description A numeric sequence of ai is ordered if a1 < a2 < - < aN. Let the subsequence of the given numeric sequence (a1, a2, -, aN) be any sequence (ai1, ai2, -, aiK), where 1 <= i1 < i2 < - < iK <= N. For example, sequence (1,

UVA 10405 Longest Common Subsequence:简单DP

省赛还有不到50天了,自己DP这块实在是弱,准备就拿着些天狂刷DP了. http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346 大意: 求两个字符串的最长公共子序列. 思路:水题,不过需要注意的就是字符串里可能会出现空格,需要用gets,真是坑. 更多精彩内容:http://www.bianceng.cnhttp://www.biancen

java算法-Longest Palindromic Substring 最长回文子串问题?JAVA

问题描述 Longest Palindromic Substring 最长回文子串问题?JAVA public class Solution { public String longestPalindrome(String s) { String ret = ""; for (int i = 0; i < s.length(); i++) { for (int j = 0; i - j >= 0 && i + j < s.length(); j++)