You are viewing a single comment's thread. Return to all comments →
import java.util.*; public class Solution { public static boolean canWin(int n,int leap, int[] game) { int i=0,ptr=0; while(i<n){ if(i+leap<=n-1 && game[i+leap]==0 && leap!=0) i=i+leap; else if(i+1<=n-1 && game[i+1]==0) i=i+1; else if(i-1>=0 && game[i-1]==0) i=i-1; else return false; game[i]=1; if(i==n-1 || i+leap>=n) return true; } return true; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int q = scan.nextInt(); while (q-- > 0) { int n = scan.nextInt(); int leap = scan.nextInt(); int[] game = new int[n+leap]; for (int i = 0; i < n; i++) { game[i] = scan.nextInt(); } System.out.println( (canWin(n,leap, game)) ? "YES" : "NO" ); } scan.close(); } }
This code passes 5 TCs. I've tried it without using recursion.Please help me complete this code.
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 →
This code passes 5 TCs. I've tried it without using recursion.Please help me complete this code.