文章目录
第68双周赛
5946.句子中的最多单词数
class Solution(object):
def mostWordsFound(self, sentences):
ans = 0
for sentence in sentences:
ans = max(ans, len(sentence.split(" ")))
return ans
时间复杂度: O ( m ∗ n ) O(m*n) O(m∗n),应该是吧
空间复杂度: O ( 1 ) O(1) O(1)
5947.从给定原材料中找到所有可以做出的菜
- 暴力:扫描多次,每次扫描都会有新菜品
class Solution(object):
def findAllRecipes(self, recipes, ingredients, supplies):
ans = set()
supplies = set(supplies)
# 暴力循环多次
for times in range(len(recipes)):
for i, recipe in enumerate(recipes):
flag = True
for ingredient in ingredients[i]:
if ingredient not in supplies:
flag = False
break
if flag:
ans.add(recipe)
supplies.add(recipe)
return list(ans)
时间复杂度: O ( n ∗ n ∗ m ) O(n*n*m) O(n∗n∗m)
空间复杂度: O ( n ) O(n) O(n)
5948.判断一个括号字符串是否有效
- 个数为奇数 F a l s e False False
- 两次遍历:当左边有过多不可改变的右括号和右边有过多不可改变的左括号时,无力回天
class Solution(object):
def canBeValid(self, s, locked):
n = len(s)
if n % 2:
return False
r = 0
for i in range(n):
if s[i] == ")" and locked[i] == "1":
r += 1
if r > i - r + 1:
return False
l = 0
for i in range(n - 1, -1, -1):
if s[i] == "(" and locked[i] == "1":
l += 1
if l > n - i - l:
return False
return True
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
第273周赛
5963.反转两次的数字
考虑个位数是0
- 一位数 T r u e True True
- 两位数及以上 F a l s e False False
class Solution(object):
def isSameAfterReversals(self, num):
if num == 0:
return True
else:
if num % 10 == 0:
return False
else:
return True
时间复杂度: O ( 1 ) O(1) O(1)
空间复杂度: O ( 1 ) O(1) O(1)
5964.执行所有后缀指令
- 两次循环
- 暴力模拟
class Solution(object):
def executeInstructions(self, n, startPos, s):
d = {"U": (-1, 0), "D": (1, 0),
"L": (0, -1), "R": (0, 1)}
length = len(s)
ans = []
for i in range(length):
flag = True
x, y = startPos[0], startPos[1]
for j in range(i, length):
x = x + d[s[j]][0]
y = y + d[s[j]][1]
if not(0 <= x < n and 0 <= y < n):
flag = False
break
if flag:
ans.append(j - i + 1)
else:
ans.append(j - i)
return ans
时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n ) O(n) O(n)
5965.相同元素的间隔之和
例子
arr | 2 | 1 | 3 | 1 | 2 | 3 | 3 |
---|---|---|---|---|---|---|---|
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
pre | 0 | 0 | 0 | 2 | 4 | 3 | 3+(6-5)×2=5 |
post | 4 | 2 | 1+|3-0|×2=7 | 0 | 0 | 1 | 0 |
ans | 4 | 2 | 7 | 2 | 4 | 4 | 5 |
-
p
r
e
D
i
c
t
preDict
preDict的
k
e
y
key
key是
a
r
r
[
i
]
arr[i]
arr[i],
v
a
l
u
e
value
value是一个三元素的数组
- v a l u e [ 0 ] value[0] value[0]表示上一个 a r r [ i ] arr[i] arr[i]的 i n d e x index index
- v a l u e [ 1 ] value[1] value[1]表示上一个 a r r [ i ] arr[i] arr[i]的前缀和
- v a l u e [ 2 ] value[2] value[2]表示 a r r [ i ] arr[i] arr[i]出现的次数
- p o s t D i c t postDict postDict同理
class Solution(object):
def getDistances(self, arr):
# 数组的长度
n = len(arr)
pre, post = [0] * n, [0] * n
# pre
preDict = {}
for i in range(n):
if arr[i] not in preDict:
preDict[arr[i]] = [i, 0, 0]
pre[i] += abs(preDict[arr[i]][0] - i) * preDict[arr[i]][2] + preDict[arr[i]][1]
preDict[arr[i]][0] = i
preDict[arr[i]][1] = pre[i]
preDict[arr[i]][2] += 1
# post
postDict = {}
for i in range(n - 1, -1 , -1):
if arr[i] not in postDict:
postDict[arr[i]] = [i, 0, 0]
post[i] += abs(postDict[arr[i]][0] - i) * postDict[arr[i]][2] + postDict[arr[i]][1]
postDict[arr[i]][0] = i
postDict[arr[i]][1] = post[i]
postDict[arr[i]][2] += 1
ans = [pre[i] + post[i] for i in range(n)]
return ans
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)