Skip to content

Commit

Permalink
refactor: update ts solution to lc problem: No.1248 (#3151)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 committed Jun 23, 2024
1 parent a6df486 commit 0041e22
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0041e22

Please sign in to comment.