Skip to content

Commit e198b96

Browse files
add 1275
1 parent 29af283 commit e198b96

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _If you like this project, please leave me a star._ ★
3131
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | [:tv:](https://www.youtube.com/watch?v=wGgcRCpSAa8)|Medium||
3232
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | |Easy||
3333
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | |Medium||
34+
|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1275.java) | |Easy|Array|
3435
|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java) | |Easy||
3536
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | |Medium||
3637
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | |Easy||
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1275. Find Winner on a Tic Tac Toe Game
5+
*
6+
* Tic-tac-toe is played by two players A and B on a 3 x 3 grid.
7+
*
8+
* Here are the rules of Tic-Tac-Toe:
9+
* Players take turns placing characters into empty squares (" ").
10+
* The first player A always places "X" characters, while the second player B always places "O" characters.
11+
* "X" and "O" characters are always placed into empty squares, never on filled ones.
12+
* The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
13+
* The game also ends if all squares are non-empty.
14+
* No more moves can be played if the game is over.
15+
* Given an array moves where each element is another array of size 2 corresponding to the row and column of
16+
* the grid where they mark their respective character in the order in which A and B play.
17+
*
18+
* Return the winner of the game if it exists (A or B), in case the game ends in a draw return "Draw",
19+
* if there are still movements to play return "Pending".
20+
*
21+
* You can assume that moves is valid (It follows the rules of Tic-Tac-Toe), the grid is initially empty and A will play first.
22+
*
23+
* Example 1:
24+
* Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
25+
* Output: "A"
26+
* Explanation: "A" wins, he always plays first.
27+
* "X " "X " "X " "X " "X "
28+
* " " -> " " -> " X " -> " X " -> " X "
29+
* " " "O " "O " "OO " "OOX"
30+
*
31+
* Example 2:
32+
* Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
33+
* Output: "B"
34+
* Explanation: "B" wins.
35+
* "X " "X " "XX " "XXO" "XXO" "XXO"
36+
* " " -> " O " -> " O " -> " O " -> "XO " -> "XO "
37+
* " " " " " " " " " " "O "
38+
*
39+
* Example 3:
40+
* Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
41+
* Output: "Draw"
42+
* Explanation: The game ends in a draw since there are no moves to make.
43+
* "XXO"
44+
* "OOX"
45+
* "XOX"
46+
*
47+
* Example 4:
48+
* Input: moves = [[0,0],[1,1]]
49+
* Output: "Pending"
50+
* Explanation: The game has not finished yet.
51+
* "X "
52+
* " O "
53+
* " "
54+
*
55+
* Constraints:
56+
* 1 <= moves.length <= 9
57+
* moves[i].length == 2
58+
* 0 <= moves[i][j] <= 2
59+
* There are no repeated elements on moves.
60+
* moves follow the rules of tic tac toe.
61+
* */
62+
public class _1275 {
63+
public static class Solution1 {
64+
public String tictactoe(int[][] moves) {
65+
String[][] board = new String[3][3];
66+
for (int i = 0; i < moves.length; i++) {
67+
if (i % 2 == 0) {
68+
board[moves[i][0]][moves[i][1]] = "X";
69+
} else {
70+
board[moves[i][0]][moves[i][1]] = "O";
71+
}
72+
if (i > 3) {
73+
if (!wins(board).equals("")) {
74+
return wins(board);
75+
}
76+
}
77+
}
78+
return moves.length == 9 ? "Draw" : "Pending";
79+
}
80+
81+
private String wins(String[][] board) {
82+
//check rows
83+
for (int i = 0; i < 3; i++) {
84+
if (board[i][0] == null) {
85+
break;
86+
}
87+
String str = board[i][0];
88+
if (str.equals(board[i][1]) && str.equals(board[i][2])) {
89+
return getWinner(str);
90+
}
91+
}
92+
93+
//check columns
94+
for (int j = 0; j < 3; j++) {
95+
if (board[0][j] == null) {
96+
break;
97+
}
98+
String str = board[0][j];
99+
if (str.equals(board[1][j]) && str.equals(board[2][j])) {
100+
return getWinner(str);
101+
}
102+
}
103+
104+
//check diagonals
105+
if (board[1][1] == null) {
106+
return "";
107+
}
108+
String str = board[1][1];
109+
if (str.equals(board[0][0]) && str.equals(board[2][2]) || (str.equals(board[0][2]) && str.equals(board[2][0]))) {
110+
return getWinner(str);
111+
}
112+
return "";
113+
}
114+
115+
private String getWinner(String str) {
116+
if (str.equals("X")) {
117+
return "A";
118+
} else {
119+
return "B";
120+
}
121+
}
122+
}
123+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1275;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1275Test {
11+
private static _1275.Solution1 solution1;
12+
private static int[][] moves;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1275.Solution1();
17+
}
18+
19+
@Before
20+
public void clear() {
21+
solution1 = new _1275.Solution1();
22+
}
23+
24+
@Test
25+
public void test1() {
26+
moves = new int[][]{
27+
{0, 0},
28+
{2, 0},
29+
{1, 1},
30+
{2, 1},
31+
{2, 2},
32+
};
33+
assertEquals("A", solution1.tictactoe(moves));
34+
}
35+
36+
@Test
37+
public void test2() {
38+
moves = new int[][]{
39+
{0, 0},
40+
{1, 1},
41+
{0, 1},
42+
{0, 2},
43+
{1, 0},
44+
{2, 0},
45+
};
46+
assertEquals("B", solution1.tictactoe(moves));
47+
}
48+
49+
@Test
50+
public void test3() {
51+
moves = new int[][]{
52+
{0, 0},
53+
{1, 1},
54+
{2, 0},
55+
{1, 0},
56+
{1, 2},
57+
{2, 1},
58+
{0, 1},
59+
{0, 2},
60+
{2, 2},
61+
};
62+
assertEquals("Draw", solution1.tictactoe(moves));
63+
}
64+
65+
@Test
66+
public void test4() {
67+
moves = new int[][]{
68+
{0, 0},
69+
{1, 1},
70+
};
71+
assertEquals("Pending", solution1.tictactoe(moves));
72+
}
73+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy