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 ...
随机推荐
- Android 从上层到底层-----app层
CPU:RK3288 系统:Android 5.1 功能:上层 app 控制 led 亮灭 开发板:Firefly RK3288 MainActivity.java package com.aaron ...
- 空间的搜索与R树
在现实地图应用中,有个比较常见的问题,比如,你到了一个地方,想查查附近1km内有什么饭店. 这时地图应用就可以马上查询出周围有什么饭店,如果让你设计,你会怎么设计.假设局限在中国的地图上,共有1000 ...
- scrapy_redis 实现多进程配置部分代码
# 启用Redis调度存储请求队列SCHEDULER = "scrapy_redis.scheduler.Scheduler"# 确保所有的爬虫通过Redis去重DUPEFILTE ...
- centos如何使用utc时间
1.将本地时间文件改名,做备份文件为localtime2 mv /etc/localtime /etc/localtime2 2.将UTC文件和本地文件做连接ln -s /usr/share/zone ...
- 浅谈PHP面向对象编程(四、类常量和静态成员)
4.0 类常量和静态成员 通过上几篇博客我们了解到,类在实例化对象时,该对象中的成员只被当前对象所有.如果希望在类中定义的成员被所有实例共享. 此时可以使用类常量或静态成员来实现,接下来将针对类常量和 ...
- Oracle 10 Recycle Bin回收站
这个功能从10g开始有了. (1)什么是Recycle Bin实际上,Recycle Bin只是一个保存被drop的对象的一个数据字典表.所以,可以通过如下语句查询回收站中的信息:select * f ...
- Android:解决重复打开界面问题
点击界面A按钮,打开界面B,由于startActivity操作是异步执行的,假如在短时间内快速点击按钮,可能会导致打开多个B界面,这个时候可以重写Activity的startActivity事件. p ...
- sql语句中避免使用mysql函数,提升mysql处理能力。
如下sql中不要用mysql内置函数now()等,这样第一可以提高sql执行效率,第二统一程序层处理sql中时间参数,避免因服务器时间差导致问题产生. 使用PDO预处理,第一可以提高sql效率,第二可 ...
- 前端学习---JavaScript
JavaScript基本知识 JavaScript是一门独立的语言,像我们学习php,python等需要安装apache,python3.6,那我们学习JavaScript只需要我们电脑有一个浏览器即 ...
- 爬虫的三种解析方式(正则解析, xpath解析, bs4解析)
一 : 正则解析 : 常用正则回顾: 单字符: . : 除换行符以外的所有字符 [] : [aoe] [a-w] 匹配集合中任意一个字符 \d : 数字 [0-9] \D : 非数字 \w : 非数字 ...