File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } req_skills
3
+ * @param {string[][] } people
4
+ * @return {number[] }
5
+ */
6
+ var smallestSufficientTeam = function ( req_skills , people ) {
7
+ const n = req_skills . length , m = people . length
8
+ const limit = 1 << n
9
+ const reqSet = new Set ( req_skills )
10
+ const si = { }
11
+ for ( let i = 0 ; i < n ; i ++ ) si [ req_skills [ i ] ] = i
12
+ const ps = { }
13
+ for ( let i = 0 ; i < m ; i ++ ) {
14
+ const p = people [ i ]
15
+ let mask = 0
16
+ for ( const s of p ) {
17
+ if ( ! reqSet . has ( s ) ) continue
18
+ mask |= ( 1 << si [ s ] )
19
+ }
20
+ ps [ i ] = mask
21
+ }
22
+ const res = Array . from ( { length : limit } , ( ) => new Array ( ) )
23
+ let dp = Array ( limit ) . fill ( Infinity )
24
+ dp [ 0 ] = 0
25
+ for ( let i = 0 ; i < m ; i ++ ) {
26
+ const pMask = ps [ i ]
27
+ // const dp2 = [...dp]
28
+ for ( let mask = 0 ; mask < limit ; mask ++ ) {
29
+ const newMask = mask | pMask
30
+ if ( dp [ newMask ] > dp [ mask ] + 1 ) {
31
+ dp [ newMask ] = dp [ mask ] + 1
32
+ res [ newMask ] = [ ...res [ mask ] ]
33
+ res [ newMask ] . push ( i )
34
+ }
35
+ }
36
+ // dp = dp2
37
+ }
38
+
39
+ return res [ limit - 1 ]
40
+ } ;
41
+
42
+ // another
43
+
1
44
/**
2
45
* @param {string[] } req_skills
3
46
* @param {string[][] } people
You can’t perform that action at this time.
0 commit comments