Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 673.Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
my solution:
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size(), maxlen = 1, ans = 0;
vector<int> cnt(n, 1), len(n, 1);
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
if (len[j]+1 > len[i]) {
len[i] = len[j]+1;
cnt[i] = cnt[j];
}
else if (len[j]+1 == len[i])
cnt[i] += cnt[j];
}
}
maxlen = max(maxlen, len[i]);
}
// find the longest increasing subsequence of the whole sequence
// sum valid counts
for (int i = 0; i < n; i++)
if (len[i] == maxlen) ans += cnt[i];
return ans;
}
};
Week 12 - 673.Number of Longest Increasing Subsequence的更多相关文章
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
随机推荐
- nodeJS打包安装和问题处理
一,执行步骤,打包报错 1,查看npm版本npm -v 2,查看gulp版本(报错怎么证明没安装)gulp --version 3,安装gulpnpm install --global gulp-cl ...
- 02.AutoMapper 之扁平化(Flattening)
https://www.jianshu.com/p/65099590c930 扁平化(Flattening) 对象映射器的常见用法是将一个复杂对象模型扁平化为一个简单模型.例如您有一个以下复杂模型 ...
- 前端开发HTML&css入门——伪类选择器和一些特殊的选择器
伪类和伪元素 有时候,你需要选择本身没有标签,但是仍然易于识别的网页部位,比如段落首行或鼠标滑过的连接.CSS为他们提供一些选择器:伪类和伪元素. 常用的一些伪类选择器: :link :visited ...
- Pygame播放背景音乐与音效
1.播放背景音乐 pygame.mixer.music.load() 加载MP3格式 加入pygame.mixer.init()即可 第十一行第一个参数:播放次数(n>0),n=0时播放1次,- ...
- Python 通过wmi获取Window服务器硬件信息
通过pip install wmi安装wmi 查看cpu序列号: wmic cpu get processorid 查看主板序列号: wmic baseboard get serialnumber 查 ...
- MIT-线性代数公开课
本博客是学习MIT-线性代数笔记,Gilbert Strang大神讲的通俗易懂,感兴趣的可以观看视频 其中习题集请点击 01)方程组的几何解释 02)矩阵消元 03)乘法和逆矩阵 04)A的LU分解 ...
- 019-openstack组件使用的默认端口号
一.OpenStack组件使用的默认端口号 openstack openstack service default ports port type keystone Identity service ...
- Codeforces1214D. Treasure Island (dp + Hash)
题目链接:传送门 思路: 仔细观察可以发现,答案最多就是2,只要把(2,1)和(1,2)堵住就可以了. 答案是0的情况就是初始状态下,(1,1)就已经不可达(n,m)了,很好判断. 所以重点就是区分答 ...
- libboost_regex 备份用时
libboost_regex-vc100-mt-s-1_57.lib //生成数据
- PHP 图片合成、仿微信群头像
PHP 图片合成.仿微信群头像 参考文章: 作者:凯歌~,php图片合成方法(多张图片合成一张). 经过测试,略作调整和注释,感谢分享. 欢迎提出改善优化意见! 示例代码: /** * 合成图片 * ...