File tree 1 file changed +56
-0
lines changed
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {character[][] } seats
3
+ * @return {number }
4
+ */
5
+ const maxStudents = function ( seats ) {
6
+ const m = seats . length , n = seats [ 0 ] . length , limit = 1 << n
7
+ const dp = Array . from ( { length : m + 1 } , ( ) => Array ( limit ) . fill ( 0 ) )
8
+
9
+ let res = 0
10
+ for ( let i = 1 ; i <= m ; i ++ ) {
11
+ for ( let mask = 0 ; mask < limit ; mask ++ ) {
12
+ let valid = true
13
+ for ( let j = 0 ; j < n ; j ++ ) {
14
+ if ( seats [ i - 1 ] [ j ] === '#' && ( ( mask >> j ) & 1 ) ) {
15
+ valid = false
16
+ break
17
+ }
18
+ if ( j < n - 1 && ( ( mask >> j ) & 1 ) && ( ( mask >> ( j + 1 ) ) & 1 ) ) {
19
+ valid = false
20
+ break
21
+ }
22
+ }
23
+
24
+ if ( ! valid ) {
25
+ dp [ i ] [ mask ] = - 1
26
+ continue
27
+ }
28
+
29
+ for ( let pre = 0 ; pre < limit ; pre ++ ) {
30
+ if ( dp [ i - 1 ] [ pre ] === - 1 ) continue
31
+ if ( ( pre & ( mask >> 1 ) ) !== 0 || ( pre & ( mask << 1 ) ) !== 0 ) continue
32
+ dp [ i ] [ mask ] = Math . max ( dp [ i ] [ mask ] , dp [ i - 1 ] [ pre ] )
33
+ }
34
+
35
+ dp [ i ] [ mask ] += bitCnt ( mask )
36
+
37
+ res = Math . max ( res , dp [ i ] [ mask ] )
38
+ }
39
+ }
40
+
41
+ return res
42
+
43
+ function bitCnt ( num ) {
44
+ let res = 0
45
+ while ( num ) {
46
+ if ( num & 1 ) res ++
47
+ num = num >> 1
48
+ }
49
+
50
+ return res
51
+ }
52
+ } ;
53
+
54
+ // another
55
+
56
+
1
57
/**
2
58
* @param {character[][] } seats
3
59
* @return {number }
You can’t perform that action at this time.
0 commit comments