题目:

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. 安装以及破解Navicat

    1.下载Navicat软件安装包 链接:https://pan.baidu.com/s/1RltCPjg1mmpOjC7vxAjQ4g 提取码:v4k8 2.下载好文件打开是这样的,先运行 " ...

  2. 力扣1337(java&python)-矩阵中战斗力最弱的 K 行(简单)

    题目: 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示. 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序. 如果第 i 行的军人数量少于第 ...

  3. 力扣429(java)-构造矩形(简单)

    题目: 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 所以,现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设计的矩 ...

  4. HarmonyOS NEXT应用开发之图片缩放效果实现

    介绍 图片预览在应用开发中是一种常见场景,在诸如QQ.微信.微博等应用中均被广泛使用.本模块基于Image组件实现了简单的图片预览功能. 使用说明: 双指捏合缩放图片大小 双击图片进行图片的大小切换 ...

  5. 技术解读:英特尔 x86 平台上,AI 能力是如何进行演进的?(附PPT)

    ​简介:AI 生态系统是怎样的?其中又有哪些关键技术? AI 计算力的指数增长意味着,为了解决越来越复杂的用例,即使是 1000 倍的计算性能增长也很容易被消耗.因此,需要通过软件生态系统的助力,才能 ...

  6. 深入分析 Flutter 渲染性能

    简介: Flutter 有很多优点,特别是对于开发者来说,跨平台多端支持,丰富的 UI 组件库和交互效果,声明式 UI,React 的更新方式,Hot-reload 提高开发效率等等.虽然它在渲染性能 ...

  7. Hologres揭秘:优化COPY,批量导入性能提升5倍+

    简介: 揭秘Hologres优化COPY的技术原理,实现批量导入性能提升5倍+ Hologres(中文名交互式分析)是阿里云自研的一站式实时数仓,这个云原生系统融合了实时服务和分析大数据的场景,全面兼 ...

  8. WPF 简单实现一个支持删除自身的应用

    我准备写一个逗比的应用,然而我担心被小伙伴看到这个应用的文件从而知道是我写的,于是我就需要实现让应用能自删除的功能.核心实现方法就是调用 cmd 传入命令行,等待几秒之后删除文件 应用程序在运行时,是 ...

  9. 一个完整的可以输出移动端当前省市(地理坐标)的html页面

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 程序员天天 CURD,怎么才能成长,职业发展的思考(2)

    接着上一篇:程序员天天 CURD,怎么才能成长,职业发展思考 上一篇写到了用年限来谈程序员的发展,在 4 - 6 年这个时间段需要做的一些事情,接着写这个时间段的. 第 4.5 年时候,你可能会做一些 ...