From 629d2dd8a6de85329ca12c4c453a63da6060e64d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 29 Jul 2025 07:12:07 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2411 No.2411.Smallest Subarrays With Maximum Bitwise OR --- .../README.md | 50 +++++++++++++++++++ .../README_EN.md | 50 +++++++++++++++++++ .../Solution.rs | 21 ++++++++ .../Solution.ts | 19 +++++++ 4 files changed, 140 insertions(+) create mode 100644 solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.rs create mode 100644 solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.ts diff --git a/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README.md b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README.md index 5324665f4dcf0..da598c53cd2f0 100644 --- a/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README.md +++ b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README.md @@ -181,6 +181,56 @@ func smallestSubarrays(nums []int) []int { } ``` +#### Typescript + +```ts +function smallestSubarrays(nums: number[]): number[] { + const n = nums.length; + const ans: number[] = Array(n).fill(1); + const f: number[] = Array(32).fill(-1); + + for (let i = n - 1; i >= 0; i--) { + let t = 1; + for (let j = 0; j < 32; j++) { + if ((nums[i] >> j) & 1) { + f[j] = i; + } else if (f[j] !== -1) { + t = Math.max(t, f[j] - i + 1); + } + } + ans[i] = t; + } + + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn smallest_subarrays(nums: Vec) -> Vec { + let n = nums.len(); + let mut ans = vec![1; n]; + let mut f = vec![-1; 32]; + + for i in (0..n).rev() { + let mut t = 1; + for j in 0..32 { + if (nums[i] >> j) & 1 != 0 { + f[j] = i as i32; + } else if f[j] != -1 { + t = t.max(f[j] - i as i32 + 1); + } + } + ans[i] = t; + } + + ans + } +} +``` + diff --git a/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README_EN.md b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README_EN.md index 39e27135248b9..574891f90c461 100644 --- a/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README_EN.md +++ b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README_EN.md @@ -181,6 +181,56 @@ func smallestSubarrays(nums []int) []int { } ``` +#### TypeScript + +```ts +function smallestSubarrays(nums: number[]): number[] { + const n = nums.length; + const ans: number[] = Array(n).fill(1); + const f: number[] = Array(32).fill(-1); + + for (let i = n - 1; i >= 0; i--) { + let t = 1; + for (let j = 0; j < 32; j++) { + if ((nums[i] >> j) & 1) { + f[j] = i; + } else if (f[j] !== -1) { + t = Math.max(t, f[j] - i + 1); + } + } + ans[i] = t; + } + + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn smallest_subarrays(nums: Vec) -> Vec { + let n = nums.len(); + let mut ans = vec![1; n]; + let mut f = vec![-1; 32]; + + for i in (0..n).rev() { + let mut t = 1; + for j in 0..32 { + if (nums[i] >> j) & 1 != 0 { + f[j] = i as i32; + } else if f[j] != -1 { + t = t.max(f[j] - i as i32 + 1); + } + } + ans[i] = t; + } + + ans + } +} +``` + diff --git a/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.rs b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.rs new file mode 100644 index 0000000000000..368c25620a92c --- /dev/null +++ b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn smallest_subarrays(nums: Vec) -> Vec { + let n = nums.len(); + let mut ans = vec![1; n]; + let mut f = vec![-1; 32]; + + for i in (0..n).rev() { + let mut t = 1; + for j in 0..32 { + if (nums[i] >> j) & 1 != 0 { + f[j] = i as i32; + } else if f[j] != -1 { + t = t.max(f[j] - i as i32 + 1); + } + } + ans[i] = t; + } + + ans + } +} diff --git a/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.ts b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.ts new file mode 100644 index 0000000000000..c70a00bf5a1db --- /dev/null +++ b/solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/Solution.ts @@ -0,0 +1,19 @@ +function smallestSubarrays(nums: number[]): number[] { + const n = nums.length; + const ans: number[] = Array(n).fill(1); + const f: number[] = Array(32).fill(-1); + + for (let i = n - 1; i >= 0; i--) { + let t = 1; + for (let j = 0; j < 32; j++) { + if ((nums[i] >> j) & 1) { + f[j] = i; + } else if (f[j] !== -1) { + t = Math.max(t, f[j] - i + 1); + } + } + ans[i] = t; + } + + return ans; +}