File tree Expand file tree Collapse file tree 12 files changed +381
-384
lines changed
solution/2400-2499/2403.Minimum Time to Kill All Monsters Expand file tree Collapse file tree 12 files changed +381
-384
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1
- using ll = long long ;
2
-
3
1
class Solution {
4
2
public:
5
- vector<ll> f;
6
- vector<int > power;
7
- int n;
8
-
9
3
long long minimumTime (vector<int >& power) {
10
- n = power.size ();
11
- f.assign (1 << n, -1 );
12
- this ->power = power;
13
- return dfs (0 );
4
+ int n = power.size ();
5
+ long long f[1 << n];
6
+ memset (f, -1 , sizeof (f));
7
+ auto dfs = [&](auto && dfs, int mask) -> long long {
8
+ if (mask == 0 ) {
9
+ return 0 ;
10
+ }
11
+ if (f[mask] != -1 ) {
12
+ return f[mask];
13
+ }
14
+ f[mask] = LLONG_MAX;
15
+ int gain = 1 + (n - __builtin_popcount (mask));
16
+ for (int i = 0 ; i < n; ++i) {
17
+ if (mask >> i & 1 ) {
18
+ f[mask] = min (f[mask], dfs (dfs, mask ^ (1 << i)) + (power[i] + gain - 1 ) / gain);
19
+ }
20
+ }
21
+ return f[mask];
22
+ };
23
+ return dfs (dfs, (1 << n) - 1 );
14
24
}
15
-
16
- ll dfs (int mask) {
17
- if (f[mask] != -1 ) return f[mask];
18
- int cnt = __builtin_popcount (mask);
19
- if (cnt == n) return 0 ;
20
- ll ans = LONG_MAX;
21
- for (int i = 0 ; i < n; ++i) {
22
- if ((mask >> i) & 1 ) continue ;
23
- ans = min (ans, dfs (mask | 1 << i) + (power[i] + cnt) / (cnt + 1 ));
24
- }
25
- f[mask] = ans;
26
- return ans;
27
- }
28
- };
25
+ };
Original file line number Diff line number Diff line change @@ -6,22 +6,20 @@ func minimumTime(power []int) int64 {
6
6
}
7
7
var dfs func (mask int ) int64
8
8
dfs = func (mask int ) int64 {
9
+ if mask == 0 {
10
+ return 0
11
+ }
9
12
if f [mask ] != - 1 {
10
13
return f [mask ]
11
14
}
12
- cnt := bits .OnesCount (uint (mask ))
13
- if cnt == n {
14
- return 0
15
- }
16
- var ans int64 = math .MaxInt64
17
- for i , v := range power {
18
- if (mask >> i & 1 ) == 1 {
19
- continue
15
+ f [mask ] = 1e18
16
+ gain := 1 + (n - bits .OnesCount (uint (mask )))
17
+ for i , x := range power {
18
+ if mask >> i & 1 == 1 {
19
+ f [mask ] = min (f [mask ], dfs (mask ^ (1 << i ))+ int64 (x + gain - 1 )/ int64 (gain ))
20
20
}
21
- ans = min (ans , dfs (mask | 1 << i )+ int64 ((v + cnt )/ (cnt + 1 )))
22
21
}
23
- f [mask ] = ans
24
- return ans
22
+ return f [mask ]
25
23
}
26
- return dfs (0 )
27
- }
24
+ return dfs (1 << n - 1 )
25
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
private int n ;
3
- private long [] f ;
4
3
private int [] power ;
4
+ private Long [] f ;
5
5
6
6
public long minimumTime (int [] power ) {
7
7
n = power .length ;
8
- f = new long [1 << n ];
9
- Arrays .fill (f , -1 );
10
8
this .power = power ;
11
- return dfs (0 );
9
+ f = new Long [1 << n ];
10
+ return dfs ((1 << n ) - 1 );
12
11
}
13
12
14
13
private long dfs (int mask ) {
15
- if (f [mask ] != -1 ) {
16
- return f [mask ];
17
- }
18
- int cnt = Integer .bitCount (mask );
19
- if (cnt == n ) {
14
+ if (mask == 0 ) {
20
15
return 0 ;
21
16
}
22
- long ans = Long .MAX_VALUE ;
17
+ if (f [mask ] != null ) {
18
+ return f [mask ];
19
+ }
20
+ f [mask ] = Long .MAX_VALUE ;
21
+ int gain = 1 + (n - Integer .bitCount (mask ));
23
22
for (int i = 0 ; i < n ; ++i ) {
24
- if ((( mask >> i ) & 1 ) == 1 ) {
25
- continue ;
23
+ if ((mask >> i & 1 ) == 1 ) {
24
+ f [ mask ] = Math . min ( f [ mask ], dfs ( mask ^ 1 << i ) + ( power [ i ] + gain - 1 ) / gain ) ;
26
25
}
27
- ans = Math .min (ans , dfs (mask | 1 << i ) + (power [i ] + cnt ) / (cnt + 1 ));
28
26
}
29
- f [mask ] = ans ;
30
- return ans ;
27
+ return f [mask ];
31
28
}
32
- }
29
+ }
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def minimumTime (self , power : List [int ]) -> int :
3
3
@cache
4
- def dfs (mask ):
5
- cnt = mask .bit_count ()
6
- if cnt == len (power ):
4
+ def dfs (mask : int ) -> int :
5
+ if mask == 0 :
7
6
return 0
8
7
ans = inf
9
- for i , v in enumerate ( power ):
10
- if mask & ( 1 << i ):
11
- continue
12
- ans = min (ans , dfs (mask | 1 << i ) + (v + cnt ) // ( cnt + 1 ) )
8
+ gain = 1 + ( n - mask . bit_count ())
9
+ for i , x in enumerate ( power ):
10
+ if mask >> i & 1 :
11
+ ans = min (ans , dfs (mask ^ ( 1 << i )) + (x + gain - 1 ) // gain )
13
12
return ans
14
13
15
- return dfs (0 )
14
+ n = len (power )
15
+ return dfs ((1 << n ) - 1 )
Original file line number Diff line number Diff line change 1
1
function minimumTime ( power : number [ ] ) : number {
2
2
const n = power . length ;
3
- const f = new Array ( 1 << n ) . fill ( - 1 ) ;
4
- function dfs ( mask ) {
5
- if ( f [ mask ] != - 1 ) {
6
- return f [ mask ] ;
7
- }
8
- const cnt = bitCount ( mask ) ;
9
- if ( cnt == n ) {
3
+ const f : number [ ] = Array ( 1 << n ) . fill ( - 1 ) ;
4
+ const dfs = ( mask : number ) : number => {
5
+ if ( mask === 0 ) {
10
6
return 0 ;
11
7
}
12
- let ans = Infinity ;
8
+ if ( f [ mask ] !== - 1 ) {
9
+ return f [ mask ] ;
10
+ }
11
+ f [ mask ] = Infinity ;
12
+ const gain = 1 + ( n - bitCount ( mask ) ) ;
13
13
for ( let i = 0 ; i < n ; ++ i ) {
14
14
if ( ( mask >> i ) & 1 ) {
15
- continue ;
15
+ f [ mask ] = Math . min ( f [ mask ] , dfs ( mask ^ ( 1 << i ) ) + Math . ceil ( power [ i ] / gain ) ) ;
16
16
}
17
- ans = Math . min ( ans , dfs ( mask | ( 1 << i ) ) + Math . ceil ( power [ i ] / ( cnt + 1 ) ) ) ;
18
17
}
19
- f [ mask ] = ans ;
20
- return ans ;
21
- }
22
- return dfs ( 0 ) ;
18
+ return f [ mask ] ;
19
+ } ;
20
+ return dfs ( ( 1 << n ) - 1 ) ;
23
21
}
24
22
25
- function bitCount ( x ) {
26
- let cnt = 0 ;
27
- for ( let i = 0 ; i < 32 ; ++ i ) {
28
- if ( ( x >> i ) & 1 ) {
29
- ++ cnt ;
30
- }
31
- }
32
- return cnt ;
23
+ function bitCount ( i : number ) : number {
24
+ i = i - ( ( i >>> 1 ) & 0x55555555 ) ;
25
+ i = ( i & 0x33333333 ) + ( ( i >>> 2 ) & 0x33333333 ) ;
26
+ i = ( i + ( i >>> 4 ) ) & 0x0f0f0f0f ;
27
+ i = i + ( i >>> 8 ) ;
28
+ i = i + ( i >>> 16 ) ;
29
+ return i & 0x3f ;
33
30
}
Original file line number Diff line number Diff line change @@ -2,16 +2,17 @@ class Solution {
2
2
public:
3
3
long long minimumTime (vector<int >& power) {
4
4
int n = power.size ();
5
- vector<long long > dp (1 << n, LONG_MAX);
6
- dp[0 ] = 0 ;
5
+ long long f[1 << n];
6
+ memset (f, 0x3f , sizeof (f));
7
+ f[0 ] = 0 ;
7
8
for (int mask = 1 ; mask < 1 << n; ++mask) {
8
- int cnt = __builtin_popcount (mask);
9
+ int gain = __builtin_popcount (mask);
9
10
for (int i = 0 ; i < n; ++i) {
10
- if (( mask >> i) & 1 ) {
11
- dp [mask] = min (dp [mask], dp [mask ^ (1 << i)] + (power[i] + cnt - 1 ) / cnt );
11
+ if (mask >> i & 1 ) {
12
+ f [mask] = min (f [mask], f [mask ^ (1 << i)] + (power[i] + gain - 1 ) / gain );
12
13
}
13
14
}
14
15
}
15
- return dp [(1 << n) - 1 ];
16
+ return f [(1 << n) - 1 ];
16
17
}
17
- };
18
+ };
Original file line number Diff line number Diff line change 1
1
func minimumTime (power []int ) int64 {
2
2
n := len (power )
3
- dp := make ([]int64 , 1 << n )
4
- for i := range dp {
5
- dp [i ] = math . MaxInt64
3
+ f := make ([]int64 , 1 << n )
4
+ for i := range f {
5
+ f [i ] = 1e18
6
6
}
7
- dp [0 ] = 0
7
+ f [0 ] = 0
8
8
for mask := 1 ; mask < 1 << n ; mask ++ {
9
- cnt := bits .OnesCount (uint (mask ))
10
- for i , v := range power {
11
- if (( mask >> i ) & 1 ) == 1 {
12
- dp [mask ] = min (dp [mask ], dp [mask ^ (1 << i )]+ int64 (( v + cnt - 1 )/ cnt ))
9
+ gain := bits .OnesCount (uint (mask ))
10
+ for i , x := range power {
11
+ if mask >> i & 1 == 1 {
12
+ f [mask ] = min (f [mask ], f [mask ^ (1 << i )]+ int64 (x + gain - 1 )/ int64 ( gain ))
13
13
}
14
14
}
15
15
}
16
- return dp [ len ( dp ) - 1 ]
17
- }
16
+ return f [ 1 << n - 1 ]
17
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public long minimumTime (int [] power ) {
3
3
int n = power .length ;
4
- long [] dp = new long [1 << n ];
5
- Arrays .fill (dp , Long .MAX_VALUE );
6
- dp [0 ] = 0 ;
4
+ long [] f = new long [1 << n ];
5
+ Arrays .fill (f , Long .MAX_VALUE );
6
+ f [0 ] = 0 ;
7
7
for (int mask = 1 ; mask < 1 << n ; ++mask ) {
8
- int cnt = Integer .bitCount (mask );
8
+ int gain = Integer .bitCount (mask );
9
9
for (int i = 0 ; i < n ; ++i ) {
10
- if ((( mask >> i ) & 1 ) == 1 ) {
11
- dp [mask ] = Math .min (dp [mask ], dp [mask ^ ( 1 << i ) ] + (power [i ] + cnt - 1 ) / cnt );
10
+ if ((mask >> i & 1 ) == 1 ) {
11
+ f [mask ] = Math .min (f [mask ], f [mask ^ 1 << i ] + (power [i ] + gain - 1 ) / gain );
12
12
}
13
13
}
14
14
}
15
- return dp [(1 << n ) - 1 ];
15
+ return f [(1 << n ) - 1 ];
16
16
}
17
- }
17
+ }
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def minimumTime (self , power : List [int ]) -> int :
3
3
n = len (power )
4
- dp = [inf ] * (1 << n )
5
- dp [0 ] = 0
4
+ f = [inf ] * (1 << n )
5
+ f [0 ] = 0
6
6
for mask in range (1 , 1 << n ):
7
- cnt = mask .bit_count ()
8
- for i , v in enumerate (power ):
9
- if ( mask >> i ) & 1 :
10
- dp [mask ] = min (dp [mask ], dp [mask ^ (1 << i )] + (v + cnt - 1 ) // cnt )
11
- return dp [- 1 ]
7
+ gain = mask .bit_count ()
8
+ for i , x in enumerate (power ):
9
+ if mask >> i & 1 :
10
+ f [mask ] = min (f [mask ], f [mask ^ (1 << i )] + (x + gain - 1 ) // gain )
11
+ return f [- 1 ]
Original file line number Diff line number Diff line change 1
1
function minimumTime ( power : number [ ] ) : number {
2
2
const n = power . length ;
3
- const dp = new Array ( 1 << n ) . fill ( Infinity ) ;
4
- dp [ 0 ] = 0 ;
3
+ const f : number [ ] = Array ( 1 << n ) . fill ( Infinity ) ;
4
+ f [ 0 ] = 0 ;
5
5
for ( let mask = 1 ; mask < 1 << n ; ++ mask ) {
6
- const cnt = bitCount ( mask ) ;
6
+ const gain = bitCount ( mask ) ;
7
7
for ( let i = 0 ; i < n ; ++ i ) {
8
8
if ( ( mask >> i ) & 1 ) {
9
- dp [ mask ] = Math . min ( dp [ mask ] , dp [ mask ^ ( 1 << i ) ] + Math . ceil ( power [ i ] / cnt ) ) ;
9
+ f [ mask ] = Math . min ( f [ mask ] , f [ mask ^ ( 1 << i ) ] + Math . ceil ( power [ i ] / gain ) ) ;
10
10
}
11
11
}
12
12
}
13
- return dp [ dp . length - 1 ] ;
13
+ return f . at ( - 1 ) ! ;
14
14
}
15
15
16
- function bitCount ( x ) {
17
- let cnt = 0 ;
18
- for ( let i = 0 ; i < 32 ; ++ i ) {
19
- if ( ( x >> i ) & 1 ) {
20
- ++ cnt ;
21
- }
22
- }
23
- return cnt ;
16
+ function bitCount ( i : number ) : number {
17
+ i = i - ( ( i >>> 1 ) & 0x55555555 ) ;
18
+ i = ( i & 0x33333333 ) + ( ( i >>> 2 ) & 0x33333333 ) ;
19
+ i = ( i + ( i >>> 4 ) ) & 0x0f0f0f0f ;
20
+ i = i + ( i >>> 8 ) ;
21
+ i = i + ( i >>> 16 ) ;
22
+ return i & 0x3f ;
24
23
}
You can’t perform that action at this time.
0 commit comments