Skip to content

Commit f4e034a

Browse files
committed
Adding solution of weekly contest
1 parent cb06d73 commit f4e034a

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed

1200-1300q/1275.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'''
2+
Tic-tac-toe is played by two players A and B on a 3 x 3 grid.
3+
4+
Here are the rules of Tic-Tac-Toe:
5+
6+
Players take turns placing characters into empty squares (" ").
7+
The first player A always places "X" characters, while the second player B always places "O" characters.
8+
"X" and "O" characters are always placed into empty squares, never on filled ones.
9+
The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
10+
The game also ends if all squares are non-empty.
11+
No more moves can be played if the game is over.
12+
Given an array moves where each element is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which A and B play.
13+
14+
Return the winner of the game if it exists (A or B), in case the game ends in a draw return "Draw", if there are still movements to play return "Pending".
15+
16+
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.
17+
18+
19+
20+
Example 1:
21+
22+
Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
23+
Output: "A"
24+
Explanation: "A" wins, he always plays first.
25+
"X " "X " "X " "X " "X "
26+
" " -> " " -> " X " -> " X " -> " X "
27+
" " "O " "O " "OO " "OOX"
28+
Example 2:
29+
30+
Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
31+
Output: "B"
32+
Explanation: "B" wins.
33+
"X " "X " "XX " "XXO" "XXO" "XXO"
34+
" " -> " O " -> " O " -> " O " -> "XO " -> "XO "
35+
" " " " " " " " " " "O "
36+
Example 3:
37+
38+
Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
39+
Output: "Draw"
40+
Explanation: The game ends in a draw since there are no moves to make.
41+
"XXO"
42+
"OOX"
43+
"XOX"
44+
Example 4:
45+
46+
Input: moves = [[0,0],[1,1]]
47+
Output: "Pending"
48+
Explanation: The game has not finished yet.
49+
"X "
50+
" O "
51+
" "
52+
53+
54+
Constraints:
55+
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+
63+
class Solution(object):
64+
def tictactoe(self, moves):
65+
"""
66+
:type moves: List[List[int]]
67+
:rtype: str
68+
"""
69+
def check(grid):
70+
for x in range(3):
71+
row = set([grid[x][0],grid[x][1],grid[x][2]])
72+
if len(row) == 1 and grid[x][0] != 0:
73+
return grid[x][0]
74+
75+
for x in range(3):
76+
column = set([grid[0][x],grid[1][x],grid[2][x]])
77+
if len(column) == 1 and grid[0][x] != 0:
78+
return grid[0][x]
79+
80+
diag1 = set([grid[0][0],grid[1][1],grid[2][2]])
81+
diag2 = set([grid[0][2],grid[1][1],grid[2][0]])
82+
if len(diag1) == 1 or len(diag2) == 1 and grid[1][1] != 0:
83+
return grid[1][1]
84+
85+
return 0
86+
87+
if not moves:
88+
return ""
89+
grid = [[0, 0, 0], [0, 0, 0], [0,0,0]]
90+
user = 1
91+
for move in moves:
92+
grid[move[0]][move[1]] = user
93+
if user == 1:
94+
user = 2
95+
else:
96+
user = 1
97+
98+
result = check(grid)
99+
if result == 1:
100+
return "A"
101+
elif result == 2:
102+
return "B"
103+
else:
104+
if len(moves) == 9:
105+
return "Draw"
106+
else:
107+
return "Pending"

1200-1300q/1276.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows:
3+
4+
Jumbo Burger: 4 tomato slices and 1 cheese slice.
5+
Small Burger: 2 Tomato slices and 1 cheese slice.
6+
Return [total_jumbo, total_small] so that the number of remaining tomatoSlices equal to 0 and the number of remaining cheeseSlices equal to 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return [].
7+
8+
9+
10+
Example 1:
11+
12+
Input: tomatoSlices = 16, cheeseSlices = 7
13+
Output: [1,6]
14+
Explantion: To make one jumbo burger and 6 small burgers we need 4*1 + 2*6 = 16 tomato and 1 + 6 = 7 cheese. There will be no remaining ingredients.
15+
Example 2:
16+
17+
Input: tomatoSlices = 17, cheeseSlices = 4
18+
Output: []
19+
Explantion: There will be no way to use all ingredients to make small and jumbo burgers.
20+
Example 3:
21+
22+
Input: tomatoSlices = 4, cheeseSlices = 17
23+
Output: []
24+
Explantion: Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.
25+
Example 4:
26+
27+
Input: tomatoSlices = 0, cheeseSlices = 0
28+
Output: [0,0]
29+
Example 5:
30+
31+
Input: tomatoSlices = 2, cheeseSlices = 1
32+
Output: [0,1]
33+
34+
35+
Constraints:
36+
37+
0 <= tomatoSlices <= 10^7
38+
0 <= cheeseSlices <= 10^7
39+
'''
40+
class Solution(object):
41+
def numOfBurgers(self, tomatoSlices, cheeseSlices):
42+
"""
43+
:type tomatoSlices: int
44+
:type cheeseSlices: int
45+
:rtype: List[int]
46+
"""
47+
jumbo = tomatoSlices - 2*cheeseSlices
48+
if jumbo >= 0 and jumbo%2 == 0:
49+
x = jumbo/2
50+
y = cheeseSlices-(jumbo/2)
51+
if x >= 0 and y >= 0:
52+
return [x, y]
53+
else:
54+
return []
55+
return []
56+

