Skip to content

Commit 403c0da

Browse files
committed
Add: Add question 1502
1 parent 149a30c commit 403c0da

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 1502. Can Make Arithmetic Progression From Sequence
2+
3+
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
4+
5+
Given an array of numbers `arr`, return `true` if the array can be rearranged to form an arithmetic progression.
6+
Otherwise, return `false`.
7+
8+
**Constraints:**
9+
10+
- `2 <= arr.length <= 1000`
11+
- `-10^6 <= arr[i] <= 10^6`
12+
13+
## 基礎思路
14+
15+
一個序列能否重新排列成等差數列,取決於以下性質:
16+
17+
- **等差定義**:排序後相鄰兩數之差必須一致。
18+
- **排序後檢查即可**:無論原本順序如何,只要重新排序後能呈現固定差值,就一定能重組成等差序列。
19+
- **差值唯一性**:將陣列由小到大排序後,計算第一段差值 `d = arr[1] - arr[0]`,若後續所有相鄰差值皆等於 `d`,則答案為 `true`
20+
21+
由於只需要排序並線性檢查一次,因此整體效率能滿足題目要求。
22+
23+
## 解題步驟
24+
25+
### Step 1:處理小尺寸陣列
26+
27+
若長度為 2 或更小,必定能形成等差數列,直接回傳 `true`
28+
29+
```typescript
30+
const arrayLength = arr.length;
31+
32+
// 長度 <= 2 必定能形成等差數列
33+
if (arrayLength <= 2) {
34+
return true;
35+
}
36+
```
37+
38+
### Step 2:排序陣列以檢查相鄰差值
39+
40+
將陣列進行原地排序,從最小到最大排列,使相鄰差值可被正確檢查。
41+
42+
```typescript
43+
// 就地排序
44+
arr.sort((first, second) => {
45+
return first - second;
46+
});
47+
```
48+
49+
### Step 3:計算等差差值
50+
51+
取前兩個元素的差值,作為後續比較的標準差。
52+
53+
```typescript
54+
// 計算共同差值
55+
const commonDifference = arr[1] - arr[0];
56+
```
57+
58+
### Step 4:主迴圈 — 檢查所有相鄰差值是否相同
59+
60+
遍歷從 index = 2 開始的所有元素,只要任意一段差值不同,即無法形成等差數列。
61+
62+
```typescript
63+
for (let index = 2; index < arrayLength; index++) {
64+
// 若任意相鄰差值不同於共同差值,則無法形成等差數列
65+
if (arr[index] - arr[index - 1] !== commonDifference) {
66+
return false;
67+
}
68+
}
69+
```
70+
71+
### Step 5:全部差值一致則返回 true
72+
73+
若迴圈順利結束,代表所有差值都相同。
74+
75+
```typescript
76+
return true;
77+
```
78+
79+
## 時間複雜度
80+
81+
- 排序花費 $O(n \log n)$;
82+
- 檢查差值為線性時間 $O(n)$;
83+
- 總時間複雜度為 $O(n \log n)$。
84+
85+
> $O(n \log n)$
86+
87+
## 空間複雜度
88+
89+
- 排序使用原地排序(in-place),額外空間為常數;
90+
- 其他輔助變數亦為常數級。
91+
- 總空間複雜度為 $O(1)$。
92+
93+
> $O(1)$
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function canMakeArithmeticProgression(arr: number[]): boolean {
2+
const arrayLength = arr.length;
3+
4+
// Length 2 always forms an arithmetic progression
5+
if (arrayLength <= 2) {
6+
return true;
7+
}
8+
9+
// Sort the array in-place
10+
arr.sort((first, second) => {
11+
return first - second;
12+
});
13+
14+
// Compute the common difference
15+
const commonDifference = arr[1] - arr[0];
16+
17+
// Validate each consecutive difference
18+
for (let index = 2; index < arrayLength; index++) {
19+
if (arr[index] - arr[index - 1] !== commonDifference) {
20+
return false;
21+
}
22+
}
23+
24+
return true;
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function canMakeArithmeticProgression(arr: number[]): boolean {
2+
3+
}

0 commit comments

Comments
 (0)