LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)
题目:
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)的更多相关文章
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
随机推荐
- 【NOIP2013模拟联考8】匹配(match) 题解
B 组都说看不懂--我也解释不清啊--只能写这么详细了 其实就是道板题 省流:f[i][s][j]表示字符串长度i,匹配情况s,ac自动机节点j Problem Description 给定k个字符串 ...
- nginx请求头相关漏洞修复(http host&X-XSS-Protection)
nginx请求头相关漏洞修复(http host&X-XSS-Protection) 参考链接:Nginx常见漏洞处理 - 码农教程 (manongjc.com) Web应用漏洞-NGINX各 ...
- Pygame安装以及解决问题:Try to run this command from the system terminal. Make sure that you use the correct version of 'pip......
在这里记录一下我的安装过程: 1.首先找到自己python程序安装目录下的Scripts文件夹(里面有pip这里面): 2.使用快捷键win + R 打开终端,先进入到安装python的盘符,然后进入 ...
- 深入理解C++中的RVO
前言 考虑存在这样一个类如HeavyObject,其拷贝赋值操作比较耗时,通常你在使用函数返回这个类的一个对象时会习惯使用哪一种方式?或者会根据具体场景选择某一种方式? // style 1 Heav ...
- 基于 MaxCompute 的智能推荐解决方案
简介: 在互联网行业红利已过.在获客成本越来越高.在用户在线时长全网基本无增长以及信息大爆炸的情况下,如何更好的转化新用户和提升老用户粘性就变得至关重要,智能化的个性化推荐无疑是经过验证的重要手段之一 ...
- Docker Desktop v20.10.8 和 WSL2 迁移镜像存储目录
只迁移存储镜像和挂载文件目录:https://www.cnblogs.com/lemonK/p/17781775.html 同时迁移docker程序目录:https://www.cnblogs.com ...
- [FAQ] Windows 终端 `git diff` 出现 LF 空格 ^M 符号, 处理方式
可能是终端内的换行配置和 IDE 当中的不一致. 比如 PHPStorm 的: Git 终端使用 git config core.autocrlf 查看是 true 还是 false. 是 tru ...
- MAUI 自定义绘图入门
在2022的5月份,某软正式发布了 MAUI 跨平台 UI 框架.我本来想着趁六一儿童节放假来写几篇关于 MAUI 入门的博客,可惜发现我不擅长写很入门的博客.再加上 MAUI 似乎是为了赶发布日期而 ...
- 2019-4-29-win10-uwp-使用-Border-布局
title author date CreateTime categories win10 uwp 使用 Border 布局 lindexi 2019-04-29 12:29:45 +0800 201 ...
- 使用 Docker 自建一款怀旧游戏之 - 超级马里奥
1)超级马里奥 简介 < 超级马里奥 >(Super Mario)是任天堂公司创造的一款经典游戏系列,是世界上最知名.最成功的游戏之一.这个系列由日本设计师宫本茂于 1985 年创造,最初 ...