Skip to content

Commit 6b77df2

Browse files
Merge remote-tracking branch 'origin/master'
2 parents ffaa9ec + adc7c15 commit 6b77df2

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

problems/src/Minesweeper.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Created by pradhang on 3/28/2017.
3+
* Accepted
4+
*/
5+
public class Minesweeper
6+
{
7+
private static final int[] R = {1, 1, 1, 0, 0, -1, -1, -1};
8+
private static final int[] C = {-1, 0, 1, -1, 1, -1, 0, 1};
9+
/**
10+
* Main method
11+
* @param args
12+
* @throws Exception
13+
*/
14+
public static void main(String[] args) throws Exception
15+
{
16+
char[][] board = {{'E','E','E','E','E'},{'E','E','M','E','E'},{'E','E','E','E','E'},{'E','E','E','E','E'}};
17+
int[] click = {3, 0};
18+
new Minesweeper().updateBoard(board, click);
19+
for (int i = 0; i < board.length; i ++)
20+
System.out.println(board[i]);
21+
}
22+
23+
public char[][] updateBoard(char[][] board, int[] click)
24+
{
25+
int r = click[0];
26+
int c = click[1];
27+
dfs(board, r, c);
28+
return board;
29+
}
30+
31+
private void dfs(char[][] board, int r, int c)
32+
{
33+
if(board[r][c] == 'M')
34+
{
35+
board[r][c] = 'X';
36+
}
37+
else
38+
{
39+
int mineCount = 0;
40+
for(int i = 0; i < 8; i ++)
41+
{
42+
int newR = r + R[i];
43+
int newC = c + C[i];
44+
if(newR >=0 && newC >= 0 && newR < board.length && newC < board[0].length &&
45+
board[newR][newC] == 'M') //boundary check
46+
mineCount++;
47+
}
48+
if(mineCount > 0)
49+
board[r][c] = (char)(mineCount + '0');
50+
else
51+
{
52+
board[r][c] = 'B';
53+
for(int i = 0; i < 8; i ++)
54+
{
55+
int newR = r + R[i];
56+
int newC = c + C[i];
57+
if(newR >=0 && newC >= 0 && newR < board.length && newC < board[0].length &&
58+
board[newR][newC] == 'E') //boundary check
59+
{
60+
dfs(board, newR, newC);
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+

problems/src/SetMatrixZeroes.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
/**
5+
* Created by pradhang on 3/28/2017.
6+
* Accepted
7+
*/
8+
public class SetMatrixZeroes
9+
{
10+
/**
11+
* Main method
12+
* @param args
13+
* @throws Exception
14+
*/
15+
public static void main(String[] args) throws Exception
16+
{
17+
int[][] matrix = {{0, 8, 7}, {9, 0, 8}, {9, 9, 0}};
18+
19+
new SetMatrixZeroes().setZeroes(matrix);
20+
}
21+
22+
public void setZeroes(int[][] matrix)
23+
{
24+
Set<Integer> row = new HashSet<>();
25+
Set<Integer> col = new HashSet<>();
26+
int m = matrix.length;
27+
int n = matrix[0].length;
28+
for(int i = 0; i < m; i ++)
29+
{
30+
for(int j = 0; j < n; j ++)
31+
{
32+
if(matrix[i][j] == 0)
33+
{
34+
row.add(i);
35+
col.add(j);
36+
}
37+
}
38+
}
39+
40+
for(int r : row)
41+
{
42+
for(int j = 0; j < n; j++)
43+
{
44+
matrix[r][j] = 0;
45+
}
46+
}
47+
48+
for(int c : col)
49+
{
50+
for(int i = 0; i < m; i++)
51+
{
52+
matrix[i][c] = 0;
53+
}
54+
}
55+
}
56+
}

problems/src/ThirdMaximumNumber.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import com.sun.tools.internal.ws.wsdl.document.soap.SOAPUse;
2-
31
/**
42
* Created by gouthamvidyapradhan on 25/03/2017.
53
* Accepted

0 commit comments

Comments
 (0)