File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } stickers
3
+ * @param {string } target
4
+ * @return {number }
5
+ */
6
+ const minStickers = function ( stickers , target ) {
7
+ const n = target . length
8
+ const limit = 1 << n
9
+ const dp = Array ( limit ) . fill ( Infinity )
10
+ dp [ 0 ] = 0
11
+ for ( let state = 0 ; state < limit ; state ++ ) {
12
+ if ( dp [ state ] === Infinity ) continue
13
+ for ( let s of stickers ) {
14
+ const ns = helper ( state , target , s )
15
+ dp [ ns ] = Math . min ( dp [ ns ] , dp [ state ] + 1 )
16
+ }
17
+
18
+ }
19
+
20
+ return dp [ limit - 1 ] === Infinity ? - 1 : dp [ limit - 1 ]
21
+
22
+ function helper ( state , target , s ) {
23
+ const n = target . length
24
+ for ( const ch of s ) {
25
+ for ( let i = 0 ; i < n ; i ++ ) {
26
+ if ( target [ i ] === ch && ( ( state >> i ) & 1 ) === 0 ) {
27
+ state |= ( 1 << i )
28
+ break
29
+ }
30
+ }
31
+ }
32
+
33
+ return state
34
+ }
35
+ } ;
36
+
37
+ // another
38
+
39
+
1
40
/**
2
41
* @param {string[] } stickers
3
42
* @param {string } target
You can’t perform that action at this time.
0 commit comments