Weekly Contest 314
Problem A
The Employee That Worked on the Longest Task
思路
按照题目要求遍历一下就行
代码
class Solution:
def hardestWorker(self, n: int, logs: List[List[int]]) -> int:
L = logs[0][1]
ans = logs[0][0]
for i in range(1,len(logs)):
d = logs[i][1]-logs[i-1][1]
# print(d)
temp = logs[i][0]
# print(d,temp,L)
if d>L or (d==L and temp<ans):
ans = temp
L = d
return ans
Problem B
Find The Original Array of Prefix Xor
思路
数学小规律 A^B = c A^C =B。按位推算可验证(1^0 = 1 1^1 = 0 0^0 =0)
代码
class Solution:
def findArray(self, pref: List[int]) -> List[int]:
ans = []
ans.append(pref[0])
for i in range(1,len(pref)):
ans.append(pref[i]^pref[i-1])
return ans
Problem C
Using a Robot to Print the Lexicographically Smallest String
思路
待补
代码
class Solution:
def robotWithString(self, s: str) -> str:
n = len(s)
used = [0 for _ in s]
last = 0
res = ""
for ch in string.ascii_lowercase:
while last-1 >= 0 and s[last-1] <= ch:
if used[last-1] == 0:
res += s[last-1]
used[last-1] = 1
last -= 1
for i, c in enumerate(s):
if i >= last and c == ch and used[i] == 0:
res += ch
last = i
used[i] = 1
s = "".join([ch for i, ch in enumerate(s) if used[i] == 0])
return res + s[::-1]
Problem D
Paths in Matrix Whose Sum Is Divisible by K
思路
待补
代码
class Solution:
def numberOfPaths(self, grid: List[List[int]], k: int) -> int:
mod = 10**9 + 7
m, n = len(grid), len(grid[0])
f = [[[0]*k for _ in range(n+1)] for _ in range(m+1)]
f[0][1][0] = 1
for i, row in enumerate(grid):
for j, x in enumerate(row):
for v in range(k):
f[i+1][j+1][(v+x)%k] = (f[i+1][j][v] + f[i][j+1][v])%mod
return f[m][n][0]
总结
出了两题,后面不太想做了,有空补补思路。
原文地址:http://www.cnblogs.com/baihualiaoluan/p/16813087.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性