题目:

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.

分析:

求出最长递增子序列的个数,我们使用lens[i]表示以nums[i]为结尾的递增子序列中元素的个数也就是长度,以time[i]表示以nums[i]为结尾的递增子序列出现的次数,遍历nums数组,维护这两个数组。

遍历nums[i]这个元素,都要和i前面的元素进行比较(用j进行遍历),如果nums[i]大于nums[j],意味着nums[i]可以拼接到nums[j]后面,产生一个更长的子序列,如果lens[i] < lens[j] +1,更新lens数组lens[i] = lens[j]+1,同时times[i] = times[j]。

如果lens[i]恰好等于lens[j] +1,意味着此时已经有和当前长度相同的子序列了,我们要更新times[i] += times[j],因为以nums[i]为结尾的子序列(长度为lens[i])已经出现过了,我们要加上出现的次数。

最后统计最大长度出现的次数,返回答案即可。

程序:

C++

class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
auto lens = vector<int>(nums.size(), 1);
auto times = vector<int>(nums.size(), 1);
for(int i = 1; i < nums.size(); ++i){
for(int j = 0; j < i; ++j){
if(nums[j] >= nums[i])
continue;
if(lens[j] + 1 > lens[i]){
lens[i] = lens[j] + 1;
times[i] = times[j];
}
else if(lens[j] + 1 == lens[i])
times[i] += times[j];
}
}
int maxLen = 0;
int res = 0;
for(int i = 0; i < lens.size(); ++i){
if(maxLen < lens[i]){
maxLen = lens[i];
res = times[i];
}
else if(lens[i] == maxLen)
res += times[i];
}
return res;
}
};

Java

class Solution {
public int findNumberOfLIS(int[] nums) {
if(nums.length == 0)
return 0;
int[] lens = new int[nums.length];
int[] times = new int[nums.length];
int maxLen = 1;
for(int i = 0; i < nums.length; ++i){
lens[i] = 1;
times[i] = 1;
for(int j = 0; j < i; ++j){
if(nums[i] <= nums[j])
continue;
if(lens[j] + 1 > lens[i]){
lens[i] = lens[j] + 1;
times[i] = times[j];
}
else if(lens[j] + 1 == lens[i]){
times[i] += times[j];
}
}
maxLen = Math.max(maxLen, lens[i]);
}
int res = 0;
for(int i = 0; i < lens.length; ++i){
if(lens[i] == maxLen)
res += times[i];
}
return res;
}
}

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)的更多相关文章

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

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

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

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

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

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

  4. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  5. LeetCode 673. Number of Longest Increasing Subsequence

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

  6. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  8. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  9. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

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

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

随机推荐

  1. ImageJ软件使用教程(三):目标计数

    目录 多点工具法 阀值分割法 二值化 填充分割 自动计数 显示结果 总结 参考资料 本文以钢筋计数为例,讲解一下如何使用ImageJ软件进行计数,这里只介绍两种方法: 多点工具法 阀值分割法 钢筋计数 ...

  2. easyx的使用,图像插入(2.0)

    本文从B站学习,借鉴,一些贴图素材借鉴游戏网图: 视频链接:图像输出_哔哩哔哩_bilibili 想使用图片,先用easyx提供的数据类型定义一个变量: 在对图片进行加载,差不多像是赋值,这个变量名相 ...

  3. 【Oracle】在PL/SQL中使用sql实现插入排序

    [Oracle]在PL/SQL中使用sql实现插入排序 一般来说,SQL要排序的话直接使用order by即可 不一般来说,就是瞎搞,正好也可以巩固自己的数据结构基础,主要也发现没有人用SQL去实现这 ...

  4. 如何基于Dataphin实现敏感数据保护

    ​简介: 在企业的发展过程中,如果不重视敏感数据的保护,和数据安全体系的建设,那么一旦发生了敏感数据泄漏事件,轻则企业口碑受损,业务受影响:重则会直接触法律,受到主管部门的处罚和制裁.本文将以一个最常 ...

  5. Python编程的若干个经典小技巧

    1. 原地交换两个数字 Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例: x,y= 10,20 print(x,y) x,y= y,x print(x,y) # ...

  6. [PHP] 有关PHP浮点数默认显示位数 precision 以及如何调整

    PHP 以浮点数显示的有效位数默认是 14 位.-1 表示将使用一种增强的算法来四舍五入这些数字. 如果想显示更长的浮点位数,可以设置如:ini_set('precision', 40); 有两点需要 ...

  7. WPF 更改 DrawingVisual 的 RenderOpen 用到的对象的内容将持续影响渲染效果

    在 WPF 里面,可以通过 DrawingVisual 来进行使用底层的绘制方法,此方法需要调用 DrawingVisual 的 RenderOpen 拿到 DrawingContext 类型的对象, ...

  8. 从改一个老项目开始的PHP踩坑记

    php所有版本的地址: https://windows.php.net/downloads/releases/archives/ 访问控制器时省略了index.php报No input file sp ...

  9. python3解析FreeSWITCH会议室列表信息

    操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 Python版本:3.9.12 进行FreeSWITCH会议室相关功能开发过程中,会遇到需要解析会议室列表信息并进行 ...

  10. SQL Server实战四:查询数据库的数据

      本文介绍基于Microsoft SQL Server软件,实现数据库表中多种数据查询方法的具体操作. 目录 1 指定列或全部列查询--查询S表学生记录 2 指定列或全部列查询--查询学生姓名与出生 ...