Last active
September 23, 2017 20:41
-
-
Save kmurudi/da012e67dc25add8e4b38b98713995db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Solution { | |
| public static boolean canWin(int leap, int[] game) { | |
| // Return true if you can win the game; otherwise, return false. | |
| int n = game.length; | |
| boolean[] boolist = new boolean[n]; | |
| boolean result = false; | |
| int n1 = 0; | |
| for (int index = 0; index<game.length; index++){ | |
| int var1; | |
| int var2; | |
| int var3; | |
| int var4; | |
| if(game[index+1]==0){ | |
| var1 = index+1; | |
| } | |
| if(game[index-1]==0){ | |
| var2 = index-1; | |
| } | |
| if(game[index+leap]==0 || index+leap>=n){ | |
| var3 = index+leap; | |
| } | |
| if(index==n-1){ | |
| var4 = index; | |
| } | |
| //var2 = move_back(game, index); | |
| //var3 = move_leap(game,index); | |
| //var4 = last_ele(index); | |
| boolist[index] = check(var1, var2, var3, var4); | |
| } | |
| // TODO What is this loop doing? | |
| // By default all values of boolist is false - after the above for loop - will change no boolist values | |
| // This is wrong. Ur not taking game array into account | |
| while(n1 != boolist.length){ | |
| result = result && boolist[n1]; | |
| n1++; | |
| } | |
| // TODO Why are u returning result ? | |
| // This block will return result. | |
| if (result == true){ | |
| return true; | |
| } | |
| else | |
| return false; | |
| //TODO Where does this method end?? | |
| //TODO u cannot define method inside another method - all these not needed, doing ops directly. | |
| /* | |
| int move_fwd(int[] game,int i){ | |
| if(game[i+1]==0){ | |
| return i+1; | |
| } | |
| } | |
| int move_back(int[] game, int i){ | |
| if(game[i-1]==0){ | |
| return i-1; | |
| } | |
| } | |
| int move_leap(int [] game, int i){ | |
| if(game[i+leap]==0 || i+leap>=n){ | |
| return i+leap; | |
| } | |
| } | |
| int last_ele(int i){ | |
| if(i==n-1){ | |
| return i; | |
| } | |
| } */ | |
| // Code would never reach here. | |
| // Since ur already returning in the beginning | |
| // Also, This method should be outside. | |
| } | |
| // how can I make n accessible here? or how to give 'n' inside check method | |
| static boolean check(int v1, int v2, int v3, int v4){ | |
| if(v1 >= n || v2 >=n || v3 >= n || v4 == n-1){ | |
| return true; | |
| } | |
| else | |
| return false; | |
| } | |
| 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]; | |
| for (int i = 0; i < n; i++) { | |
| game[i] = scan.nextInt(); | |
| } | |
| System.out.println( (canWin(leap, game)) ? "YES" : "NO" ); | |
| } | |
| scan.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment