题目:

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. axiso封装

    import axios from 'axios';import {Message } from 'element-ui'//element-ui提示框组件import config from './ ...

  2. 关于Kubernetes规划的灵魂n问

    Kubernetes已经成为企业新一代云IT架构的重要基础设施,但是在企业部署和运维Kubernetes集群的过程中,依然充满了复杂性和困扰.阿里云容器服务自从2015年上线后,一路伴随客户和社区的成 ...

  3. 【视频特辑】提效神器!如何用Quick BI高效配置员工的用数权限

    ​简介:随着企业数字化进程逐步加速,企业所产生和积累的数据资源日益增多.每当员工的用数权限发生变动,管理员都需要进行复杂繁琐的重复性配置流程,不仅耗时耗力还容易出错. 如何能便捷地对员工用数权限进行高 ...

  4. 聚焦 | 数据湖分析如何面向对象存储OSS进行优化?

    简介: 最佳实践,以DLA为例子.DLA致力于帮助客户构建低成本.简单易用.弹性的数据平台,比传统Hadoop至少节约50%的成本.其中DLA Meta支持云上15+种数据数据源(OSS.HDFS.D ...

  5. 日志审计携手DDoS防护助力云上安全

    ​简介: 本文主要介绍日志审计结合DDoS防护保障云上业务安全的新实践. 日志审计携手DDoS防护助力云上安全 1 背景介绍 设想一下,此时你正在高速公路上开车去上班,路上还有其他汽车,总体而言,大家 ...

  6. 什么是 objdump 命令

    objdump 是在类 Unix 操作系统上显示关于目标文件的各种信息的命令行程序. 它以一种可阅读的格式让你更多地了解二进制文件可能带有的附加信息. 简单来说,和 IDA 这类软件都可用于反汇编. ...

  7. [FAQ] Error: com.mysql.jdbc.Driver not loaded. :jdbc_driver_library

    以上问题出现在 logstash.conf 未配置好 MySQL 的 JDBC 驱动时导致的错误提示. 首先,下载好 MySQL JDBC 驱动库,可以放到 logstash.conf 所在当前目录或 ...

  8. WPF 已知问题 RadioButton 指定 GroupName 后关闭窗口可能导致无法选中

    本文记录一个 WPF 已知问题,当 WPF 的 RadioButton 指定 GroupName 且将 IsChecked 状态绑定到 ViewModel 上,将包含以上控件的代码的窗口显示两个,接着 ...

  9. WPF 获取全局所有窗口的创建显示事件 监控窗口打开

    本文将告诉大家如何在 WPF 里面进行全局监控任意的窗口创建显示打开,可以获取到每个 WPF 窗口的打开的时机.如此可以用来辅助定位问题和输出日志 这篇博客是有背景的,老司机告诉我说他的应用不响应鼠标 ...

  10. 使用 Kafka Assistant,为您的开发加速

    简要介绍 快速查看所有 Kafka 集群,包括Brokers.Topics和Consumers 支持各种认证模式:PLAINTEXT.SASL_PLAINTEXT.SSL.SASL_SSL 对Kafk ...