Skip to content

Commit 26926ec

Browse files
committed
Optimize TSP solution by implementing a more efficient algorithm and enhancing performance through improved state management techniques.
1 parent c4716cd commit 26926ec

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: 2025-11-17-LeetCode刷题笔记-1437-是否所有1都至少相隔k个元素
3+
date: 2025-11-17
4+
tags:
5+
- 算法学习
6+
- LeetCode
7+
---
8+
9+
# 题目信息
10+
- 平台:LeetCode
11+
- 题目:1437. 是否所有 1 都至少相隔 k 个元素
12+
- 难度:Easy
13+
- 题目链接:[Check If All 1's Are at Least Length K Places Away](https://leetcode.cn/problems/check-if-all-1s-are-at-least-length-k-places-away/description/?envType=daily-question&envId=2025-11-17)
14+
15+
---
16+
17+
## 题目描述
18+
> 给定一个只包含 0/1 的数组
19+
ums 和整数 k,判断任意两个 1 之间的间隔(指两个 1 的索引差减 1)是否都至少为 k。
20+
21+
---
22+
23+
## 初步思路
24+
1. 从左到右扫描数组,记录上一个 1 出现的位置 last1。
25+
2. 每遇到一个新的 1,检查与 last1 之间的间隔是否小于 k,若是则返回 False。
26+
3. 否则更新 last1,继续扫描,最后返回 rue。
27+
28+
---
29+
30+
## 算法分析
31+
- 核心:一次遍历 + 记录最近的 1。
32+
- 技巧:可以将 last1 初始化为一个足够小的值(例如 -inf 或 -k-1),这样第一次遇到 1 时无需特判。
33+
- 时间复杂度:O(n)
34+
- 空间复杂度:O(1)
35+
36+
---
37+
38+
## 代码实现(Python)
39+
```python
40+
from math import inf
41+
from typing import List
42+
43+
class Solution:
44+
def kLengthApart(self, nums: List[int], k: int) -> bool:
45+
last1 = -inf
46+
for index, num in enumerate(nums):
47+
if num != 1:
48+
continue
49+
if index - last1 - 1 < k:
50+
return False
51+
last1 = index
52+
return True
53+
```
54+
55+
## 测试用例
56+
| 输入 | 输出 | 说明 |
57+
|------|------|------|
58+
| nums = [1,0,0,0,1,0,0,1], k = 2 | false | 后两个 1 间隔不足 2 |
59+
| nums = [1,0,0,0,1,0,0,0], k = 2 | true | 所有 1 间隔均≥2 |
60+
| nums = [0,0,0], k = 1 | true | 无 1,自然满足 |
61+
62+
---
63+
64+
## 总结与反思
65+
1. 典型的双指针问题,一个指针记录上一个 1 出现的位置,一个指针遍历数组。
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: 2025-10-23-leetcode刷题笔记模板
3+
date: 2025-10-23
4+
tags:
5+
- 算法学习
6+
---
7+
8+
# 题目信息
9+
10+
11+
---
12+
13+
## 题目描述
14+
> 用自己的话简述题意、输入输出、约束。
15+
16+
---
17+
18+
## 初步思路
19+
20+
21+
22+
---
23+
24+
## 算法分析
25+
26+
27+
---
28+
29+
---
30+
31+
## 代码实现
32+
```cpp
33+
34+
35+
```
36+
37+
---
38+
39+
40+
---
41+
42+
## 总结与反思
43+
1.
44+
2.
45+
46+
---
47+
48+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: 2025-11-17-LeetCode刷题笔记-1437-是否所有1都至少相隔k个元素
3+
date: 2025-11-17
4+
tags:
5+
- 算法学习
6+
- LeetCode
7+
---
8+
9+
# 题目信息
10+
- 平台:LeetCode
11+
- 题目:1437. 是否所有 1 都至少相隔 k 个元素
12+
- 难度:Easy
13+
- 题目链接:[Check If All 1's Are at Least Length K Places Away](https://leetcode.cn/problems/check-if-all-1s-are-at-least-length-k-places-away/description/?envType=daily-question&envId=2025-11-17)
14+
15+
---
16+
17+
## 题目描述
18+
> 给定一个只包含 0/1 的数组
19+
ums 和整数 k,判断任意两个 1 之间的间隔(指两个 1 的索引差减 1)是否都至少为 k。
20+
21+
---
22+
23+
## 初步思路
24+
1. 从左到右扫描数组,记录上一个 1 出现的位置 last1。
25+
2. 每遇到一个新的 1,检查与 last1 之间的间隔是否小于 k,若是则返回 False。
26+
3. 否则更新 last1,继续扫描,最后返回 rue。
27+
28+
---
29+
30+
## 算法分析
31+
- 核心:一次遍历 + 记录最近的 1。
32+
- 技巧:可以将 last1 初始化为一个足够小的值(例如 -inf 或 -k-1),这样第一次遇到 1 时无需特判。
33+
- 时间复杂度:O(n)
34+
- 空间复杂度:O(1)
35+
36+
---
37+
38+
## 代码实现(Python)
39+
```python
40+
from math import inf
41+
from typing import List
42+
43+
class Solution:
44+
def kLengthApart(self, nums: List[int], k: int) -> bool:
45+
last1 = -inf
46+
for index, num in enumerate(nums):
47+
if num != 1:
48+
continue
49+
if index - last1 - 1 < k:
50+
return False
51+
last1 = index
52+
return True
53+
```
54+
55+
## 测试用例
56+
| 输入 | 输出 | 说明 |
57+
|------|------|------|
58+
| nums = [1,0,0,0,1,0,0,1], k = 2 | false | 后两个 1 间隔不足 2 |
59+
| nums = [1,0,0,0,1,0,0,0], k = 2 | true | 所有 1 间隔均≥2 |
60+
| nums = [0,0,0], k = 1 | true | 无 1,自然满足 |
61+
62+
---
63+
64+
## 总结与反思
65+
1. 典型的双指针问题,一个指针记录上一个 1 出现的位置,一个指针遍历数组。

0 commit comments

Comments
 (0)