Longest Arithmetic Subsequence LeetCode solution in Java

1
class Solution {
2
public int longestArithSeqLength(int[] nums) {
3
int n = nums.length;
4
int longest = 0;
5
Map<Integer, Integer>[] dp = new HashMap[n];
6

7
for (int i = 0; i < n; i++) {
8
dp[i] = new HashMap<>();
9

10
for (int j = 0; j < i; j++) {
11
int diff = nums[i] - nums[j];
12
dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);
13
longest = Math.max(longest, dp[i].get(diff));
14
}
15
}
16

17
return longest;
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0