File tree Expand file tree Collapse file tree 4 files changed +140
-0
lines changed
solution/2400-2499/2411.Smallest Subarrays With Maximum Bitwise OR Expand file tree Collapse file tree 4 files changed +140
-0
lines changed Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments