673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道为什么len[i] == len[j] + 1:因为可以间隔相加。
也不知道为什么是DP:原来小人是间隔着跳的。
[一句话思路]:
长度一个数组、数量一个数组,两个分开算
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 如果出现了新的最长数组,count需要和最大长度一起换
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
count length分开算
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[算法思想:递归/分治/贪心]:贪心
[关键模板化代码]:
count更新或相加:
if (nums[j] < nums[i]) {
if (length[j] + 1 > length[i]) {
length[i] = length[j] + 1;
//renew cnt[i]
count[i] = count[j];
}else if (length[j] + 1 == length[i]) {
count[i] += count[j];
}
}
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
LIS本身
[代码风格] :
class Solution {
public int findNumberOfLIS(int[] nums) {
//cc
if (nums == null || nums.length == 0) return 0; //ini: length[], count[], res
int n = nums.length, res = 0, max_len = 0;
int[] length = new int[n];
int[] count = new int[n]; //for loop: i, nums[j] < nums[i], count j, max_length
for (int i = 0; i < n; i++) {
//; not ,
length[i] = 1; count[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
if (length[j] + 1 > length[i]) {
length[i] = length[j] + 1;
//renew cnt[i]
count[i] = count[j];
}else if (length[j] + 1 == length[i]) {
count[i] += count[j];
}
}
}
if (length[i] > max_len) {
max_len = length[i];
//renew cnt[i]
res = count[i];
}
else if (length[i] == max_len) res += count[i];
} return res;
}
}
673. Number of Longest Increasing Subsequence最长递增子序列的数量的更多相关文章
- [LeetCode] 673. 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] 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 ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...
随机推荐
- xunsearch之php索引维护(四)
1.添加文档 $xs = new XS('njw'); $index = $xs->index; $data = array( 'pid' => 234, // 此字段为主键,必须指定 ' ...
- 轻量级封装DbUtils&Mybatis之一概要
Why 一时兴起,自以为是的对Jdbc访问框架做了一个简单的摸底,近期主要采用Mybatis,之前也有不少采用Dbutils,因此希望能让这两个框架折腾的更好用. DbUtils:非常简单的Jdbc访 ...
- Server Tomcat v9.0 Server at localhost failed to start.
最近老是出现这样的问题,在网上找了很多方法都不行,试着把Tomcat重新配置了一下就好了,事后找到一个博客,试了一下也可以使用
- linux 查看字体
fc-list #字体列表 fc-list :lang=zh #中文字体 fc-match -v "字体名" # 查看字体详情
- 北京师范大学第十六届程序设计竞赛决赛 F 汤圆防漏理论
链接:https://www.nowcoder.com/acm/contest/117/F来源:牛客网 汤圆防漏理论 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...
- Java-Runoob-面向对象:Java 抽象类
ylbtech-Java-Runoob-面向对象:Java 抽象类 1.返回顶部 1. Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...
- SpringBoot中RedisTemplate订阅发布对象
解说 RedisMessageListenerContainer Redis订阅发布的监听容器,你的消息发布.订阅配置都必须在这里面实现 addMessageListener(MessageListe ...
- .NET System.Web.HttpContext.Current.Request报索引超出数组界限。
移动端使用Dio发送 FormData, 请求类型 multipart/form-data, FormData内可以一个或多个包含文件时. 请求接口时获取上传的fomdata数据使用 System.W ...
- JavaScript 数组some()和filter()
some方法 array1.some(callbackfn[, thisArg]) 对数组array1中的每个元素调用回调函数callbackfn,当回调函数返回true或者遍历完所有数组后,so ...
- 文档主题生成模型(LDA)
一.问题描述 1.1文本建模相关 统计文本建模的目的其实很简单:就是估算一组参数,这组参数使得整个语料库出现的概率最大.这是很简单的极大似然的思想了,就是认为观测到的样本的概率是最大的.建模的目标也是 ...