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 ...
随机推荐
- Unit02: JDBC核心API
Unit02: JDBC核心API db.properties 注意:如果使用连接池,可以在这个文件中增加对连接池的相关设置: 连接池参数,常用参数有: 初始连接数 最大连接数 最小连接数 每次增加的 ...
- UEFI 启动GPT分区 Win10和Ubuntu16.04双系统安装
测试机器:联想小新锐7000 工具:UltraISO 系统下载 为Ubuntu分配空间 右键“我的电脑”——>“管理”——>“磁盘管理”,(win+x快捷键)选择一个有很大空闲空间的磁盘, ...
- 部署docker
部署和开发环境不一样,我们不需要频繁地进入到容器内部,所以一般我们会将代码和环境打包到一块,部署到服务器上 Clone 代码 将项目代码克隆到本地 git clone git@git.coding.n ...
- Linux系统命令与脚本开发
系统命令 # cat EFO cat >> file << EOF neirong EOF # 清空 >file 清空文件 [root@Poppy conf]# sed ...
- htm标签的语意
标签名 英文全拼 标签语意 div division 分割 span span 范围 ol ordered list 排序列表 ul unordered list 不排序列表 li list item ...
- 【学习笔记】dp基础
知识储备:dp入门. 好了,完成了dp入门,我们可以做一些稍微不是那么裸的题了. dp基础,主要是做题,只有练习才能彻底掌握. 洛谷P1417 烹调方案 分析:由于时间的先后会对结果有影响,所以c[i ...
- OD 实验(二十一) - 对反调试程序的逆向分析(二)
程序: 运行程序 点击“Verify” 关闭该程序,启动 OD 再运行程序 逆向: 用 OD 载入程序 按 F8 往下走 执行完这个 call 指令就弹出了对话框 这个 call 指令调用了 Dial ...
- RabbitMQ_direct
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @version: @author: morgana @licens ...
- RPC通信
@version: @author: morgana @license: Apache Licence @contact: vipmorgana@gmail.com @site: @software: ...
- XSD 学习
1.新建文件 Rhythmk.xsd <?xml version="1.0" encoding="utf-8"?> <xs:schema id ...