- 浏览: 183452 次
- 性别:
- 来自: 济南
文章分类
最新评论
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(n^2) complexity.
用动态规划解决,时间复杂度为O(n^2)。创建一个动态数组dp[],维护一个变量max来记录最大长度。dp[i]代表到nums[i]元素的最长递增序列,最后返回max。代码如下:
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(n^2) complexity.
用动态规划解决,时间复杂度为O(n^2)。创建一个动态数组dp[],维护一个变量max来记录最大长度。dp[i]代表到nums[i]元素的最长递增序列,最后返回max。代码如下:
public class Solution { public int lengthOfLIS(int[] nums) { if(nums == null || nums.length == 0) return 0; int[] result = new int[nums.length]; Arrays.fill(result, 1); int max = 1; for(int i = 1; i < result.length; i++) { for(int j = i; j > 0; j--) { if(nums[i] > nums[j - 1]) { result[i] = Math.max(result[i], result[j - 1] + 1); } } if(result[i] > max) max = result[i]; } return max; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
LMS Longest Monotonically Increasing Sequence Algorithm
java java_leetcode题解之Longest Increasing Path in a Matrix.java
最长增长子序列(Longest Increasing Subsequence,简称LIS)是序列分析中的一个基本概念,自上世纪以来一直是研究的热点。对于长度为n的序列,可以使用O(n log n)的时间复杂度精确计算其LIS。然而,在亚线性时间下...
### 最长递增子序列(Longest Increasing Subsequence, LIS) #### 题目描述 本题要求在给定的一个无序整数数组 `nums` 中寻找最长递增子序列的长度。递增子序列指的是数组中的一个序列,其中每个元素都严格大于前...
c++ C++_leetcode题解674. Longest Continuous Increasing Subsequence.cpp
c c语言_leetcode题解之0300_longest_increasing_subsequence
最长递增子序列(Longest Increasing Subsequence, LIS)算法是一种经典的动态规划问题,它在计算机科学中有着广泛的应用,特别是在算法竞赛、数据分析以及优化问题等领域。本篇将深入探讨这个算法,结合Java编程...
今天,我们将讨论动态规划在解决LCS(Longest Common Subsequence)和LIS(Longest Increasing Subsequence)问题中的应用。 首先,让我们来看LCS问题。给定两个序列P1和P2,我们想找到它们的最长公共子序列。为了...
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
最长单调递增子序列(Longest Increasing Subsequence, LIS)是计算机科学中一种经典的问题,主要涉及算法设计和分析,特别是在动态规划领域的应用。在这个问题中,我们需要在给定的一个整数序列中找出一个尽可能长...
以下是一个使用Python实现的动态规划算法示例,用于解决最长递增子序列(Longest Increasing Subsequence,简称LIS)问题。 python def longest_increasing_subsequence(nums): n = len(nums) dp = [1] * n # ...
这是动态规划中,求最长公共子序列(Longest common string)的源代码。自己编写执行。程序简单,有注释。
北大POJ2533-Longest Ordered Subsequence【O(nlogn)】
Pku acm 第2533题 Longest Ordered Subsequence 代码,有详细的注释,动态规划
最长公共子序列(Longest Common Subsequence, LCS)问题是计算机科学领域中一个重要的经典问题,它不仅被广泛应用于文本比较、生物信息学中的基因序列比对等领域,而且也是动态规划算法的一个典型应用场景。...
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...
printf("The length of the longest ordered subsequence is: %d\n", longestOrderedSubsequence(nums, numsSize)); return 0; } ``` 在上面的C程序中,我们首先定义了动态规划数组`dp`,然后通过两层循环来填充...
在 Rust 编程语言中,最长递增子序列(Longest Increasing Subsequence,简称 LIS)是一种常见的算法问题,它的目标是找到一个给定序列中,长度最长的严格递增子序列。这种问题在多种场景下都有应用,比如优化决策...
【Longest Common Ancestor (LCA)问题】是图论中的一个重要概念,特别是在树结构的处理中。给定树T中的两个节点u和v,LCA指的是离根节点最远的公共祖先,即同时是u和v祖先的那个节点。解决LCA问题可以转化为求解...
标题“POJ2533-Longest Ordered Subsequence”是指北京大学在线判题系统POJ上的一道编程题目,其核心任务是寻找一个序列中最长的有序子序列。描述中的“解题报告+AC代码”表明这个压缩包包含了对这道问题的解答思路...