Skip to content

Commit 7ead208

Browse files
authored
feat: add solutions to lc problem: No.1501 (#4644)
1 parent 9b0c8ad commit 7ead208

File tree

9 files changed

+183
-128
lines changed

9 files changed

+183
-128
lines changed

solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README.md

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ tags:
5858

5959
### 方法一:排序 + 遍历
6060

61-
我们可以先将数组 `arr` 排序,然后遍历数组,判断相邻两项的差是否相等即可
61+
我们可以先将数组 $\textit{arr}$ 排序,然后计算前两项的差值 $d$,接着遍历数组,判断相邻两项的差是否等于 $d$
6262

63-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `arr` 的长度。
63+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
6464

6565
<!-- tabs:start -->
6666

@@ -130,8 +130,9 @@ func canMakeArithmeticProgression(arr []int) bool {
130130
function canMakeArithmeticProgression(arr: number[]): boolean {
131131
arr.sort((a, b) => a - b);
132132
const n = arr.length;
133+
const d = arr[1] - arr[0];
133134
for (let i = 2; i < n; i++) {
134-
if (arr[i - 2] - arr[i - 1] !== arr[i - 1] - arr[i]) {
135+
if (arr[i] - arr[i - 1] !== d) {
135136
return false;
136137
}
137138
}
@@ -146,8 +147,9 @@ impl Solution {
146147
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
147148
arr.sort();
148149
let n = arr.len();
150+
let d = arr[1] - arr[0];
149151
for i in 2..n {
150-
if arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i] {
152+
if arr[i] - arr[i - 1] != d {
151153
return false;
152154
}
153155
}
@@ -165,8 +167,10 @@ impl Solution {
165167
*/
166168
var canMakeArithmeticProgression = function (arr) {
167169
arr.sort((a, b) => a - b);
168-
for (let i = 1; i < arr.length - 1; i++) {
169-
if (arr[i] << 1 != arr[i - 1] + arr[i + 1]) {
170+
const n = arr.length;
171+
const d = arr[1] - arr[0];
172+
for (let i = 2; i < n; i++) {
173+
if (arr[i] - arr[i - 1] !== d) {
170174
return false;
171175
}
172176
}
@@ -183,8 +187,9 @@ int cmp(const void* a, const void* b) {
183187

184188
bool canMakeArithmeticProgression(int* arr, int arrSize) {
185189
qsort(arr, arrSize, sizeof(int), cmp);
190+
int d = arr[1] - arr[0];
186191
for (int i = 2; i < arrSize; i++) {
187-
if (arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i]) {
192+
if (arr[i] - arr[i - 1] != d) {
188193
return 0;
189194
}
190195
}
@@ -200,11 +205,11 @@ bool canMakeArithmeticProgression(int* arr, int arrSize) {
200205
201206
### 方法二:哈希表 + 数学
202207
203-
我们先找出数组 $arr$ 中的最小值 $a$ 和最大值 $b$,如果数组 $arr$ 可以重排成等差数列,那么公差 $d = \frac{b - a}{n - 1}$ 必须为整数。
208+
我们先找出数组 $\textit{arr}$ 中的最小值 $a$ 和最大值 $b$,如果数组 $\textit{arr}$ 可以重排成等差数列,那么公差 $d = \frac{b - a}{n - 1}$ 必须为整数。
204209
205-
我们可以用哈希表来记录数组 $arr$ 中的所有元素,然后遍历 $i \in [0, n)$,判断 $a + d \times i$ 是否在哈希表中,如果不在,说明数组 $arr$ 不能重排成等差数列,返回 `false`。否则遍历完数组后,返回 `true`。
210+
我们可以用哈希表来记录数组 $\textit{arr}$ 中的所有元素,然后遍历 $i \in [0, n)$,判断 $a + d \times i$ 是否在哈希表中,如果不在,说明数组 $\textit{arr}$ 不能重排成等差数列,返回 `false`。否则遍历完数组后,返回 `true`。
206211
207-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `arr` 的长度。
212+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
208213
209214
<!-- tabs:start -->
210215
@@ -301,23 +306,18 @@ func canMakeArithmeticProgression(arr []int) bool {
301306
```ts
302307
function canMakeArithmeticProgression(arr: number[]): boolean {
303308
const n = arr.length;
304-
const map = new Map<number, number>();
305-
let min = Infinity;
306-
let max = -Infinity;
307-
for (const num of arr) {
308-
map.set(num, (map.get(num) ?? 0) + 1);
309-
min = Math.min(min, num);
310-
max = Math.max(max, num);
311-
}
312-
if (max === min) {
313-
return true;
314-
}
315-
if ((max - min) % (arr.length - 1)) {
309+
const a = Math.min(...arr);
310+
const b = Math.max(...arr);
311+
312+
if ((b - a) % (n - 1) !== 0) {
316313
return false;
317314
}
318-
const diff = (max - min) / (arr.length - 1);
319-
for (let i = min; i <= max; i += diff) {
320-
if (map.get(i) !== 1) {
315+
316+
const d = (b - a) / (n - 1);
317+
const s = new Set(arr);
318+
319+
for (let i = 0; i < n; ++i) {
320+
if (!s.has(a + d * i)) {
321321
return false;
322322
}
323323
}
@@ -328,37 +328,57 @@ function canMakeArithmeticProgression(arr: number[]): boolean {
328328
#### Rust
329329

330330
```rust
331-
use std::collections::HashMap;
332331
impl Solution {
333332
pub fn can_make_arithmetic_progression(arr: Vec<i32>) -> bool {
334-
let n = arr.len() as i32;
335-
let mut min = i32::MAX;
336-
let mut max = i32::MIN;
337-
let mut map = HashMap::new();
338-
for &num in arr.iter() {
339-
*map.entry(num).or_insert(0) += 1;
340-
min = min.min(num);
341-
max = max.max(num);
342-
}
343-
if min == max {
344-
return true;
345-
}
346-
if (max - min) % (n - 1) != 0 {
333+
let n = arr.len();
334+
let a = *arr.iter().min().unwrap();
335+
let b = *arr.iter().max().unwrap();
336+
337+
if (b - a) % (n as i32 - 1) != 0 {
347338
return false;
348339
}
349-
let diff = (max - min) / (n - 1);
350-
let mut k = min;
351-
while k <= max {
352-
if *map.get(&k).unwrap_or(&0) != 1 {
340+
341+
let d = (b - a) / (n as i32 - 1);
342+
let s: std::collections::HashSet<_> = arr.into_iter().collect();
343+
344+
for i in 0..n {
345+
if !s.contains(&(a + d * i as i32)) {
353346
return false;
354347
}
355-
k += diff;
356348
}
357349
true
358350
}
359351
}
360352
```
361353

354+
#### JavaScript
355+
356+
```js
357+
/**
358+
* @param {number[]} arr
359+
* @return {boolean}
360+
*/
361+
var canMakeArithmeticProgression = function (arr) {
362+
const n = arr.length;
363+
const a = Math.min(...arr);
364+
const b = Math.max(...arr);
365+
366+
if ((b - a) % (n - 1) !== 0) {
367+
return false;
368+
}
369+
370+
const d = (b - a) / (n - 1);
371+
const s = new Set(arr);
372+
373+
for (let i = 0; i < n; ++i) {
374+
if (!s.has(a + d * i)) {
375+
return false;
376+
}
377+
}
378+
return true;
379+
};
380+
```
381+
362382
<!-- tabs:end -->
363383

364384
<!-- solution:end -->

solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/README_EN.md

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ tags:
5656

5757
### Solution 1: Sorting + Traversal
5858

59-
We can first sort the array `arr`, then traverse the array, and check whether the difference between adjacent items is equal.
59+
We can first sort the array $\textit{arr}$, then traverse the array, and check whether the difference between adjacent items is equal.
6060

61-
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array `arr`.
61+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{arr}$.
6262

6363
<!-- tabs:start -->
6464

@@ -128,8 +128,9 @@ func canMakeArithmeticProgression(arr []int) bool {
128128
function canMakeArithmeticProgression(arr: number[]): boolean {
129129
arr.sort((a, b) => a - b);
130130
const n = arr.length;
131+
const d = arr[1] - arr[0];
131132
for (let i = 2; i < n; i++) {
132-
if (arr[i - 2] - arr[i - 1] !== arr[i - 1] - arr[i]) {
133+
if (arr[i] - arr[i - 1] !== d) {
133134
return false;
134135
}
135136
}
@@ -144,8 +145,9 @@ impl Solution {
144145
pub fn can_make_arithmetic_progression(mut arr: Vec<i32>) -> bool {
145146
arr.sort();
146147
let n = arr.len();
148+
let d = arr[1] - arr[0];
147149
for i in 2..n {
148-
if arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i] {
150+
if arr[i] - arr[i - 1] != d {
149151
return false;
150152
}
151153
}
@@ -163,8 +165,10 @@ impl Solution {
163165
*/
164166
var canMakeArithmeticProgression = function (arr) {
165167
arr.sort((a, b) => a - b);
166-
for (let i = 1; i < arr.length - 1; i++) {
167-
if (arr[i] << 1 != arr[i - 1] + arr[i + 1]) {
168+
const n = arr.length;
169+
const d = arr[1] - arr[0];
170+
for (let i = 2; i < n; i++) {
171+
if (arr[i] - arr[i - 1] !== d) {
168172
return false;
169173
}
170174
}
@@ -181,8 +185,9 @@ int cmp(const void* a, const void* b) {
181185

182186
bool canMakeArithmeticProgression(int* arr, int arrSize) {
183187
qsort(arr, arrSize, sizeof(int), cmp);
188+
int d = arr[1] - arr[0];
184189
for (int i = 2; i < arrSize; i++) {
185-
if (arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i]) {
190+
if (arr[i] - arr[i - 1] != d) {
186191
return 0;
187192
}
188193
}
@@ -198,11 +203,11 @@ bool canMakeArithmeticProgression(int* arr, int arrSize) {
198203
199204
### Solution 2: Hash Table + Mathematics
200205
201-
We first find the minimum value $a$ and the maximum value $b$ in the array $arr$. If the array $arr$ can be rearranged into an arithmetic sequence, then the common difference $d = \frac{b - a}{n - 1}$ must be an integer.
206+
We first find the minimum value $a$ and the maximum value $b$ in the array $\textit{arr}$. If the array $\textit{arr}$ can be rearranged into an arithmetic sequence, then the common difference $d = \frac{b - a}{n - 1}$ must be an integer.
202207
203-
We can use a hash table to record all elements in the array $arr$, then traverse $i \in [0, n)$, and check whether $a + d \times i$ is in the hash table. If not, it means that the array $arr$ cannot be rearranged into an arithmetic sequence, and we return `false`. Otherwise, after traversing the array, we return `true`.
208+
We can use a hash table to record all elements in the array $\textit{arr}$, then traverse $i \in [0, n)$, and check whether $a + d \times i$ is in the hash table. If not, it means that the array $\textit{arr}$ cannot be rearranged into an arithmetic sequence, and we return `false`. Otherwise, after traversing the array, we return `true`.
204209
205-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `arr`.
210+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$.
206211
207212
<!-- tabs:start -->
208213
@@ -299,23 +304,18 @@ func canMakeArithmeticProgression(arr []int) bool {
299304
```ts
300305
function canMakeArithmeticProgression(arr: number[]): boolean {
301306
const n = arr.length;
302-
const map = new Map<number, number>();
303-
let min = Infinity;
304-
let max = -Infinity;
305-
for (const num of arr) {
306-
map.set(num, (map.get(num) ?? 0) + 1);
307-
min = Math.min(min, num);
308-
max = Math.max(max, num);
309-
}
310-
if (max === min) {
311-
return true;
312-
}
313-
if ((max - min) % (arr.length - 1)) {
307+
const a = Math.min(...arr);
308+
const b = Math.max(...arr);
309+
310+
if ((b - a) % (n - 1) !== 0) {
314311
return false;
315312
}
316-
const diff = (max - min) / (arr.length - 1);
317-
for (let i = min; i <= max; i += diff) {
318-
if (map.get(i) !== 1) {
313+
314+
const d = (b - a) / (n - 1);
315+
const s = new Set(arr);
316+
317+
for (let i = 0; i < n; ++i) {
318+
if (!s.has(a + d * i)) {
319319
return false;
320320
}
321321
}
@@ -326,37 +326,57 @@ function canMakeArithmeticProgression(arr: number[]): boolean {
326326
#### Rust
327327

328328
```rust
329-
use std::collections::HashMap;
330329
impl Solution {
331330
pub fn can_make_arithmetic_progression(arr: Vec<i32>) -> bool {
332-
let n = arr.len() as i32;
333-
let mut min = i32::MAX;
334-
let mut max = i32::MIN;
335-
let mut map = HashMap::new();
336-
for &num in arr.iter() {
337-
*map.entry(num).or_insert(0) += 1;
338-
min = min.min(num);
339-
max = max.max(num);
340-
}
341-
if min == max {
342-
return true;
343-
}
344-
if (max - min) % (n - 1) != 0 {
331+
let n = arr.len();
332+
let a = *arr.iter().min().unwrap();
333+
let b = *arr.iter().max().unwrap();
334+
335+
if (b - a) % (n as i32 - 1) != 0 {
345336
return false;
346337
}
347-
let diff = (max - min) / (n - 1);
348-
let mut k = min;
349-
while k <= max {
350-
if *map.get(&k).unwrap_or(&0) != 1 {
338+
339+
let d = (b - a) / (n as i32 - 1);
340+
let s: std::collections::HashSet<_> = arr.into_iter().collect();
341+
342+
for i in 0..n {
343+
if !s.contains(&(a + d * i as i32)) {
351344
return false;
352345
}
353-
k += diff;
354346
}
355347
true
356348
}
357349
}
358350
```
359351

352+
#### JavaScript
353+
354+
```js
355+
/**
356+
* @param {number[]} arr
357+
* @return {boolean}
358+
*/
359+
var canMakeArithmeticProgression = function (arr) {
360+
const n = arr.length;
361+
const a = Math.min(...arr);
362+
const b = Math.max(...arr);
363+
364+
if ((b - a) % (n - 1) !== 0) {
365+
return false;
366+
}
367+
368+
const d = (b - a) / (n - 1);
369+
const s = new Set(arr);
370+
371+
for (let i = 0; i < n; ++i) {
372+
if (!s.has(a + d * i)) {
373+
return false;
374+
}
375+
}
376+
return true;
377+
};
378+
```
379+
360380
<!-- tabs:end -->
361381

362382
<!-- solution:end -->

solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ int cmp(const void* a, const void* b) {
44

55
bool canMakeArithmeticProgression(int* arr, int arrSize) {
66
qsort(arr, arrSize, sizeof(int), cmp);
7+
int d = arr[1] - arr[0];
78
for (int i = 2; i < arrSize; i++) {
8-
if (arr[i - 2] - arr[i - 1] != arr[i - 1] - arr[i]) {
9+
if (arr[i] - arr[i - 1] != d) {
910
return 0;
1011
}
1112
}
1213
return 1;
13-
}
14+
}

solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*/
55
var canMakeArithmeticProgression = function (arr) {
66
arr.sort((a, b) => a - b);
7-
for (let i = 1; i < arr.length - 1; i++) {
8-
if (arr[i] << 1 != arr[i - 1] + arr[i + 1]) {
7+
const n = arr.length;
8+
const d = arr[1] - arr[0];
9+
for (let i = 2; i < n; i++) {
10+
if (arr[i] - arr[i - 1] !== d) {
911
return false;
1012
}
1113
}

0 commit comments

Comments
 (0)