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;
}
};

最新文章

  1. 使用vlc进行二次开发做自己的播放器
  2. [MongoDB]MongoDB与JAVA结合使用CRUD
  3. 在使用Intelligencia.UrlRewriter过程中 中文乱码问题
  4. [deviceone开发]-纳豆项目源码开源
  5. 认识SuperSocket 1.6.4
  6. 使用sql语句查询日期在一定时间内的数据
  7. UI2_视图切换ViewController
  8. mongoDB 插入数据 用java实现
  9. SharePoint 2010 产品六大功能模块
  10. 试用ubuntu-12.04.3-desktop-amd64
  11. asp.net 获取IP地理位置的几个主要接口
  12. Mysql8安装与配置
  13. HDOJ 4267 A Simple Problem with Integers (线段树)
  14. 03 爬虫之selenium模块
  15. 001.MySQL高可用主从复制简介
  16. 如何查看Isilon的节点的CPU的信息?
  17. bzoj1055 ||P4290 [HAOI2008]玩具取名
  18. Python基础(3) - 数据类型:5字典类型
  19. 关于TensorFlow的GPU设置
  20. LightOJ 1023 Discovering Permutations 水题

热门文章

  1. linux的管道 |和grep命令以及一些其他命令(diff,echo,cat,date,time,wc,which,whereis,gzip,zcat,unzip,sort)
  2. vue elementui 切换语言
  3. Docker介绍,安装和常用的命令
  4. C#索引器2 字符串作为索引号
  5. Manjaro系统和软件安装记录
  6. 【串线篇】spring boot启动配置原理
  7. linux运维、架构之路-cobbler无人值守
  8. python multiprocessing pool
  9. Element ui 中的表单提交按钮多次点击bug修复
  10. Codeforces 916B Jamie and Binary Sequence ( 模拟 &amp;&amp; 思维 )