300:最长递增子序列
This commit is contained in:
parent
228b2409a8
commit
187ecf20bd
@ -0,0 +1,79 @@
|
|||||||
|
//给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
|
||||||
|
//
|
||||||
|
// 子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序
|
||||||
|
//列。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [10,9,2,5,3,7,101,18]
|
||||||
|
//输出:4
|
||||||
|
//解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [0,1,0,3,2,3]
|
||||||
|
//输出:4
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [7,7,7,7,7,7,7]
|
||||||
|
//输出:1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 2500
|
||||||
|
// -104 <= nums[i] <= 104
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 你可以设计时间复杂度为 O(n2) 的解决方案吗?
|
||||||
|
// 你能将算法的时间复杂度降低到 O(n log(n)) 吗?
|
||||||
|
//
|
||||||
|
// Related Topics 数组 二分查找 动态规划
|
||||||
|
// 👍 1781 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//300:最长递增子序列
|
||||||
|
class LongestIncreasingSubsequence {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new LongestIncreasingSubsequence().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int lengthOfLIS(int[] nums) {
|
||||||
|
int[] dp = new int[nums.length];
|
||||||
|
dp[0] = 1;
|
||||||
|
int max = 1;
|
||||||
|
for (int i = 1; i < nums.length; i++) {
|
||||||
|
dp[i] = 1;
|
||||||
|
for (int j = 0; j < nums.length; j++) {
|
||||||
|
if (nums[i] > nums[j]) {
|
||||||
|
dp[i] = Math.max(dp[i], dp[j] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
max = Math.max(max, dp[i]);
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
<p>给你一个整数数组 <code>nums</code> ,找到其中最长严格递增子序列的长度。</p>
|
||||||
|
|
||||||
|
<p>子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,<code>[3,6,2,7]</code> 是数组 <code>[0,3,1,6,2,2,7]</code> 的子序列。</p>
|
||||||
|
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [10,9,2,5,3,7,101,18]
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
<strong>解释:</strong>最长递增子序列是 [2,3,7,101],因此长度为 4 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [0,1,0,3,2,3]
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [7,7,7,7,7,7,7]
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 2500</code></li>
|
||||||
|
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><b>进阶:</b></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>你可以设计时间复杂度为 <code>O(n<sup>2</sup>)</code> 的解决方案吗?</li>
|
||||||
|
<li>你能将算法的时间复杂度降低到 <code>O(n log(n))</code> 吗?</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li><li>动态规划</li></div></div>\n<div><li>👍 1781</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user