博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
300. Longest Increasing Subsequence
阅读量:6606 次
发布时间:2019-06-24

本文共 1161 字,大约阅读时间需要 3 分钟。

题目:

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 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?

解答:

public class Solution {    //State: f[i] is from 0 - i the longest length of increasing subsequence    //Function: f[i] = Max(f[i - k] + 1) if nums[i] > nums[k]    //Initialize: f[i] = 1;    //Result: f[nums.length - 1];    public int lengthOfLIS(int[] nums) {        if (nums == null || nums.length == 0) {            return 0;        }                int[] f = new int[nums.length];        Arrays.fill(f, 1);        int max = 1;        for (int i = 1; i < nums.length; i++) {            for (int k = 0; k < i; k++) {                if (nums[i] > nums[k]) {                    f[i] = Math.max(f[i], f[k] + 1);                }            }            max = Math.max(max, f[i]);        }                return max;    }}

转载地址:http://qdbso.baihongyu.com/

你可能感兴趣的文章
MySQLs数据库建外键时自动跑到缩影处,真奇怪
查看>>
static关键字
查看>>
js 合并多个对象 Object.assign
查看>>
Java 反射机制
查看>>
Unity 碰撞检测中碰撞器与触发器的区别
查看>>
Elasticsearch配置文件说明
查看>>
初识java
查看>>
temporary Object and destructor
查看>>
xcode - 移动手势
查看>>
细说浏览器特性检测(1)-jQuery1.4添加部分
查看>>
古中国数学家的计算力真是惊人
查看>>
Java基础-算术运算符(Arithmetic Operators)
查看>>
C#编程(四十七)----------集合接口和类型
查看>>
【转】关于大型网站技术演进的思考(十二)--网站静态化处理—缓存(4)
查看>>
积跬步,聚小流------Bootstrap学习记录(1)
查看>>
HDUPhysical Examination(贪心)
查看>>
HTML5 FileAPI
查看>>
使用tdcss.js轻松制作自己的style guide
查看>>
SecureCRTPortable.exe 如何上传文件
查看>>
C++中public、protected及private用法
查看>>