【英雄算法六月集训】Day16
class Solution {
int [ ] [ ] list = new int [ ] [ ] {
{ 0 , 1 } , { 0 , 2 } , { 1 , 2 } , { 0 , 3 }
} ;
List < List < String > > ans= new ArrayList < List < String > > ( ) ;
{
ans. add ( new ArrayList < String > ( ) ) ;
ans. add ( new ArrayList < String > ( ) ) ;
ans. add ( new ArrayList < String > ( ) ) ;
ans. add ( new ArrayList < String > ( ) ) ;
ans. add ( new ArrayList < String > ( ) ) ;
}
Map < String , Integer > hash= new HashMap < String , Integer > ( ) ;
String gen ( String ori, int start, int step) {
char [ ] s = ori. toCharArray ( ) ;
int i;
for ( i= start; i < s. length; i+= step) {
s[ i] = ( char ) ( '1' - ( s[ i] - '0' ) ) ;
}
return new String ( s) ;
}
void dfs ( int dep, String s) {
ans. get ( dep) . add ( s) ;
if ( dep>= 4 ) {
return ;
}
for ( int i= 0 ; i< 4 ; ++ i) {
dfs ( dep+ 1 , gen ( s, list[ i] [ 0 ] , list[ i] [ 1 ] ) ) ;
}
}
public int flipLights ( int n, int presses) {
int i;
StringBuilder start= new StringBuilder ( ) ;
for ( i= 0 ; i< n; ++ i) {
start. append ( '0' ) ;
}
dfs ( 0 , start. toString ( ) ) ;
if ( presses> 4 ) {
if ( ( presses& 1 ) == 1 ) {
presses= 3 ;
} else {
presses= 4 ;
}
}
for ( i= 0 ; i< ans. get ( presses) . size ( ) ; ++ i) {
hash. put ( ans. get ( presses) . get ( i) , 1 ) ;
}
return hash. size ( ) ;
}
}
class Solution {
public int minimumOperations ( int [ ] nums, int start, int goal) {
int maxn= 1010 ;
LinkedList < Integer > q= new LinkedList < Integer > ( ) ;
int [ ] step = new int [ maxn] ;
for ( int a= 0 ; a< maxn; ++ a) step[ a] = - 1 ;
int x, nextx, i;
q. push ( start) ;
step[ start] = 0 ;
while ( ! q. isEmpty ( ) ) {
x= q. peekFirst ( ) ;
q. pop ( ) ;
for ( i = 0 ; i < nums. length; ++ i) {
nextx = x+ nums[ i] ;
if ( nextx == goal) {
return step[ x] + 1 ;
}
if ( nextx<= 1000 && nextx>= 0 ) {
if ( step[ nextx] == - 1 ) {
step[ nextx] = step[ x] + 1 ;
q. add ( nextx) ;
}
}
nextx = x- nums[ i] ;
if ( nextx == goal) {
return step[ x] + 1 ;
}
if ( nextx<= 1000 && nextx>= 0 ) {
if ( step[ nextx] == - 1 ) {
step[ nextx] = step[ x] + 1 ;
q. add ( nextx) ;
}
}
nextx = ( x^ nums[ i] ) ;
if ( nextx == goal) {
return step[ x] + 1 ;
}
if ( nextx<= 1000 && nextx>= 0 ) {
if ( step[ nextx] == - 1 ) {
step[ nextx] = step[ x] + 1 ;
q. add ( nextx) ;
}
}
}
}
return - 1 ;
}
}
class Solution {
int theImportance;
Map < Integer , Employee > id2Map= new HashMap < Integer , Employee > ( ) ;
int dfs ( int id) {
Employee employee = id2Map. get ( id) ;
if ( employee == null ) {
return 0 ;
}
int sum = employee. importance;
for ( Integer eId: employee. subordinates) {
sum+= dfs ( eId) ;
}
return sum;
}
public int getImportance ( List < Employee > employees, int id) {
int i= 0 ;
for ( Employee e: employees) {
id2Map. put ( e. id, e) ;
}
return dfs ( id) ;
}
}