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.
My Java solution with linear time and space complexity
publicstaticStringgameOfStones(intn){// goal: print the name of the winnerif(n==1)return"Second";boolean[]moves=newboolean[n+1];moves[0]=false;for(inti=1;i<=n;i++){booleancanWin=false;if(i>=2&&!moves[i-2])canWin=true;if(i>=3&&!moves[i-3])canWin=true;if(i>=5&&!moves[i-5])canWin=true;moves[i]=canWin;}returnmoves[n]?"First":"Second";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Game of Stones
You are viewing a single comment's thread. Return to all comments →
My Java solution with linear time and space complexity