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.
publicstaticbooleancanWin(intleap,int[]game){returnisSolvable(leap,game,0);}privatestaticbooleanisSolvable(intleap,int[]game,inti){// Base Casesif(i>=game.length){returntrue;}elseif(i<0||game[i]==1){returnfalse;}game[i]=1;// marks as visited// Recursive CasesreturnisSolvable(leap,game,i+leap)||isSolvable(leap,game,i+1)||isSolvable(leap,game,i-1);}
Java 1D Array (Part 2)
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
Solution is basically to do a depth-first search (DFS). Instead of creating a "visited" array, we can mark each array value as 1 when visiting it.
Full code available at my HackerRank solutions. Here is the main snippet:
Let me know if you have any questions.