GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 最长等差数列-1027 · Issue #82 · sl1673495/leetcode-javascript Open Labels Description给定一个整数数组 A,返回 A 中最长等差子序列的长度。 回想一下,A 的子序列是列表
来源:力扣(LeetCode) 思路从后往前求 dp 的规划数组,对于每一项都要求从本项开始,一直循环到结尾,记录每个相对每个值的差的等差数列长度。 注意由于这个问题的最优解等差数列的起点不一定在第一项,所以需要在每次求解的时候都去更新全局变量 /** * @param {number[]} A * @return {number} */ let longestArithSeqLength = function (A) { let dp = [] let n = A.length if (!n) { return 0 } let max = 0 dp[n - 1] = { 0: 0 } for (let i = n - 2; i >= 0; i--) { dp[i] = {} let cur = A[i] for (let j = i + 1; j < n; j++) { let diff = A[j] - cur let maxLength = (dp[j][diff] || 0) + 1 let maxIDiff = Math.max(dp[i][diff] || 0, maxLength) dp[i][diff] = maxIDiff max = Math.max(max, maxIDiff) } } return max + 1 }; ActivityMetadataMetadataAssigneesLabelsProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |