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. C#.NET、Power BI、数据挖掘

    阅读目录 1.采集目目标特点与分析 2.方案第一版-Low到爆,别笑话 3.碰壁后的第二版方案 4.最终方案第三版 5.总结 说起采集,其实我是个外行,以前拔过阿里巴巴的客户数据,在我博客的文章:C# ...

  2. C语言scanf的返回值

    #include <stdio.h> int main(void) { long num; long sum = 0L; int status; printf("Please e ...

  3. java中遍历实体类属性和类型,属性值

    public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, I ...

  4. 深度复数网络 Deep Complex Networks

    转自:https://www.jiqizhixin.com/articles/7b1646c4-f9ae-4d5f-aa38-a6e5b42ec475  (如有版权问题,请联系本人) 目前绝大多数深度 ...

  5. 错误:非法字符:“\ufeff”

        导入开源的项目的时候,你可以碰到以上的编码问题,这一般这个项目是用eclipse开发的.主要原因是: Eclipse可以自动把UTF-8+BOM文件转为普通的UTF-8文件,但Android ...

  6. php内置函数分析range()

    PHP_FUNCTION(range) { zval *zlow, *zhigh, *zstep = NULL, tmp; , is_step_double = ; double step = 1.0 ...

  7. “jps”命令的用处?

    jps位于jdk的bin目录下,其作用是显示当前系统的java进程情况,及其id号. jps相当于Solaris进程工具ps.不像”pgrep java” 或”ps -ef grep java”,jp ...

  8. Extjs中如何在一行textfield后面增加文字提示

    添加监听事件: listeners: {               render: function(obj) {                     var font=document.cre ...

  9. 用设计模式来替代if-else

    前言 物流行业中,通常会涉及到EDI报文(XML格式文件)传输和回执接收,每发送一份EDI报文,后续都会收到与之关联的回执(标识该数据在第三方系统中的流转状态).这里枚举几种回执类型:MT1101.M ...

  10. HDU 4300 Clairewd's message ( 拓展KMP )

    题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...