Skip to content

Commit f31b62a

Browse files
committed
Runtime: 440 ms (Top 73.2%) | Memory: 70.79 MB (Top 29.5%)
1 parent d6d2705 commit f31b62a

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
class Solution {
2-
public int longestArithSeqLength(int[] nums) {
3-
int len=nums.length;
4-
HashMap<Integer,Integer>[] dp=new HashMap[len];
5-
int maxvalue=1;
6-
for(int i=0;i<len;i++){
7-
int curr=nums[i];
8-
dp[i]=new HashMap<>();
9-
HashMap<Integer,Integer>currmap=dp[i];
10-
for(int j=0;j<i;j++){
11-
int diff=curr-nums[j];
12-
HashMap<Integer,Integer>prev=dp[j];
13-
int newval=prev.getOrDefault(diff,0)+1;
14-
currmap.put(diff,newval);
15-
dp[i]=currmap;
16-
maxvalue=Math.max(maxvalue,currmap.get(diff));
1+
// Runtime: 440 ms (Top 73.2%) | Memory: 70.79 MB (Top 29.5%)
2+
3+
class Solution
4+
{
5+
public int longestArithSeqLength(int[] nums)
6+
{
7+
int n = nums.length;
8+
int longest = 0;
9+
Map<Integer, Integer>[] dp = new HashMap[n];
10+
11+
for (int i = 0; i < n; i++)
12+
{
13+
dp[i] = new HashMap<>();
14+
15+
for (int j = 0; j < i; j++)
16+
{
17+
int diff = nums[i] - nums[j];
18+
dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);
19+
longest = Math.max(longest, dp[i].get(diff));
1720
}
1821
}
19-
return maxvalue+1;
22+
23+
return longest;
2024
}
21-
}
25+
}

0 commit comments

Comments
 (0)