Skip to content

Commit 93272ae

Browse files
committed
✨ [1027] DPPPPP!!! FK
1 parent e9f12dc commit 93272ae

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

1027/my_solution.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestArithSeqLength = (nums) => {
6+
let dp = [], max = 0;
7+
8+
for (let right = 0; right < nums.length; right++) {
9+
dp.push(new Map());
10+
for (let left = 0; left < right; left++) {
11+
let diff = nums[left] - nums[right];
12+
13+
let newScore = (dp[left].get(diff) || 0) + 1;
14+
dp[right].set(diff, newScore);
15+
max = Math.max(max, newScore);
16+
}
17+
}
18+
19+
console.log(dp)
20+
21+
return max + 1;
22+
};
23+
24+
let x = null;
25+
26+
// x = longestArithSeqLength([3, 6, 9, 12]) // 4
27+
x = longestArithSeqLength([9, 4, 7, 2, 10]) // 3
28+
// x = longestArithSeqLength([20, 1, 15, 3, 10, 5, 8]) // 4
29+
x = longestArithSeqLength([83, 20, 17, 43, 52, 78, 68, 45]) // 3
30+
31+
console.log("Result")
32+
console.log(x)

1027/solution.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestArithSeqLength = (nums) => {
6+
let dp = [], max = 0;
7+
8+
for (let right = 0; right < nums.length; right++) {
9+
dp.push(new Map());
10+
for (let left = 0; left < right; left++) {
11+
let diff = nums[left] - nums[right];
12+
let newScore = (dp[left].get(diff) || 0) + 1;
13+
dp[right].set(diff, newScore);
14+
max = Math.max(max, newScore);
15+
}
16+
}
17+
18+
return max + 1;
19+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
- [832. Flipping an Image](./832/)
7676
- [876. Middle of the Linked List](./876/)
7777
- [997. Find the Town Judge](./997/)
78+
- [1027. Longest Arithmetic Subsequence](./1027/)
7879
- [1161. Maximum Level Sum of a Binary Tree](./1161/)
7980
- [1187. Make Array Strictly Increasing](./1187/)
8081
- [1342. Number of Steps to Reduce a Number to Zero](./1342/)
@@ -137,7 +138,7 @@ Batch create:
137138
NOTE: JS IS HERE
138139
-->
139140
```ssh
140-
chapter=104 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
141+
chapter=1027 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
141142
```
142143
> then you can use `x` for quick debug.
143144

0 commit comments

Comments
 (0)