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的更多相关文章

  1. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  2. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  3. 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  4. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...

  6. LeetCode 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  7. [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  8. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  9. LeetCode Number of Longest Increasing Subsequence

    原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...

随机推荐

  1. Xdex(百度版)脱壳工具基本原理

    [原创]Xdex(百度版)脱壳工具基本原理作 者: sherrydl时 间: 2015-12-13,10:52:45链 接: http://bbs.pediy.com/showthread.php?t ...

  2. vuex深入

    多模块  http://www.php.cn/js-tutorial-385084.html Vuex 模块化+命名空间后, 如何调用其他模块的 state, actions, mutations, ...

  3. .net 敏捷开发框架7.0.3 旗舰版

    联系QQ:1516462411 索取

  4. 从零开始学MySQL(四)

    上节连接:https://www.cnblogs.com/RajXie/p/10880809.html 上节说到,在创建表的同时,需要给出列的定义.列的定义可展开如下: 列名 列的数据类型 列的一些其 ...

  5. Matlab 2019a 下载和安装步骤

    目录 1. 安装包下载 + 多套数学建模视频教程 2. 安装步骤 1. 安装包下载 + 多套数学建模视频教程 参考:https://blog.csdn.net/COCO56/article/detai ...

  6. FPGA异步时钟系统中信号处理之单比特信号

    有些东西当你习以为常而不去深思熟虑的时候,致命的错误就会因此埋下!      FPGA开发中难免会遇到跨时钟域处理的问题,而对于单比特信号,我会不假思索的回答:打两拍不久解决了吗?但是事实时,这佯作的 ...

  7. python面向对象--类的装饰器

    # def deco(obj): # print("=====",obj) # obj.x=1 # return obj # @deco#===> test = deco(t ...

  8. PAT Basic 1026 程序运行时间 (15 分)

    要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() 被调用时所耗费的时间.这个时间单位是 clock ...

  9. The Battle of Chibi(数据结构优化dp,树状数组)

    The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...

  10. 计蒜客 蓝桥模拟 B.素数个数

    用 0,1,2,3⋯70,1,2,3 \cdots 70,1,2,3⋯7 这 888 个数组成的所有整数中,质数有多少个(每个数字必须用到且只能用一次). 提示:以 000 开始的数字是非法数字. 代 ...