1200-1300q/1277.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
3+
4+
5+
6+
Example 1:
7+
8+
Input: matrix =
9+
[
10+
[0,1,1,1],
11+
[1,1,1,1],
12+
[0,1,1,1]
13+
]
14+
Output: 15
15+
Explanation:
16+
There are 10 squares of side 1.
17+
There are 4 squares of side 2.
18+
There is 1 square of side 3.
19+
Total number of squares = 10 + 4 + 1 = 15.
20+
Example 2:
21+
22+
Input: matrix =
23+
[
24+
[1,0,1],
25+
[1,1,0],
26+
[1,1,0]
27+
]
28+
Output: 7
29+
Explanation:
30+
There are 6 squares of side 1.
31+
There is 1 square of side 2.
32+
Total number of squares = 6 + 1 = 7.
33+
34+
35+
Constraints:
36+
37+
1 <= arr.length <= 300
38+
1 <= arr[0].length <= 300
39+
0 <= arr[i][j] <= 1
40+
'''
41+
class Solution(object):
42+
def countSquares(self, matrix):
43+
"""
44+
:type matrix: List[List[int]]
45+
:rtype: int
46+
"""
47+
48+
p_arr = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
49+
result = 0
50+
51+
for index_i in range(1, len(matrix)):
52+
for index_j in range(1, len(matrix[0])):
53+
if matrix[index_i][index_j] == 1:
54+
matrix[index_i][index_j] = min(matrix[index_i-1][index_j-1], min(matrix[index_i-1][index_j], matrix[index_i][index_j-1]))+1
55+
# print p_arr
56+
return sum([ sum(x) for x in matrix])

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1414
##### [Problems 1100-1200](./1200-1300q/)
1515
| # | Title | Solution | Difficulty |
1616
|---| ----- | -------- | ---------- |
17+
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Python](./1200-1300q/1277.py)|Medium|
18+
|1276|[Number of Burgers with No Waste of Ingredients](https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/)|[Python](./1200-1300q/1276.py)|Medium|
19+
|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Python](./1200-1300q/1275.py)|Easy|
1720
|1268|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Python](./1200-1300q/1268.py)|Medium|
1821
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Python](./1200-1300q/1267.py)|Medium|
1922
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Python](./1200-1300q/1266.py)|Easy|

0 commit comments

Comments
 (0)