LeetCode673
LeetCode每日一题2021.9.20
思路
在最长上升子序列的转移时,维护一个 cnt 数组,表示 以 i 结尾的最长上升子序列个数
f[i] 表示以 i 结尾的最长上升子序列的长度
转移方程为 cnt[i] = (f[i] == f[j] + 1 ? cnt[i] + cnt[j] : cnt[j]);
当f[i] < f[j] + 1的时候, 此时以 i 结尾的最长上升子序列个数显然会被更新,因此直接赋值即可
当f[i] = f[j] + 1的时候, 此时以 i 结尾的最长上升子序列个数需要累加上cnt[j]
AC_CODE
const int N = 2020;
#define n a.size()
class Solution {
public:
int f[N] = {0}, cnt[N] = {0};
int findNumberOfLIS(vector<int>& a) {
int res = 0;
for(int i = 0; i < n; i ++ ) {
f[i] = cnt[i] = 1;
for(int j = 0; j < i; j ++ ) {
if(a[j] < a[i]) {
if(f[i] < f[j] + 1) {
f[i] = f[j] + 1;
cnt[i] = cnt[j];
} else if(f[i] == f[j] + 1) {
cnt[i] += cnt[j];
}
}
}
res = max(res, f[i]);
}
int ans = 0;
for(int i = 0; i < n; i ++ )
ans += (f[i] == res) * cnt[i];
return ans;
}
};
LeetCode673的更多相关文章
- [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- The Monkey King(hdu5201)
The Monkey King Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- Codeforces 931D:Peculiar apple-tree
D. Peculiar apple-tree time limit per test : 1 second memory limit per test : 256 megabytes input : ...
- uniapp使用uni.openDocument打开文件时,安卓打开成功,iOS打开失败【原因:打开的文件的文件名是中文】
解决办法:使用escape进行文件名编码 uni.downloadFile({ url: url, success: function(res) { var filePath = res.tempFi ...
- CSS基础 清除浮动
1.单伪元素清除法:清除浮动 .clearfix::after{ content: '.'; display: block; c ...
- 简单通俗讲解 android 内存泄漏
在柠檬班社区看到老师一篇android 内存泄漏写的通俗易懂,绝对是小白能看懂的! 原文:http://www.lemfix.com/topics/2 平常会听到程序员说"内存泄漏" ...
- linux 之 nginx安装步骤
配置规划 用户 lzh 用户目录 /lzh 下载 进入官网下载nginx http://nginx.org/download/ 安装 解压 cd /lzh/app tar -zxvf nginx-1 ...
- RabbitMQ 消息中间件 的下载与安装【window10】
1.前言 弄了好久,才终于把 rabbitmq装好 ,本来是很简单的,但是,安装有个要求就是路径不能有中文字符, 虽然可以安装,但是无法运行,需要修改路径名为非中文字符后重装rabbitmq才可以运行 ...
- nvm切换node版本出现乱码 exit status 1:
nvm切换nodejs版本出现exit status 1:乱码 跟着网上的教程一步一步做,还是出现问题.浪费一下午的时间 最后发现却因为我没用CMD管理员权限运行 扑街 解决方法: 用管理员身份运行就 ...
- centos7 RPM命令使用
rpm -qa 软件名称 ---查看软件是否安装成功 rpm -ql 软件名称 ---查看软件中都有什么 rpm -qf 文件名称(绝对路径) ---查看属于哪个软件 rpm -ivh 包 ...
- Linux上天之路(九)之文件和文件夹的权限
主要内容 linux 基本权限 linux特殊权限 linux隐藏权限 linux file ACL 权限 1. Linux的基本权限 使用ls -l filename 命令查看文件或文件夹详细权限 ...