Skip to main content

周赛 Weekly Contest 387

https://leetcode.cn/contest/weekly-contest-387/

01 100243. Distribute Elements Into Two Arrays I

class Solution:
def resultArray(self, nums: List[int]) -> List[int]:
if len(nums) <= 2:
return nums
arrA = [nums[0]]
arrB = [nums[1]]
for i in range(2, len(nums)):
if arrA[-1] > arrB[-1]:
arrA.append(nums[i])
else:
arrB.append(nums[i])
return arrA+arrB

02 100237. Count Submatrices with Top-Left Element and Sum Less Than k

class Solution:
def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
rows = len(grid)
cols = len(grid[0])
hor = [[grid[i][j] for j in range(cols)] for i in range(rows)]
ver = [[grid[i][j] for j in range(cols)] for i in range(rows)]
for i in range(rows):
for j in range(cols):
if j > 0:
hor[i][j] +=hor[i][j-1]
for j in range(cols):
for i in range(rows):
if i > 0:
ver[i][j] += ver[i-1][j]
cnt =0
for i in range(rows):
for j in range(cols):
if i > 0 and j > 0:
grid[i][j] += grid[i-1][j-1]
if i > 0:
grid[i][j] += ver[i-1][j]
if j > 0:
grid[i][j] += hor[i][j-1]
if grid[i][j] <= k:
cnt +=1
return cnt

03 100234. Minimum Operations to Write the Letter Y on a Grid

详细题解:https://leetcode.cn/problems/minimum-operations-to-write-the-letter-y-on-a-grid/solutions/2664785/si-lu-jian-dan-yi-ci-bian-li-xiao-bai-ye-sny2

class Solution:
def minimumOperationsToWriteY(self, grid: List[List[int]]) -> int:
minOps = 0xFFFFFFF
rows = len(grid)
cols = len(grid[0])
yCnts = [0, 0, 0] # the count of elements of y
nCnts = [0, 0, 0] # the count of elements outside y
for i in range(rows):
for j in range(cols):
if i < rows//2:
if i == j or (j > cols//2 and i == (cols-1-j)): # Y的两个对角线斜杠
yCnts[grid[i][j]] +=1
else:
nCnts[grid[i][j]] +=1
else:
if j == cols//2: # Y的下半部分直线
yCnts[grid[i][j]] +=1
else:
nCnts[grid[i][j]] +=1

for i in range(3):
yChangeCnt = sum(yCnts) - yCnts[i]
for j in range(3):
if i == j:
continue
nChangeCnt = sum(nCnts) - nCnts[j]
minOps = min(minOps, yChangeCnt+nChangeCnt)

return minOps

04 100246. Distribute Elements Into Two Arrays II

ENG:

  1. You need to distribute all the elements of nums between two arrays arr1 and arr2 using n operations.
  2. If there is still a tie如果还是平局
  3. 10的9次方,10 to the 9th power