Skip to content

Commit c221a70

Browse files
committed
feat: add solutions to lc problem: No.1027
1 parent 316d63c commit c221a70

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

solution/1000-1099/1027.Longest Arithmetic Subsequence/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ func max(a, b int) int {
154154
}
155155
```
156156

157+
### **TypeScript**
158+
159+
```ts
160+
function longestArithSeqLength(nums: number[]): number {
161+
const n = nums.length;
162+
let ans = 0;
163+
const f: number[][] = Array.from({ length: n }, () =>
164+
new Array(1001).fill(0),
165+
);
166+
for (let i = 1; i < n; ++i) {
167+
for (let k = 0; k < i; ++k) {
168+
const j = nums[i] - nums[k] + 500;
169+
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
170+
ans = Math.max(ans, f[i][j]);
171+
}
172+
}
173+
return ans + 1;
174+
}
175+
```
176+
157177
### **...**
158178

159179
```

solution/1000-1099/1027.Longest Arithmetic Subsequence/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ func max(a, b int) int {
136136
}
137137
```
138138

139+
### **TypeScript**
140+
141+
```ts
142+
function longestArithSeqLength(nums: number[]): number {
143+
const n = nums.length;
144+
let ans = 0;
145+
const f: number[][] = Array.from({ length: n }, () =>
146+
new Array(1001).fill(0),
147+
);
148+
for (let i = 1; i < n; ++i) {
149+
for (let k = 0; k < i; ++k) {
150+
const j = nums[i] - nums[k] + 500;
151+
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
152+
ans = Math.max(ans, f[i][j]);
153+
}
154+
}
155+
return ans + 1;
156+
}
157+
```
158+
139159
### **...**
140160

141161
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function longestArithSeqLength(nums: number[]): number {
2+
const n = nums.length;
3+
let ans = 0;
4+
const f: number[][] = Array.from({ length: n }, () =>
5+
new Array(1001).fill(0),
6+
);
7+
for (let i = 1; i < n; ++i) {
8+
for (let k = 0; k < i; ++k) {
9+
const j = nums[i] - nums[k] + 500;
10+
f[i][j] = Math.max(f[i][j], f[k][j] + 1);
11+
ans = Math.max(ans, f[i][j]);
12+
}
13+
}
14+
return ans + 1;
15+
}

0 commit comments

Comments
 (0)