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 ...
随机推荐
- lightoj 1102 - Problem Makes Problem
1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...
- 教学日志:javaSE-面向对象2
一.局部变量和成员变量 package class4.oop1; /** * @Auther: Yu Panpan * @Date: 2021/12/10 - 12 - 10 - 14:47 * @D ...
- Mind the Box: $\ell_1$-APGD for Sparse Adversarial Attacks on Image Classifiers
目录 概 主要内容 Croce F. and Hein M. Mind the box: \(\ell_1\)-APGD for sparse adversarial attacks on image ...
- 日志分析系统 - k8s部署ElasticSearch集群
K8s部署ElasticSearch集群 1.前提准备工作 1.1 创建elastic的命名空间 namespace编排文件如下: elastic.namespace.yaml --- apiVers ...
- 自动化集成:Kubernetes容器引擎详解
前言:该系列文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译.打包.镜像构建.部署等操作:本篇文章主要描述Kubernetes引擎用法. 一.基础简介 Kube ...
- CS基础 float 浮动的使用方法
html代码: <body> <div class='red'> </div> <div class='green'> </div> < ...
- SQL怎么求多列的和?
日常比较常使用的SQL,查询各科的总分,并求出总分大于240的学生名字和总分,如图,要求出linux.Mysql.Java三科的总分,并查处总分大于240的学生姓名和总分 可能你会想到sum,但是su ...
- centOS8安装java14
一.去官网下载相应的linux版本 二.通过xftp把下载下的文件传输到linux下指定目录 三.使用命令 rpm -ivh 安装(tar.gz 使用 tar zxvf 命令) 四.配置环境变量 ...
- access注入 - 联合查询
1.access数据库简介 简介:Microsoft Office Access是由微软发布的关系数据库管理系统.它结合了 MicrosoftJet Database Engine 和 图形用户界面两 ...
- python2.7发送邮件失败之——代码问题
使用python2.7发送邮件,代码如下: from email.header import Headerfrom email.mime.text import MIMETextimport smtp ...