赞
踩
模拟过程即可:
class Solution: def hardestWorker(self, n: int, logs: List[List[int]]) -> int: res=[] time_max=0 for i in range(len(logs)): if(i==0): time=logs[i][1] else: time=logs[i][1]-logs[i-1][1] if(time>time_max): res=[] res.append(logs[i][0]) time_max=time elif(time==time_max): res.append(logs[i][0]) return min(res)
思路:差分 : arr[i] = pref[i] ^ pref[i - 1]
b1 = a1 那么通过前缀和bn 可以得到原数组an a1 = b1
b2 = a1 + a2 a2 = b2 - b1
b3 = a1 + a2 + a3 a3 = b3 - b2
所以通过前缀和数组的逆运算(+ -> - * -> / ) 就可以得到原数组
# 异或在编程里面的逆就是 异或
pref1 = a1 a1 = pref1
pref2 = a1 ^ a2 ---> a2 = pref2 ^ pref1
pref3 = a1 ^ a2 ^ a3 a3 = pref3 ^ pref2
class Solution:
def findArray(self, pref: List[int]) -> List[int]:
arr=[]
for i in range(len(pref)):
if(i==0):
arr.append(pref[i])
else:
arr.append(pref[i]^pref[i-1])
return arr
class Solution: def numberOfPaths(self, grid: List[List[int]], k: int) -> int: MOD = 10 ** 9 + 7 m, n = len(grid), len(grid[0]) dp = [[[0] * k for _ in range(n + 1)] for _ in range(m + 1)] dp[1][1][grid[0][0]%k]=1 for i in range(1,m+1): for j in range(1,n+1): if(i==1 and j==1): continue for x in range(k): # dp[i][j][x]表示从 (0, 0)走到 (i, j)时,路径和与 k 取模正好等于 x 的路径数量。 dp[i][j][(x+grid[i-1][j-1])%k] = (dp[i][j-1][x] + dp[i-1][j][x])%MOD # dp[i][j][0]表示路径之和可以被k整除 return dp[m][n][0]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。