题解 | #最长山脉#
最长山脉
https://www.nowcoder.com/practice/f4e974a50eda429fbf36515a4197b148
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ function longestmountain(nums) { // write code here let dp = new Array(nums.length); dp[0] = 1; let direction = "top"; for (let i = 1; i < nums.length; i++) { if (direction === "top") { if (nums[i - 1] < nums[i]) { dp[i] = dp[i - 1] + 1; } else if (nums[i - 1] > nums[i]) { direction = "bottom"; dp[i] = dp[i - 1] + 1; } else { dp[i] = 1; } } else { if (nums[i - 1] > nums[i]) { dp[i] = dp[i - 1] + 1; } else if (nums[i - 1] < nums[i]) { direction = "top"; dp[i] = 2; } else { direction = "top"; dp[i] = 1; } } } const length = Math.max(...dp); return length >= 3 ? length : 0; } module.exports = { longestmountain: longestmountain, };