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.
reposting a solution from @kdubey2181, that I think is the most elegant and simple way to solve this problem.
importjava.util.*;publicclassSolution{publicstaticbooleancanWin(intleap,int[]game,intposition){intlastIndex=game.length-1;//did you win by jumping over the last index?if(position>lastIndex){returntrue;}//did you reverse out of bounds or meet a obstacle?if(position<0||game[position]==1){returnfalse;}//mark current position as exploredgame[position]=1;//Explore all possible solutions from current positionreturncanWin(leap,game,position+1)||canWin(leap,game,position-1)||canWin(leap,game,position+leap);}publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);intq=scan.nextInt();while(q-->0){intn=scan.nextInt();intleap=scan.nextInt();int[]game=newint[n];for(inti=0;i<n;i++){game[i]=scan.nextInt();}System.out.println((canWin(leap,game,0))?"YES":"NO");}scan.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java 1D Array (Part 2)
You are viewing a single comment's thread. Return to all comments →
reposting a solution from @kdubey2181, that I think is the most elegant and simple way to solve this problem.