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 ...
随机推荐
- JavaSE 手写 Web 服务器(一)
原文地址:JavaSE 手写 Web 服务器(一) 博客地址:http://www.extlight.com 一.背景 某日,在 Java 技术群中看到网友讨论 tomcat 容器相关内容,然后想到自 ...
- 如何把SQLServer数据库从高版本降级到低版本
如何把SQLServer数据库从高版本降级到低版本 编写人:CC阿爸 2015-4-7 近期在给一个客户推行ECM系统时,基本客户的硬件环境,我们为其安装的为SQL2008 64位的数据库系统.在安装 ...
- python set集合 以及 深浅拷贝
set集合 特点: 无序, 不重复, 元素必须可哈希(不可变) 作用: 去重复 本身是可变的数据类型. 有增删改查操作. frozenset()冻结的集合. 不可变的. 可hash的 深浅拷贝() 1 ...
- SSMS安装英文版后无法修改为中文
SSMS的UI语言和所安装的Visual Studio的语言是相关的,你这种情况应该是第一次安装的时候安装了英文版的visual studio isolated shell,在卸载的时候你没有卸载这个 ...
- Python Issue: ValueError unknown locale: UTF-8 on OS X (Spyder)
In your bash_profile you lack of something. add export LANG="en_US.UTF-8" export LC_COLLAT ...
- 局部加权线性回归(Locally weighted linear regression)
首先我们来看一个线性回归的问题,在下面的例子中,我们选取不同维度的特征来对我们的数据进行拟合. 对于上面三个图像做如下解释: 选取一个特征,来拟合数据,可以看出来拟合情况并不是很好,有些数据误差还是比 ...
- 启动Eclipse之后,关闭Maven自动更新
问题描述: 因为架包的修改,所以Maven需要更新,一启动Eclipse之后,自动更新,由于Maven的架包很多download不下来,就一直卡着的样子,很长时间,什么都做不了. 解决办法: Ecli ...
- kali2.0安装及使用笔记(附带vim配置,长期更新)
作者:陈栋权 时间:16/08/19 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利. 如有特别用途,请与我联系邮 ...
- 关于git的reset、checkout、revert
https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting/file-level-operations 最 ...
- 【读书笔记】 DevOps实践 - 驭DevOps之力强化技术栈并优化IT运行
读书小结 DevOps实践 - 驭DevOps之力强化技术栈并优化IT运行 这本书共200页,读完大概三天:(我指的不是fulltime的一天,而是工作时间以外的一天) 本书是参加16年QConf开发 ...