From 751db3ab9392fcc256813d50a18702936dfe53d7 Mon Sep 17 00:00:00 2001 From: kazusa <409053122@qq.com> Date: Mon, 15 Dec 2025 15:30:58 +0800 Subject: [PATCH] =?UTF-8?q?[2110]=20=E8=82=A1=E7=A5=A8=E5=B9=B3=E6=BB=91?= =?UTF-8?q?=E4=B8=8B=E8=B7=8C=E9=98=B6=E6=AE=B5=E7=9A=84=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javascript/2110.股票平滑下跌阶段的数目.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/leecode/javascript/2110.股票平滑下跌阶段的数目.js diff --git a/src/leecode/javascript/2110.股票平滑下跌阶段的数目.js b/src/leecode/javascript/2110.股票平滑下跌阶段的数目.js new file mode 100644 index 0000000..3195d2a --- /dev/null +++ b/src/leecode/javascript/2110.股票平滑下跌阶段的数目.js @@ -0,0 +1,61 @@ +/* + * @lc app=leetcode.cn id=2110 lang=javascript + * @lcpr version= + * + * [2110] 股票平滑下跌阶段的数目 + */ + +// @lc code=start +/** + * @param {number[]} prices + * @return {number} + */ +var getDescentPeriods = function (prices) { + // 使用双指针圈定连续的段,当段不连续后,计算连续段所有符合的子段数,重新寻找连续段 + let [left, right] = [0, 0]; + let ans = 0; + while (right <= prices.length - 1) { + if (right == left) { + // 重置后的位置 + right++; + continue; + } + // 不满足连续段 + if (right >= 1 && prices[right - 1] - prices[right] != 1) { + // 找出上一个连续段的个数 + let num = right - 1 - left + 1; + // 计算连续段所有符合的子段数 + ans += (num * (num + 1)) / 2; + // 重置连续段左右指针 + left = right; + continue; + } + right++; + } + right = prices.length - 1; + // 最后一次计算 + if (prices[right - 1] - prices[right] != 1) { + ans += 1; + } else { + let num = right - left + 1; + ans += (num * (num + 1)) / 2; + } + return ans; +}; +getDescentPeriods([3,2,1,4]); +// @lc code=end + +/* +// @lcpr case=start +// [3,2,1,4]\n +// @lcpr case=end + +// @lcpr case=start +// [8,6,7,7]\n +// @lcpr case=end + +// @lcpr case=start +// [1]\n +// @lcpr case=end + + */