Skip to content

Commit d57f0c5

Browse files
committed
Add: Add 2025/11/17
1 parent a4367da commit d57f0c5

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 1437. Check If All 1's Are at Least Length K Places Away
2+
3+
Given an binary array `nums` and an integer `k`, return `true` if all `1`'s are at least `k` places away from each other, otherwise return `false`.
4+
5+
**Constraints:**
6+
7+
- `1 <= nums.length <= 10^5`
8+
- `0 <= k <= nums.length`
9+
- `nums[i]` is `0` or `1`
10+
11+
## 基礎思路
12+
13+
本題要判斷陣列中的所有 `1` 是否彼此至少相距 `k` 個位置。
14+
由於每個 `1` 都會對相對位置產生限制,因此我們只需 **找到所有出現 `1` 的索引,並確認相鄰兩個 `1` 的距離是否 ≥ k+1**
15+
16+
在思考此問題時,我們可以注意:
17+
18+
- 整個陣列只需要單次線性掃描。
19+
- 遇到 `1` 時,只需檢查它與前一次出現 `1` 的距離。
20+
- 若距離不足,立即返回 `false`
21+
- 若掃描完成均未違規,即可返回 `true`
22+
- 因為只需要記錄上一個 `1` 的位置,因此空間為 $O(1)$。
23+
24+
此策略使用簡單線性邏輯,效率穩定,能處理上限為 10⁵ 的陣列。
25+
26+
## 解題步驟
27+
28+
### Step 1:初始化必要變數
29+
30+
記錄陣列長度以避免重複存取,並用變數儲存上一個 `1` 的出現位置;
31+
初始值設定為足夠遠,使第一個 `1` 一定通過距離檢查。
32+
33+
```typescript
34+
// 快取長度以避免迴圈中反覆存取屬性
35+
const length = nums.length;
36+
37+
// 紀錄上一個 '1' 的索引;初始化為足夠遠,使第一個 '1' 自然通過檢查
38+
let previousOneIndex = -k - 1;
39+
```
40+
41+
### Step 2:遍歷陣列,尋找所有 `1` 並檢查距離
42+
43+
逐一掃描陣列:
44+
45+
* 若不是 `1` 則略過
46+
* 若是 `1`,則與上一個 `1` 比較距離
47+
* 距離不足則立即返回 `false`
48+
* 若距離足夠,更新上一個 `1` 的位置
49+
50+
```typescript
51+
for (let index = 0; index < length; index++) {
52+
// 讀取元素以避免重複索引開銷
53+
const value = nums[index];
54+
55+
// 只有遇到 '1' 時才需處理
56+
if (value === 1) {
57+
// 若距離上一個 '1' 過近則立即返回 false
58+
if (index - previousOneIndex <= k) {
59+
return false;
60+
}
61+
62+
// 更新上一個 '1' 的位置
63+
previousOneIndex = index;
64+
}
65+
}
66+
```
67+
68+
### Step 3:若掃描完整個陣列均無違規則返回成功
69+
70+
若迴圈未提前結束,代表所有 `1` 的間距皆符合要求。
71+
72+
```typescript
73+
// 若未出現違規距離,表示所有 '1' 均合法
74+
return true;
75+
```
76+
77+
## 時間複雜度
78+
79+
- 只進行一次長度為 n 的迴圈。
80+
- 每次迭代皆為常數操作。
81+
- 總時間複雜度為 $O(n)$。
82+
83+
> $O(n)$
84+
85+
## 空間複雜度
86+
87+
- 僅使用常數額外變數 `previousOneIndex`
88+
- 總空間複雜度為 $O(1)$。
89+
90+
> $O(1)$
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function kLengthApart(nums: number[], k: number): boolean {
2+
// Cache length to avoid repeated property access in the loop
3+
const length = nums.length;
4+
5+
// Track index of the previous '1'; initialize far enough to always pass for the first '1'
6+
let previousOneIndex = -k - 1;
7+
8+
for (let index = 0; index < length; index++) {
9+
// Read once to avoid repeated indexing cost
10+
const value = nums[index];
11+
12+
// Only act when we see a '1'
13+
if (value === 1) {
14+
// Check distance from the previous '1'; if too close, return early
15+
if (index - previousOneIndex <= k) {
16+
return false;
17+
}
18+
19+
// Update previous '1' position
20+
previousOneIndex = index;
21+
}
22+
}
23+
24+
// If we never violated the distance constraint, the layout is valid
25+
return true;
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function kLengthApart(nums: number[], k: number): boolean {
2+
3+
}

0 commit comments

Comments
 (0)