File tree 3 files changed +148
-0
lines changed
solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets
3 files changed +148
-0
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,30 @@ function countMaxOrSubsets(nums: number[]): number {
140
140
}
141
141
```
142
142
143
+ ``` ts
144
+ function countMaxOrSubsets(nums : number []): number {
145
+ const n = nums .length ;
146
+ let res = 0 ;
147
+ let max = - Infinity ;
148
+ const dfs = (i : number , sum : number ) => {
149
+ for (let j = i ; j < n ; j ++ ) {
150
+ const num = sum | nums [j ];
151
+ if (num >= max ) {
152
+ if (num > max ) {
153
+ max = num ;
154
+ res = 0 ;
155
+ }
156
+ res ++ ;
157
+ }
158
+ dfs (j + 1 , num );
159
+ }
160
+ };
161
+ dfs (0 , 0 );
162
+
163
+ return res ;
164
+ }
165
+ ```
166
+
143
167
### ** C++**
144
168
145
169
``` cpp
@@ -196,6 +220,41 @@ func countMaxOrSubsets(nums []int) int {
196
220
}
197
221
```
198
222
223
+ ### ** Rust**
224
+
225
+ ``` rust
226
+ impl Solution {
227
+ fn dfs (nums : & Vec <i32 >, i : usize , sum : i32 ) -> (i32 , i32 ) {
228
+ let n = nums . len ();
229
+ let mut max = i32 :: MIN ;
230
+ let mut res = 0 ;
231
+ for j in i .. n {
232
+ let num = sum | nums [j ];
233
+ if num >= max {
234
+ if num > max {
235
+ max = num ;
236
+ res = 0 ;
237
+ }
238
+ res += 1 ;
239
+ }
240
+ let (r_max , r_res ) = Self :: dfs (nums , j + 1 , num );
241
+ if r_max >= max {
242
+ if r_max > max {
243
+ max = r_max ;
244
+ res = 0 ;
245
+ }
246
+ res += r_res ;
247
+ }
248
+ }
249
+ (max , res )
250
+ }
251
+
252
+ pub fn count_max_or_subsets (nums : Vec <i32 >) -> i32 {
253
+ Self :: dfs (& nums , 0 , 0 ). 1
254
+ }
255
+ }
256
+ ```
257
+
199
258
### ** ...**
200
259
201
260
```
Original file line number Diff line number Diff line change @@ -130,6 +130,30 @@ function countMaxOrSubsets(nums: number[]): number {
130
130
}
131
131
```
132
132
133
+ ``` ts
134
+ function countMaxOrSubsets(nums : number []): number {
135
+ const n = nums .length ;
136
+ let res = 0 ;
137
+ let max = - Infinity ;
138
+ const dfs = (i : number , sum : number ) => {
139
+ for (let j = i ; j < n ; j ++ ) {
140
+ const num = sum | nums [j ];
141
+ if (num >= max ) {
142
+ if (num > max ) {
143
+ max = num ;
144
+ res = 0 ;
145
+ }
146
+ res ++ ;
147
+ }
148
+ dfs (j + 1 , num );
149
+ }
150
+ };
151
+ dfs (0 , 0 );
152
+
153
+ return res ;
154
+ }
155
+ ```
156
+
133
157
### ** C++**
134
158
135
159
``` cpp
@@ -186,6 +210,41 @@ func countMaxOrSubsets(nums []int) int {
186
210
}
187
211
```
188
212
213
+ ### ** Rust**
214
+
215
+ ``` rust
216
+ impl Solution {
217
+ fn dfs (nums : & Vec <i32 >, i : usize , sum : i32 ) -> (i32 , i32 ) {
218
+ let n = nums . len ();
219
+ let mut max = i32 :: MIN ;
220
+ let mut res = 0 ;
221
+ for j in i .. n {
222
+ let num = sum | nums [j ];
223
+ if num >= max {
224
+ if num > max {
225
+ max = num ;
226
+ res = 0 ;
227
+ }
228
+ res += 1 ;
229
+ }
230
+ let (r_max , r_res ) = Self :: dfs (nums , j + 1 , num );
231
+ if r_max >= max {
232
+ if r_max > max {
233
+ max = r_max ;
234
+ res = 0 ;
235
+ }
236
+ res += r_res ;
237
+ }
238
+ }
239
+ (max , res )
240
+ }
241
+
242
+ pub fn count_max_or_subsets (nums : Vec <i32 >) -> i32 {
243
+ Self :: dfs (& nums , 0 , 0 ). 1
244
+ }
245
+ }
246
+ ```
247
+
189
248
### ** ...**
190
249
191
250
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ fn dfs ( nums : & Vec < i32 > , i : usize , sum : i32 ) -> ( i32 , i32 ) {
3
+ let n = nums. len ( ) ;
4
+ let mut max = i32:: MIN ;
5
+ let mut res = 0 ;
6
+ for j in i..n {
7
+ let num = sum | nums[ j] ;
8
+ if num >= max {
9
+ if num > max {
10
+ max = num;
11
+ res = 0 ;
12
+ }
13
+ res += 1 ;
14
+ }
15
+ let ( r_max, r_res) = Self :: dfs ( nums, j + 1 , num) ;
16
+ if r_max >= max {
17
+ if r_max > max {
18
+ max = r_max;
19
+ res = 0 ;
20
+ }
21
+ res += r_res;
22
+ }
23
+ }
24
+ ( max, res)
25
+ }
26
+
27
+ pub fn count_max_or_subsets ( nums : Vec < i32 > ) -> i32 {
28
+ Self :: dfs ( & nums, 0 , 0 ) . 1
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments