最长等差数列
难度:
标签:
题目描述
代码结果
运行时间: 534 ms, 内存: 25.3 MB
/*
Solution Approach in Java Stream:
1. The same approach as the Java solution can be applied using Java Streams for initialization and computation.
2. Use Stream API to create and initialize the dp array with HashMaps.
3. Use nested loops with Stream API operations to calculate the longest arithmetic subsequence.
*/
import java.util.*;
import java.util.stream.*;
public class Solution {
public int longestArithSeqLength(int[] nums) {
int n = nums.length;
if (n <= 2) return n;
int maxLength = 2;
List<Map<Integer, Integer>> dp = IntStream.range(0, n)
.mapToObj(i -> new HashMap<Integer, Integer>())
.collect(Collectors.toList());
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
int diff = nums[i] - nums[j];
dp.get(i).put(diff, dp.get(j).getOrDefault(diff, 1) + 1);
maxLength = Math.max(maxLength, dp.get(i).get(diff));
}
}
return maxLength;
}
}
解释
方法:
本题解使用了动态规划的方法。定义一个字典`dp`,其中`dp[a][d]`表示以数字`a`结尾,公差为`d`的最长等差子序列的长度。遍历数组中的每个数字`a`,对于每个数字,再从之前遍历过的数字中选择一个数字`b`,计算公差`d = a - b`。如果这个公差在`dp[b]`中已存在,则说明可以在以`b`结尾,公差为`d`的子序列后追加`a`,形成更长的子序列。否则,以`a`和`b`可以形成一个新的长度为2的子序列。通过这种方式,可以更新`dp[a][d]`的值,并记录过程中出现的最大长度。
时间复杂度:
O(n^2)
空间复杂度:
O(n^2)
代码细节讲解
🦆
在解题的动态规划方法中,dp字典是如何初始化和更新的?特别是在遇到新的公差d时,dp[a][d]是如何设置的?
▷🦆
为何在遍历元素b时,需要特别判断`b == a`并跳过处理?这样的处理对算法的正确性有什么影响?
▷🦆
如果数组`nums`中含有重复的数字,这种方法处理重复数字时是否会有问题?例如,处理公差计算时可能出现的除0错误。
▷