We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// grid char constconstGOOD='G',BAD='B',PLUSED='P';// replace string char by indexfunctionreplaceCharAt(str:string,index:number,char:string):string{if(index<0||index>=str.length){thrownewError("Index out of range");}returnstr.substr(0,index)+char+str.substr(index+1);}// select 1 coordinate, let pluses grow, return pluses size and new grid that pluses already growedfunctiongetPluses(grid:string[],startPosition:number[]):[number,string[]][]{consty=startPosition[0]constx=startPosition[1]letplused_grid=[...grid];letsize=0;plused_grid[y]=replaceCharAt(plused_grid[y],x,PLUSED)letresult:[number,string[]][]=[[size,[...plused_grid]]]while(true){try{if(plused_grid[y-(size+1)][x]!=GOOD)break;if(plused_grid[y+(size+1)][x]!=GOOD)break;if(plused_grid[y][x-(size+1)]!=GOOD)break;if(plused_grid[y][x+(size+1)]!=GOOD)break;plused_grid[y-(size+1)]=replaceCharAt(plused_grid[y-(size+1)],x,PLUSED)plused_grid[y]=replaceCharAt(plused_grid[y],x-(size+1),PLUSED)plused_grid[y]=replaceCharAt(plused_grid[y],x+(size+1),PLUSED)plused_grid[y+(size+1)]=replaceCharAt(plused_grid[y+(size+1)],x,PLUSED)size++;result.push([size,[...plused_grid]])}catch(e){break;}}returnresult;}functiontwoPluses(grid:string[]):number{// Write your code here// store result of this problemletmaxTotal=0;// loop the grid, find GOOD coordinate, mean it is not in BAD or PLUSEDfor(lety1=0;y1<grid.length;y1++){for(letx1=0;x1<grid[y1].length;x1++){letcell=grid[y1][x1];if(cell!=GOOD)continue;// got 1st pluses, try 2nd loop on a grid that placed 1st plusesgetPluses(grid,[y1,x1]).forEach(([size_1,grid_1])=>{for(lety2=0;y2<grid_1.length;y2++){for(letx2=0;x2<grid_1[y2].length;x2++){letcell=grid_1[y2][x2];if(cell!=GOOD)continue;// got 2nd pluses, calculate size of 2 pluses, get the largergetPluses(grid_1,[y2,x2]).forEach(([size_2,grid_2])=>{// size is the wing length, so total is: // size * 4 (wings) + 1 (intersection point)maxTotal=Math.max((size_1*4+1)*(size_2*4+1),maxTotal);})}}})}}// donereturnmaxTotal}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ema's Supercomputer
You are viewing a single comment's thread. Return to all comments →
My answer in Typescript, explain in comment