Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 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.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
my solution:
class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size(), maxlen = 1, ans = 0;
vector<int> cnt(n, 1), len(n, 1);
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
if (len[j]+1 > len[i]) {
len[i] = len[j]+1;
cnt[i] = cnt[j];
}
else if (len[j]+1 == len[i])
cnt[i] += cnt[j];
}
}
maxlen = max(maxlen, len[i]);
}
// find the longest increasing subsequence of the whole sequence
// sum valid counts
for (int i = 0; i < n; i++)
if (len[i] == maxlen) ans += cnt[i];
return ans;
}
};
Week 12 - 673.Number of Longest Increasing Subsequence的更多相关文章
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
随机推荐
- Memcached下载安装和使用
一.简介:Memcached 是一个高性能的分布式,基于内存的key-value存储的对象缓存系统(并不是一个数据库),用于动态Web应用以减轻数据库负载. 二.下载和安装1.下载和安装Memcach ...
- 新手使用GIT 上传文件到github
手把手教你如何使用 Git # 初始化一个新的Git仓库 1.方式一: mkdir(make directory) test或者直接进入文件夹中再打开git 方式二:cd /文件夹 cd(change ...
- 20140919-FPGA-有效观察设计中的差分信号
今天回来坐在电脑前,打开Xilinx的Documentation Navigator寻找NCO相关的User Guide,但是在不经意中发现了一个这样的IP,我感觉对于观察设计中的查分信号十分有用.之 ...
- Python自动化学习--控制浏览器
from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://www. ...
- python List 常用方法
list是python常用的数据类型,属于可变的数据类型.用[]表示,里面的元素用','隔开,并且里面的元素类型可以不同,对于每个元素,list都有一个索引一一对应,第一个元素的索引是0,第二个是1, ...
- linear_func
''' class torch.nn.Linear(in_features,out_features,bias = True )[来源] 参数: in_features - 每个输入样本的大小out_ ...
- 基于python3环境下搭建Robot Framework 自动化测试框架(一)
大家都知道,Robot Framework 是基于python2 环境 的一套自动化测试工具,据说python 2 到2020年不维护,现在用python 3 的环境搭建Robot Framework ...
- re模块下的的常用方法
引入模块: import re 1.查找findall 匹配所有,每一项都是列表中的一个元素 ret=re.findall("\d+","sjkhk172按实际花费9 ...
- vue根据路由判断所在的内容
1.所要实现的效果 2.利用路由来判断激活那个tab,更加准确
- Spring5最新完整教程IDEA版【通俗易懂2019.11月】
1.Maven找包: spring-webmvc spring-jdbc 2.Spring的本质是控制反转,依靠依赖注入来实现.以一个servcie对象为例,即是service暴露注入接口(构造,se ...