From e5229c61ccac09608b2110f9eaa83c2c8b2b8562 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 22 Jun 2024 19:36:53 +0300 Subject: [PATCH] refactor: update ts solution to lc problem: No.1248 --- .../1248.Count Number of Nice Subarrays/README.md | 7 ++----- .../1248.Count Number of Nice Subarrays/README_EN.md | 7 ++----- .../1248.Count Number of Nice Subarrays/Solution.ts | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md b/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md index 7c5a8b3e828d1..4f93aa018c6fc 100644 --- a/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md +++ b/solution/1200-1299/1248.Count Number of Nice Subarrays/README.md @@ -157,13 +157,10 @@ function numberOfSubarrays(nums: number[], k: number): number { const n = nums.length; const cnt = Array(n + 1).fill(0); cnt[0] = 1; - let ans = 0; - let t = 0; + let [t, ans] = [0, 0]; for (const v of nums) { t += v & 1; - if (t >= k) { - ans += cnt[t - k]; - } + ans += cnt[t - k] ?? 0; cnt[t] += 1; } return ans; diff --git a/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md b/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md index 492b0e35d7a75..b6eebf6cc6e73 100644 --- a/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md +++ b/solution/1200-1299/1248.Count Number of Nice Subarrays/README_EN.md @@ -155,13 +155,10 @@ function numberOfSubarrays(nums: number[], k: number): number { const n = nums.length; const cnt = Array(n + 1).fill(0); cnt[0] = 1; - let ans = 0; - let t = 0; + let [t, ans] = [0, 0]; for (const v of nums) { t += v & 1; - if (t >= k) { - ans += cnt[t - k]; - } + ans += cnt[t - k] ?? 0; cnt[t] += 1; } return ans; diff --git a/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts b/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts index 4a6eaa498b4df..c5545d8d35674 100644 --- a/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts +++ b/solution/1200-1299/1248.Count Number of Nice Subarrays/Solution.ts @@ -2,13 +2,10 @@ function numberOfSubarrays(nums: number[], k: number): number { const n = nums.length; const cnt = Array(n + 1).fill(0); cnt[0] = 1; - let ans = 0; - let t = 0; + let [t, ans] = [0, 0]; for (const v of nums) { t += v & 1; - if (t >= k) { - ans += cnt[t - k]; - } + ans += cnt[t - k] ?? 0; cnt[t] += 1; } return ans;