File tree Expand file tree Collapse file tree 1 file changed +13
-17
lines changed
Expand file tree Collapse file tree 1 file changed +13
-17
lines changed Original file line number Diff line number Diff line change 11package class086 ;
22
33import java .util .Arrays ;
4+ import java .util .HashMap ;
45import java .util .List ;
56
67// 最小的必要团队
1415// 测试链接 : https://leetcode.cn/problems/smallest-sufficient-team/
1516public class Code02_SmallestSufficientTeam {
1617
17- // 讲解080、081 - 状压dp
1818 public static int [] smallestSufficientTeam (String [] skills , List <List <String >> people ) {
1919 Arrays .sort (skills );
2020 int n = skills .length ;
2121 int m = people .size ();
22+ HashMap <String , Integer > map = new HashMap <>();
23+ int cnt = 0 ;
24+ for (String s : skills ) {
25+ if (!map .containsKey (s )) {
26+ map .put (s , cnt ++);
27+ }
28+ }
2229 int [] arr = new int [m ];
23- for (int i = 0 ; i < m ; i ++) {
24- int status = 0 ;
25- List <String > skill = people .get (i );
26- skill .sort ((a , b ) -> a .compareTo (b ));
27- int p1 = 0 ;
28- int p2 = 0 ;
29- while (p1 < n && p2 < skill .size ()) {
30- int compare = skills [p1 ].compareTo (skill .get (p2 ));
31- if (compare < 0 ) {
32- p1 ++;
33- } else if (compare > 0 ) {
34- p2 ++;
35- } else {
36- status |= 1 << p1 ;
37- p1 ++;
38- p2 ++;
30+ for (int i = 0 , status ; i < m ; i ++) {
31+ status = 0 ;
32+ for (String s : people .get (i )) {
33+ if (map .containsKey (s )) {
34+ status |= 1 << map .get (s );
3935 }
4036 }
4137 arr [i ] = status ;
You can’t perform that action at this time.
0 commit comments