题目:

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. 大厂面试题:ReentrantLock 与 synchronized异同点对比

    写在开头 在过去的博文中我们学习了ReentrantLock 与 synchronized这两种Java并发使用频率最高的同步锁,在很多大厂面试题中有个经典考题: ReentrantLock 与 sy ...

  2. 持续定义Saas模式云数据仓库+实时分析

    简介: 从实时分析的价值.场景和数据流程,以及用户对平台能力要求展开,讲述云数据仓库MaxCompute的产品能力优势 ,面对实时分析场景的能力演进要求.进而以实时分析典型场景的全数据流程处理.建模和 ...

  3. 【ClickHouse 技术系列】- ClickHouse 中的嵌套数据结构

    ​简介:本文翻译自 Altinity 针对 ClickHouse 的系列技术文章.面向联机分析处理(OLAP)的开源分析引擎 ClickHouse,因其优良的查询性能,PB级的数据规模,简单的架构,被 ...

  4. 阿里大数据云原生化实践,EMR Spark on ACK 产品介绍

    开源大数据社区 & 阿里云 EMR 系列直播 第六期   主题:EMR spark on ACK 产品演示及最佳实践   讲师:石磊,阿里云 EMR 团队技术专家 内容框架: 云原生化挑战及阿 ...

  5. WPF 优化 EnsureHandle 启动性能

    本文将记录一个在 WPF 应用程序启动过程中的性能优化点.如果一个窗口需要设置 WindowStyle 属性,那么在窗口 EnsureHandle 之前,设置 WindowStyle 属性将会比在 E ...

  6. WPF 框架开发 ColumnDefinition 和 RowDefinition 的代码在哪

    我的 VisualStudio 在更新到 2022 就构建不通过 WPF 仓库,提示我在 Grid 的代码里面找不到 ColumnDefinitionCollection 和 RowDefinitio ...

  7. .Net项目部署到Docker

    .Net项目部署到Docker 环境 linux docker .Net 7 步骤 编写Dockerfile 上传项目文件到linux 运行项目文件到docker 一.设置项目端口 在Program. ...

  8. 四:大数据架构回顾-IOTA架构

    IOTA大数据架构是一种基于AI生态下的全新的数据架构模式,2018年,易观首次提出这一概念.IOTA的整体思路是设定标准数据模型,通过边缘计算技术把所有的计算过程分散在数据产生.计算和查询过程当中, ...

  9. 02 python爬虫-bs4

    目录 步骤 代码 结果 new.csv 图片 步骤 爬取主页面中的文章详情的url和图片地址 下载图片 并请求加文章详情中的页面内容 爬取文章详情中的标题.作者.发布时间 代码 import requ ...

  10. 日常Bug排查-连接突然全部关闭

    日常Bug排查-连接突然全部关闭 前言 日常Bug排查系列都是一些简单Bug的排查.笔者将在这里介绍一些排查Bug的简单技巧,同时顺便积累素材. Bug现场 最近碰到一个问题,一台机器上的连接数在达到 ...