Skip to content

Commit fd08d57

Browse files
committed
feat: 20260304 check in
1 parent 2f3c779 commit fd08d57

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

leetcode/2-热题100/15-动态规划/300. 最长递增子序列.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# [300. 最长递增子序列](https://leetcode.cn/problems/longest-increasing-subsequence/description/)
1+
# 300. 最长递增子序列
22

3-
> **日期:** 2024-07-14
4-
> **所用时间:** 5min
3+
> **日期:** 2024-07-14、2026-03-04
4+
> **所用时间:** 5min
5+
> **知识点:** 动态规划、二分查找
56
67
## 1. 动态规划
78

@@ -66,6 +67,8 @@ public:
6667

6768
变量 $res$ 用于记录当前最长递增子序列的长度。每当 $x$ 被添加到 $tails$ 末尾时, $res$ 增加,避免了传统 $O(n^2)$ 的动态规划解法。
6869

70+
复杂度分析:
71+
6972
- 时间复杂度: $O(nlogn)$
7073
- 空间复杂度: $O(n)$
7174

@@ -86,4 +89,21 @@ class Solution:
8689
tails[r] = x
8790
res += 1 if r == res else 0
8891
return res
92+
```
93+
94+
二分查找使用库函数的写法如下:
95+
96+
**Python3**
97+
98+
```python
99+
class Solution:
100+
def lengthOfLIS(self, nums: List[int]) -> int:
101+
f = []
102+
for x in nums:
103+
j = bisect.bisect_left(f, x)
104+
if j == len(f):
105+
f.append(x)
106+
else:
107+
f[j] = x
108+
return len(f)
89109
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 1582. 二进制矩阵中的特殊位置
2+
3+
> **日期**:2026-03-04
4+
> **所用时间**:10min
5+
> **知识点**:模拟、矩阵
6+
7+
## 1. 题目描述
8+
9+
给定一个大小为 `m x n`**二进制矩阵** `mat`(元素仅为 0 或 1)。
10+
11+
若位置 `(i, j)` 满足:**该行中只有 `mat[i][j]` 为 1,且该列中只有 `mat[i][j]` 为 1**,则称该位置为**特殊位置**
12+
13+
请返回矩阵中**特殊位置**的个数。
14+
15+
**示例 1:**
16+
17+
- **输入**:mat = [[1,0,0],[0,0,1],[1,0,0]]
18+
- **输出**:1
19+
- **解释**:只有 (1, 2) 满足:第 1 行只有该位置为 1,第 2 列只有该位置为 1。
20+
21+
**示例 2:**
22+
23+
- **输入**:mat = [[1,0,0],[0,1,0],[0,0,1]]
24+
- **输出**:3
25+
- **解释**:三个对角线位置 (0,0)、(1,1)、(2,2) 均为特殊位置。
26+
27+
**提示:**
28+
29+
- `m == mat.length`
30+
- `n == mat[i].length`
31+
- `1 <= m, n <= 100`
32+
- `mat[i][j]` 为 0 或 1
33+
34+
## 2. 模拟
35+
36+
枚举每一行:若该行只有一个 1(即 `sum(row) == 1`),找到该 1 的列下标 `j`,再检查该列是否也只有一个 1(即该列所有行的和是否为 1)。若行、列都仅有一个 1,则该位置为特殊位置,计数加一。
37+
38+
复杂度分析:
39+
40+
- 时间复杂度:$O(m \times n)$,遍历每行并求行和、列和。
41+
- 空间复杂度:$O(1)$,仅常数额外空间。
42+
43+
**Python3**
44+
45+
```python
46+
class Solution:
47+
def numSpecial(self, mat: List[List[int]]) -> int:
48+
ans = 0
49+
for row in mat:
50+
if sum(row) != 1:
51+
continue
52+
j = row.index(1)
53+
if sum(row[j] for row in mat) == 1:
54+
ans += 1
55+
return ans
56+
```

0 commit comments

Comments
 (0)