2018字节跳动最长递增子序列

版权声明:原创文章,转载需注明转载出处! https://blog.csdn.net/zhoumingsong123/article/details/82082294
//错误解
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int nt=scan.nextInt();
        int t = scan.nextInt();
        int num []=new int[nt*t];
        for(int i=0;i<nt;i++) {
            num[i]=scan.nextInt();
        }

        for(int i=1;i<t;i++) {

            for(int j=0;j<nt;j++)
            {
                num[i*nt+j]=num[j];
            }


        }

        int num2[]=Arrays.copyOf(num,num.length);
        Arrays.sort(num2);
        String strM=String.valueOf(num);//

        String strN=String.valueOf(num2);//

        //String.valueOf并没有支持将int数组转化为字符串。
        //其实其适配的是String.valueof(Object object)方法
        //转化成字符串也不对,因为比如10,对应字符串有两位



        int m=strM.length();
        int n=strN.length();
        int f[][]=new int[m+1][n+1];

        f[0][0]=0;

        for(int i=1;i<=m;i++)
        {
            f[i][0]=0;
        }
        for(int i=1;i<=n;i++)
        {
            f[0][i]=0;
        }

        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(strM.charAt(i-1)==strN.charAt(j-1))
                {
                    f[i][j]=f[i-1][j-1]+1;
                }
                else
                {

                    f[i][j]=Math.max(f[i-1][j],f[i][j-1]);
                }
            }
        }

        System.out.println(f[m][n]);









    }


}

Given an unsorted array of integers, find the length of longest increasing subsequence.

Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Note:

There may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?

class Solution {
    public int lengthOfLIS(int[] num) {

        TreeSet<Integer> treeSet=new TreeSet();

        for(int i=0;i<num.length;i++)
        {
            treeSet.add(num[i]);//注意题目要求的是严格递增
        }

        int num2[]=new int[treeSet.size()];        

        int index=0;
        for(int i:treeSet)
        {
            num2[index]=i;
            index++;
        }

        int m=num.length;
        int n=num2.length;

        int f[][]=new int[m+1][n+1];

        f[0][0]=0;

        for(int i=1;i<=m;i++)
        {
            f[i][0]=0;
        }
        for(int i=1;i<=n;i++)
        {
            f[0][i]=0;
        }

        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(num[i-1]==num2[j-1])
                {
                    f[i][j]=f[i-1][j-1]+1;
                }
                else
                {

                    f[i][j]=Math.max(f[i-1][j],f[i][j-1]);
                }
            }
        }


       return f[m][n];
    }
}

还有nlogn的解法。
本解法的具体操作如下:

开一个栈,依次读取数组元素 x 与栈顶元素 top:
如果 x > top,将 x 入栈;
如果 x < top,则二分查找栈中第一个 大于等于x 的数,并用 x 替换它。
(替换保证栈的长度不变)
遍历结束之后,最长递增序列长度即为栈的大小。

由于使用了二分搜索,故时间复杂度变成了 Θ(nlgn)。

特别注意的是:本方法只能用于求最长递增子序列的长度,千万不要以为栈中的序列就是最长递增子序列:

例一:原序列为1,5,8,3,6,7
栈为1,5,8,此时读到3,用3替换5,得到1,3,8; 再读6,用6替换8,得到1,3,6;再读7,得到最终栈为1,3,6,7。最长递增子序列为长度4。

例二:原序列为1,5,8,3
则最终栈为1,3,8。明显这不是最长递增子序列!
详细请见

猜你喜欢

转载自blog.csdn.net/zhoumingsong123/article/details/82082294