Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2aae650

Browse files
feat: add solutions to lc problem: No.2411 (doocs#4603)
No.2411.Smallest Subarrays With Maximum Bitwise OR
1 parent fb3cfb3 commit 2aae650

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

‎solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,56 @@ func smallestSubarrays(nums []int) []int {
181181
}
182182
```
183183

184+
#### Typescript
185+
186+
```ts
187+
function smallestSubarrays(nums: number[]): number[] {
188+
const n = nums.length;
189+
const ans: number[] = Array(n).fill(1);
190+
const f: number[] = Array(32).fill(-1);
191+
192+
for (let i = n - 1; i >= 0; i--) {
193+
let t = 1;
194+
for (let j = 0; j < 32; j++) {
195+
if ((nums[i] >> j) & 1) {
196+
f[j] = i;
197+
} else if (f[j] !== -1) {
198+
t = Math.max(t, f[j] - i + 1);
199+
}
200+
}
201+
ans[i] = t;
202+
}
203+
204+
return ans;
205+
}
206+
```
207+
208+
#### Rust
209+
210+
```rust
211+
impl Solution {
212+
pub fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
213+
let n = nums.len();
214+
let mut ans = vec![1; n];
215+
let mut f = vec![-1; 32];
216+
217+
for i in (0..n).rev() {
218+
let mut t = 1;
219+
for j in 0..32 {
220+
if (nums[i] >> j) & 1 != 0 {
221+
f[j] = i as i32;
222+
} else if f[j] != -1 {
223+
t = t.max(f[j] - i as i32 + 1);
224+
}
225+
}
226+
ans[i] = t;
227+
}
228+
229+
ans
230+
}
231+
}
232+
```
233+
184234
<!-- tabs:end -->
185235

186236
<!-- solution:end -->

‎solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR/README_EN.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,56 @@ func smallestSubarrays(nums []int) []int {
181181
}
182182
```
183183

184+
#### TypeScript
185+
186+
```ts
187+
function smallestSubarrays(nums: number[]): number[] {
188+
const n = nums.length;
189+
const ans: number[] = Array(n).fill(1);
190+
const f: number[] = Array(32).fill(-1);
191+
192+
for (let i = n - 1; i >= 0; i--) {
193+
let t = 1;
194+
for (let j = 0; j < 32; j++) {
195+
if ((nums[i] >> j) & 1) {
196+
f[j] = i;
197+
} else if (f[j] !== -1) {
198+
t = Math.max(t, f[j] - i + 1);
199+
}
200+
}
201+
ans[i] = t;
202+
}
203+
204+
return ans;
205+
}
206+
```
207+
208+
#### Rust
209+
210+
```rust
211+
impl Solution {
212+
pub fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
213+
let n = nums.len();
214+
let mut ans = vec![1; n];
215+
let mut f = vec![-1; 32];
216+
217+
for i in (0..n).rev() {
218+
let mut t = 1;
219+
for j in 0..32 {
220+
if (nums[i] >> j) & 1 != 0 {
221+
f[j] = i as i32;
222+
} else if f[j] != -1 {
223+
t = t.max(f[j] - i as i32 + 1);
224+
}
225+
}
226+
ans[i] = t;
227+
}
228+
229+
ans
230+
}
231+
}
232+
```
233+
184234
<!-- tabs:end -->
185235

186236
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn smallest_subarrays(nums: Vec<i32>) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut ans = vec![1; n];
5+
let mut f = vec![-1; 32];
6+
7+
for i in (0..n).rev() {
8+
let mut t = 1;
9+
for j in 0..32 {
10+
if (nums[i] >> j) & 1 != 0 {
11+
f[j] = i as i32;
12+
} else if f[j] != -1 {
13+
t = t.max(f[j] - i as i32 + 1);
14+
}
15+
}
16+
ans[i] = t;
17+
}
18+
19+
ans
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function smallestSubarrays(nums: number[]): number[] {
2+
const n = nums.length;
3+
const ans: number[] = Array(n).fill(1);
4+
const f: number[] = Array(32).fill(-1);
5+
6+
for (let i = n - 1; i >= 0; i--) {
7+
let t = 1;
8+
for (let j = 0; j < 32; j++) {
9+
if ((nums[i] >> j) & 1) {
10+
f[j] = i;
11+
} else if (f[j] !== -1) {
12+
t = Math.max(t, f[j] - i + 1);
13+
}
14+
}
15+
ans[i] = t;
16+
}
17+
18+
return ans;
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /