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 ...
随机推荐
- '2'>'10'==true? JS是如何进行隐式类型转换的?
前言 '2'>'10'返回的true,可能很多人都不是很能理解吧? 在js中,当运算符在运算时,如果两边数据不统一,CPU就无法计算,这时我们编译器会自动将运算符两边的数据做一个数据类型转换,转 ...
- mybatis 内部定义对象和集合
mapper 配置文件中 引入两个重要的标签:association和collection标签.
- SHARPENING (HIGHPASS) SPATIAL FILTERS
目录 Laplacian UNSHARP MASKING AND HIGHBOOST FILTERING First-Order Derivatives Roberts cross-gradient ...
- jsoncpp转换字符串
Json::Value root; ...//root中写入数据 //方法一:转为格式化字符串,里面加了很多空格及换行符 string strJson1 = root.toStyledString() ...
- [android]打印C++的输出信息在安卓logcat上调试
#include <android/log.h> //宏定义全局函数:C++打印log到android-debug模式下帮助调试(勿删) //调用方式:slogd("test n ...
- markdownpad 2 pro版本(注册码)
简介 markdown – 一种轻量级文本标记语言,当今程序员必备技能markdownpad -- windows平台下好用的markdown编辑器 官网下载地址:http://www.markdow ...
- 编写Java程序,使用 Java 的 I/O 流将 H:\eclipse.zip 文件拷贝至 E 盘下,重新命名为 eclipse 安装 .zip。
查看本章节 查看作业目录 需求说明: 使用 Java 的 I/O 流将 H:\eclipse.zip 文件拷贝至 E 盘下,重新命名为 eclipse 安装 .zip.在拷贝过程中,每隔2000 毫秒 ...
- docker学习:docker安装
Centos7 安装docker 下载安装 yum install docker-ce 启动docker systemctl start docker 创建并编写镜像加速文件 vim /etc/doc ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
- Nginx 全模块安装及匹配方式、反向代理和负载均衡配置
一.安装 OpenResty OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超 ...