Skip to content

Commit 108d578

Browse files
committed
Added 2 solutions
1 parent 19ba49d commit 108d578

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
int[] rowCount;
3+
int[] colCount;
4+
int[] diagCount;
5+
public String tictactoe(int[][] moves) {
6+
rowCount = new int[3];
7+
colCount = new int[3];
8+
diagCount = new int[2];
9+
int numOfMoves = 0;
10+
for (int i = 0; i < moves.length; i++) {
11+
int x = moves[i][0];
12+
int y = moves[i][1];
13+
int move = i % 2 == 0 ? 1 : -1;
14+
updateBoard(x, y, move);
15+
numOfMoves++;
16+
if (checkForWinner(x, y)) {
17+
return i % 2 == 0 ? "A" : "B";
18+
}
19+
}
20+
return numOfMoves == 9 ? "Draw" : "Pending";
21+
}
22+
23+
private boolean checkForWinner(int x, int y) {
24+
if (rowCount[x] == 3 || rowCount[x] == -3) {
25+
return true;
26+
}
27+
if (colCount[y] == 3 || colCount[y] == -3) {
28+
return true;
29+
}
30+
if (x == y && (diagCount[0] == 3 || diagCount[0] == -3)) {
31+
return true;
32+
}
33+
if (x + y == 2 && (diagCount[1] == 3 || diagCount[1] == -3)) {
34+
return true;
35+
}
36+
return false;
37+
}
38+
39+
private void updateBoard(int x, int y, int move) {
40+
rowCount[x] += move;
41+
colCount[y] += move;
42+
if (x == y) {
43+
diagCount[0] += move;
44+
}
45+
if (x + y == 2) {
46+
diagCount[1] += move;
47+
}
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
3+
int twoX = tomatoSlices - 2 * cheeseSlices;
4+
int x = twoX / 2;
5+
int y = cheeseSlices - x;
6+
return twoX >= 0 && twoX % 2 == 0 && y >= 0 ? Arrays.asList(x, y) : new ArrayList<>();
7+
}
8+
}
9+
10+
/*
11+
4x + 2y = tomato
12+
x + y = cheese
13+
=>
14+
2x = tomato - 2 * cheese
15+
x = (tomato - 2 * cheese) / 2
16+
y = cheese - x
17+
*/

0 commit comments

Comments
 (0)