From cb73e9e96a05822027783ec2e95d8e6b005112c4 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 14 Sep 2024 22:28:34 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.2419 --- .../README.md | 42 ++++++++++++++++++- .../README_EN.md | 40 ++++++++++++++++++ .../Solution.js | 15 +++++++ .../Solution.ts | 15 +++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js create mode 100644 solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md index 66a3323734c16..d2a741a21ecf8 100644 --- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md @@ -52,7 +52,7 @@ tags: 输入:nums = [1,2,3,4] 输出:1 解释: -子数组按位与运算的最大值是 4 。 +子数组按位与运算的最大值是 4 。 能得到此结果的最长子数组是 [4],所以返回 1 。 @@ -161,6 +161,46 @@ func longestSubarray(nums []int) int { } ``` +#### TypeScript + +```ts +function longestSubarray(nums: number[]): number { + const mx = Math.max(...nums); + let [ans, cnt] = [0, 0]; + + for (const x of nums) { + if (x === mx) { + cnt++; + ans = Math.max(ans, cnt); + } else { + cnt = 0; + } + } + + return ans; +} +``` + +#### JavaScript + +```js +function longestSubarray(nums) { + const mx = Math.max(...nums); + let [ans, cnt] = [0, 0]; + + for (const x of nums) { + if (x === mx) { + cnt++; + ans = Math.max(ans, cnt); + } else { + cnt = 0; + } + } + + return ans; +} +``` + diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md index 96b33268c4496..83dd92a8226b9 100644 --- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md @@ -159,6 +159,46 @@ func longestSubarray(nums []int) int { } ``` +#### TypeScript + +```ts +function longestSubarray(nums: number[]): number { + const mx = Math.max(...nums); + let [ans, cnt] = [0, 0]; + + for (const x of nums) { + if (x === mx) { + cnt++; + ans = Math.max(ans, cnt); + } else { + cnt = 0; + } + } + + return ans; +} +``` + +#### JavaScript + +```js +function longestSubarray(nums) { + const mx = Math.max(...nums); + let [ans, cnt] = [0, 0]; + + for (const x of nums) { + if (x === mx) { + cnt++; + ans = Math.max(ans, cnt); + } else { + cnt = 0; + } + } + + return ans; +} +``` + diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js new file mode 100644 index 0000000000000..c949ce8d97802 --- /dev/null +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.js @@ -0,0 +1,15 @@ +function longestSubarray(nums) { + const mx = Math.max(...nums); + let [ans, cnt] = [0, 0]; + + for (const x of nums) { + if (x === mx) { + cnt++; + ans = Math.max(ans, cnt); + } else { + cnt = 0; + } + } + + return ans; +} diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts new file mode 100644 index 0000000000000..4bd008ce7efba --- /dev/null +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.ts @@ -0,0 +1,15 @@ +function longestSubarray(nums: number[]): number { + const mx = Math.max(...nums); + let [ans, cnt] = [0, 0]; + + for (const x of nums) { + if (x === mx) { + cnt++; + ans = Math.max(ans, cnt); + } else { + cnt = 0; + } + } + + return ans; +}