目录
1275. Find Winner on a Tic Tac Toe Game-easy。array
5276. Number of Burgers with No Waste of Ingredients-easy。math
5277. Count Square Submatrices with All Ones-medium。DP
5278. Palindrome Partitioning III-hard。DP
Tic-tac-toe is played by two players A and B on a 3 x 3 grid.
Here are the rules of Tic-Tac-Toe:
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.
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".
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.
Example 1:
Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output: "A"
Explanation: "A" wins, he always plays first.
"X " "X " "X " "X " "X "
" " -> " " -> " X " -> " X " -> " X "
" " "O " "O " "OO " "OOX"
没有找到更optimized和clean的方法
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
if not moves:
return "Pending"
row = [0]*3
col = [0]*3
dia1 = [0]*5
dia2 = [0]*5
flag = 1
visited = [[False for _ in range(3)] for _ in range(3)]
for m in moves:
if not visited[m[0]][m[1]]:
visited[m[0]][m[1]] = True
else:
return "Draw"
row[m[0]] += 1 if flag else -1
col[m[1]] += 1 if flag else -1
dia1[m[0]+m[1]] += 1 if flag else -1
dia2[m[0]-m[1]+2] += 1 if flag else -1
if row[m[0]] == 3 or col[m[1]] == 3 or dia1[m[0]+m[1]] == 3 or dia2[m[0]-m[1]+2] == 3:
return "A"
elif row[m[0]] == -3 or col[m[1]] == -3 or dia1[m[0]+m[1]] == -3 or dia2[m[0]-m[1]+2] == -3:
return "B"
flag ^= 1
for row in visited:
for ele in row:
if not ele:
return "Pending"
return "Draw"
Given two integers tomatoSlices
and cheeseSlices
. The ingredients of different burgers are as follows:
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 []
.
Example 1:
Input: tomatoSlices = 16, cheeseSlices = 7
Output: [1,6]
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.
Example 2:
Input: tomatoSlices = 17, cheeseSlices = 4
Output: []
Explantion: There will be no way to use all ingredients to make small and jumbo burgers.
Example 3:
Input: tomatoSlices = 4, cheeseSlices = 17
Output: []
Explantion: Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.
解二元一次方程,注意解只能为整数所以可以先行对输入进行验证
class Solution:
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
if tomatoSlices < 0 or cheeseSlices < 0 or tomatoSlices & 1 or not (tomatoSlices >= cheeseSlices*2 and tomatoSlices <= cheeseSlices*4):
return []
a = tomatoSlices//2
b = cheeseSlices*2
return [a - cheeseSlices, b-a]
Given a m * n
matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1
constrain决定了暴力也可,在此最优是O(mn)O(mn)。dp[i][j]表示截至(包括)matrix[i][j]的最大square边长,然后把对应边长的cnt++,因为每个ij只计算最大的,更小的也应该算上,所以得到每种square的计数之后还要做一次后缀和,再求和
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if not matrix:
return 0
m = len(matrix)
n = len(matrix[0])
dp = matrix[:]
res = [0]*(max(m, n)+1)
for i in range(m):
for j in range(n):
if matrix[i][j]:
if i > 0 and j > 0:
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1
if dp[i][j]:
res[dp[i][j]] += 1
for i in range(len(res)-2, 0, -1):
res[i] += res[i+1]
return sum(res)
You are given a string s
containing lowercase letters and an integer k
. You need to :
s
to other lowercase English letters.s
into k
non-empty disjoint substrings such that each substring is palindrome.Return the minimal number of characters that you need to change to divide the string.
Example 1:
Input: s = "abc", k = 2
Output: 1
Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.
Example 2:
Input: s = "aabbc", k = 3
Output: 0
Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome.
Example 3:
Input: s = "leetcode", k = 8
Output: 0
Constraints:
1 <= k <= s.length <= 100
.s
only contains lowercase English letters.dp[i][j]表示长度为i切分成j份,每个子字符串都是palindrome对应的最小变换次数
class Solution:
def palindromePartition(self, s: str, k: int) -> int:
if len(s) <= k:
return 0
n = len(s)
dp = [[float('inf') for _ in range(k+1)] for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
for j in range(k):
for l in range(1, n-i+1):
cost = 0
a = i
b = i+l-1
while a < b:
cost += 1 if s[a] != s[b] else 0
a += 1
b -= 1
dp[i+l][j+1] = min(dp[i+l][j+1], dp[i][j] + cost)
return dp[n][k]