Created
June 13, 2017 04:57
-
-
Save hoanbka/061c1f533d89a94cb8f8676eaa7c4b99 to your computer and use it in GitHub Desktop.
bomber - Google Interview
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
package code_fight; | |
/** | |
* Created by Hoan Nguyen on 6/10/2017. | |
* https://codefights.com/interview/d8oFgW9dnFgTrfhsX/companies/N3sScnJbzdPDQaHPj | |
*/ | |
public class bomber { | |
static int bomber(char[][] field) { | |
int max = 0; | |
if (field.length == 0) { | |
return 0; | |
} | |
for (int i = 0; i < field.length; i++) { | |
for (int j = 0; j < field[0].length; j++) { | |
int cnt = 0; | |
int k1 = 0, k2 = 0, k3 = 0, k4 = 0; | |
if (field[i][j] == '0') { | |
while (i - k1 >= 0) { | |
if (field[i - k1][j] == 'E') { | |
cnt++; | |
} else if (field[i - k1][j] == 'W') { | |
break; | |
} | |
k1++; | |
} | |
while (j - k2 >= 0) { | |
if (field[i][j - k2] == 'E') { | |
cnt++; | |
} else if (field[i][j - k2] == 'W') { | |
break; | |
} | |
k2++; | |
} | |
while (i + k3 < field.length) { | |
if (field[i + k3][j] == 'E') { | |
cnt++; | |
} else if (field[i + k3][j] == 'W') { | |
break; | |
} | |
k3++; | |
} | |
while (j + k4 < field[0].length) { | |
if (field[i][j + k4] == 'E') { | |
cnt++; | |
} else if (field[i][j + k4] == 'W') { | |
break; | |
} | |
k4++; | |
} | |
} | |
System.out.println("cnt= " + cnt + " @ " + "i: " + i + " j: " + j); | |
max = Math.max(max, cnt); | |
} | |
} | |
return max; | |
} | |
public static void main(String[] args) { | |
char[][] field = | |
{{'0', '0', 'E', '0'}, | |
{'W', '0', 'W', 'E'}, | |
{'0', 'E', '0', 'W'}, | |
{'0', 'W', '0', 'E'}}; | |
/*char [][] field = {{'E'}, | |
{'E'}, | |
{'E'}};*/ | |
/* char[][] field = | |
{{'0', 'E', '0', '0'}, | |
{'E', '0', 'W', 'E'}, | |
{'0', 'E', '0', '0'}};*/ | |
System.out.println(bomber(field)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment