- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/longest-increasing-subsequence/description/
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:
1、 TheremaybemorethanoneLIScombination,itisonlynecessaryforyoutoreturnthelength.;
2、 YouralgorithmshouldruninO(n2)complexity.;
Follow up: Could you improve it to O(n log n) time complexity?
求数组的最长递增子序列。即LIS.
这个题是动态规划的经典题目,其实没有那么难,只要明白其中的道理即可。在《计算机考研机试指南》P160中有详细的解答。
核心思想是使用一个数组dp来保存,dp[i]的意义是到该位置为止的最长递增子序列。
最后求所有位置的最大值
,而不是dp的最后元素。
Python代码:
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
dp = [0] * len(nums)
dp[0] = 1
for i in range(1, len(nums)):
tmax = 1
for j in range(0, i):
if nums[i] > nums[j]:
tmax = max(tmax, dp[j] + 1)
dp[i] = tmax
return max(dp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
C++代码如下:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
const int N = nums.size();
if (N == 0) return 0;
// dp[i] means the LIS when the subsequence ends with nums[i]
vector<int> dp(N, 1);
for (int i = 1; i < N; ++i) {
for (int j = i - 1; j >= 0; --j) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
return *max_element(dp.begin(), dp.end());
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
情况 我正在尝试熟悉 Java 中的线程。出于这个原因,我修改了我在一本书中找到的程序列表。所做的事情非常简单: 它创建一个包含 100.000.000 个元素的 boolean[] 数组。 它使用
我正在使用 Zurb Foundation 使用以下代码制作导航栏:
Write a program that reads a sequence of integers and print 'increasing' if each subsequent number i
我有比特币源代码(多线程),我打算添加一些新行。在比特币网络上,数据消息在数据存储在 vector 中的地方交换。我想在 vector 大小增加时执行一些指令: 如何在 C++ 中编写以下模式以便在
We are given a network flow, as well as a max flow in the network. An edge would be called increasin
我想增加 Line2D 的宽度。我找不到任何方法来做到这一点。我是否需要为此实际制作一个小矩形? 最佳答案 您应该使用 setStroke 来设置 Graphics2D 对象的笔画。 http://w
题目地址:https://leetcode.com/problems/increasing-triplet-subsequence/description/ 题目描述: Given an unso
题目地址:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目描述 Given an unsor
题目地址:https://leetcode.com/problems/monotone-increasing-digits/description/ 题目描述: Given a non-negat
我已经从头开始实现逻辑回归,但是当我运行脚本时,算法总是预测错误的标签。我尝试通过将所有 1 切换为 0 来更改训练输出和 test_output,反之亦然,但它总是预测错误的标签。 我还注意到,将“
我正在尝试增加kivyMD 按钮 的宽度 和高度,但它不受支持(size_hint)。 •我应该创建自己的继承自 Button 类的按钮类吗? •考虑到我希望我的应用程序在 Android 上运行,这
ArrayList tempArray = new ArrayList<>(size); 我正在为我的合并排序构建一个 tempArray,它将根据上下文对整数或字符串进行排序。因此类型为 T Arr
我正在尝试创建大约200万条记录的Lucene。索引时间约为9小时。 您能否建议如何提高性能? 最佳答案 我写了一篇关于如何并行化Lucene索引的可怕文章。它确实写得很糟糕,但是您会发现它是here
我需要你的帮助来解决这个问题 这是我的 ulimit -a 的结果在我的 linux 服务器上 core file size (blocks, -c) 0 scheduling
我想了解: 与完全虚拟化或硬件辅助虚拟化(如 virtio_net 或 virtio_blk)相比,virtio 驱动程序如何提高性能? 这些 virtio 驱动程序如何影响 VMEXIT/VMENT
我正在使用 rose2.m 生成许多角度直方图。我希望显示每个箱子中元素数量的比例范围在 0-50 之间,对于所有地 block 以 10 为增量增加,即使特定地 block 上的最大元素数量小于 5
我正在为我使用远程音频单元的 iPhone 构建一个录音应用程序。在对传入缓冲区执行一些音频分析后,我使用以下方法将缓冲区写入磁盘: ExtAudioFileWriteAsync 但是,我遇到的问题是
您好,我正在解析 xml 文件,我正在返回 Objects 类的 ArrayList,但是对于此方法的每次调用,ArrayList 的大小为从 0-8 和 8 增加到 16 和 24...而不是创建一
在一些 Django 测试中,我有循环来测试很多东西。 在最终结果中它显示为: Ran 1 test in 3.456s 我想为每个循环增加该计数器,我该怎么做? 它正在使用 subTest() ,但
我正在努力解决这个指针算术: int x; int *y = &x; ++y; y 增加了多少? 我知道:“&”是引用运算符,可以读作“address of”。“*”是解引用运算符,可以读作“指向的值
我是一名优秀的程序员,十分优秀!