Skip to content

Commit 3070808

Browse files
committed
+ problem 673
1 parent 8271742 commit 3070808

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 673. Number of Longest Increasing Subsequence
2+
Given an integer array `nums`, return *the number of longest increasing subsequences*.
3+
4+
**Notice** that the sequence has to be **strictly** increasing.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [1,3,5,4,7]
9+
<strong>Output:</strong> 2
10+
<strong>Explanation:</strong> The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> nums = [2,2,2,2,2]
16+
<strong>Output:</strong> 5
17+
<strong>Explanation:</strong> The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
18+
</pre>
19+
20+
#### Constraints:
21+
* `1 <= nums.length <= 2000`
22+
* <code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code>
23+
24+
## Solutions (Python)
25+
26+
### 1. Solution
27+
```Python
28+
class Solution:
29+
def findNumberOfLIS(self, nums: List[int]) -> int:
30+
dp = [[[10000001, 0], [-1000001, 1]]]
31+
32+
for num in nums:
33+
if dp[-1][-1][0] < num:
34+
dp.append([[1000001, 0]])
35+
36+
i = bisect.bisect_left(dp, num, key=lambda x: x[-1][0])
37+
j = bisect.bisect_left(dp[i - 1][::-1], num, key=lambda x: x[0])
38+
count = dp[i][-1][1] + dp[i - 1][-1][1] - dp[i - 1][-j - 1][1]
39+
dp[i].append([num, count])
40+
41+
return dp[-1][-1][1]
42+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 673. 最长递增子序列的个数
2+
给定一个未排序的整数数组 `nums`*返回最长递增子序列的个数*
3+
4+
**注意** 这个数列必须是 **严格** 递增的。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [1,3,5,4,7]
9+
<strong>输出:</strong> 2
10+
<strong>解释:</strong> 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
11+
</pre>
12+
13+
#### 示例 2:
14+
<pre>
15+
<strong>输入:</strong> nums = [2,2,2,2,2]
16+
<strong>输出:</strong> 5
17+
<strong>解释:</strong> 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
18+
</pre>
19+
20+
#### 提示:
21+
* `1 <= nums.length <= 2000`
22+
* <code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code>
23+
24+
## 题解 (Python)
25+
26+
### 1. 题解
27+
```Python
28+
class Solution:
29+
def findNumberOfLIS(self, nums: List[int]) -> int:
30+
dp = [[[10000001, 0], [-1000001, 1]]]
31+
32+
for num in nums:
33+
if dp[-1][-1][0] < num:
34+
dp.append([[1000001, 0]])
35+
36+
i = bisect.bisect_left(dp, num, key=lambda x: x[-1][0])
37+
j = bisect.bisect_left(dp[i - 1][::-1], num, key=lambda x: x[0])
38+
count = dp[i][-1][1] + dp[i - 1][-1][1] - dp[i - 1][-j - 1][1]
39+
dp[i].append([num, count])
40+
41+
return dp[-1][-1][1]
42+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findNumberOfLIS(self, nums: List[int]) -> int:
3+
dp = [[[10000001, 0], [-1000001, 1]]]
4+
5+
for num in nums:
6+
if dp[-1][-1][0] < num:
7+
dp.append([[1000001, 0]])
8+
9+
i = bisect.bisect_left(dp, num, key=lambda x: x[-1][0])
10+
j = bisect.bisect_left(dp[i - 1][::-1], num, key=lambda x: x[0])
11+
count = dp[i][-1][1] + dp[i - 1][-1][1] - dp[i - 1][-j - 1][1]
12+
dp[i].append([num, count])
13+
14+
return dp[-1][-1][1]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
[670][670l] |[Maximum Swap][670] |![rs]
357357
[671][671l] |[Second Minimum Node In a Binary Tree][671] |![py]
358358
[672][672l] |[Bulb Switcher II][672] |![rs]
359+
[673][673l] |[Number of Longest Increasing Subsequence][673] |![py]
359360
[674][674l] |[Longest Continuous Increasing Subsequence][674] |![rs]
360361
[675][675l] |[Cut Off Trees for Golf Event][675] |![rs]
361362
[680][680l] |[Valid Palindrome II][680] |![py]
@@ -1604,6 +1605,7 @@
16041605
[670]:Problemset/0670-Maximum%20Swap/README.md#670-maximum-swap
16051606
[671]:Problemset/0671-Second%20Minimum%20Node%20In%20a%20Binary%20Tree/README.md#671-second-minimum-node-in-a-binary-tree
16061607
[672]:Problemset/0672-Bulb%20Switcher%20II/README.md#672-bulb-switcher-ii
1608+
[673]:Problemset/0673-Number%20of%20Longest%20Increasing%20Subsequence/README.md#673-number-of-longest-increasing-subsequence
16071609
[674]:Problemset/0674-Longest%20Continuous%20Increasing%20Subsequence/README.md#674-longest-continuous-increasing-subsequence
16081610
[675]:Problemset/0675-Cut%20Off%20Trees%20for%20Golf%20Event/README.md#675-cut-off-trees-for-golf-event
16091611
[680]:Problemset/0680-Valid%20Palindrome%20II/README.md#680-valid-palindrome-ii
@@ -2854,6 +2856,7 @@
28542856
[670l]:https://leetcode.com/problems/maximum-swap/
28552857
[671l]:https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
28562858
[672l]:https://leetcode.com/problems/bulb-switcher-ii/
2859+
[673l]:https://leetcode.com/problems/number-of-longest-increasing-subsequence/
28572860
[674l]:https://leetcode.com/problems/longest-continuous-increasing-subsequence/
28582861
[675l]:https://leetcode.com/problems/cut-off-trees-for-golf-event/
28592862
[677l]:https://leetcode.com/problems/map-sum-pairs/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
[670][670l] |[最大交换][670] |![rs]
357357
[671][671l] |[二叉树中第二小的节点][671] |![py]
358358
[672][672l] |[灯泡开关 Ⅱ][672] |![rs]
359+
[673][673l] |[最长递增子序列的个数][673] |![py]
359360
[674][674l] |[最长连续递增序列][674] |![rs]
360361
[675][675l] |[为高尔夫比赛砍树][675] |![rs]
361362
[680][680l] |[验证回文字符串 Ⅱ][680] |![py]
@@ -1604,6 +1605,7 @@
16041605
[670]:Problemset/0670-Maximum%20Swap/README_CN.md#670-最大交换
16051606
[671]:Problemset/0671-Second%20Minimum%20Node%20In%20a%20Binary%20Tree/README_CN.md#671-二叉树中第二小的节点
16061607
[672]:Problemset/0672-Bulb%20Switcher%20II/README_CN.md#672-灯泡开关-Ⅱ
1608+
[673]:Problemset/0673-Number%20of%20Longest%20Increasing%20Subsequence/README_CN.md#673-最长递增子序列的个数
16071609
[674]:Problemset/0674-Longest%20Continuous%20Increasing%20Subsequence/README_CN.md#674-最长连续递增序列
16081610
[675]:Problemset/0675-Cut%20Off%20Trees%20for%20Golf%20Event/README_CN.md#675-为高尔夫比赛砍树
16091611
[680]:Problemset/0680-Valid%20Palindrome%20II/README_CN.md#680-验证回文字符串-ii
@@ -2854,6 +2856,7 @@
28542856
[670l]:https://leetcode.cn/problems/maximum-swap/
28552857
[671l]:https://leetcode.cn/problems/second-minimum-node-in-a-binary-tree/
28562858
[672l]:https://leetcode.cn/problems/bulb-switcher-ii/
2859+
[673l]:https://leetcode.cn/problems/number-of-longest-increasing-subsequence/
28572860
[674l]:https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
28582861
[675l]:https://leetcode.cn/problems/cut-off-trees-for-golf-event/
28592862
[677l]:https://leetcode.cn/problems/map-sum-pairs/

0 commit comments

Comments
 (0)