标签:arr,hash,int,ret,差子,序列,difference,最长 From: https://blog.csdn.net/robin_suli/article/details/144570600题目:
解析:
代码:public int longestSubsequence(int[] arr, int difference) { //直接创建一个dp表,直接在这个hash表里做动态规划 Map<Integer,Integer> hash = new HashMap<>(); int ret = 1; //hash里绑定<x,dp[i]> for(int x : arr){ hash.put(x,hash.getOrDefault(x-difference,0)+1); ret = Math.max(ret,hash.get(x)); } return ret; }