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; +}