给出题目一的试题链接如下:
这一题思路很直接,就是统计一下两个string种所有字母的个数,然后一一比较两者之间的差值。
给出python代码实现如下:
class Solution:
def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
cnt1 = Counter(word1)
cnt2 = Counter(word2)
for ch in string.ascii_lowercase:
if abs(cnt1[ch] - cnt2[ch]) > 3:
return False
return True
提交代码评测得到:耗时37ms,占用内存14.2MB。
给出题目二的试题链接如下:
这一题事实上机器人只能走在最外围,因此,我们只需要计算出周长就能准确定位出任意时间其处在的位置,由此也就能定位其当时的方式。
唯一麻烦一点的就是一些边界条件的判断。
给出python代码实现如下:
class Robot:
def __init__(self, width: int, height: int):
self.is_begining = True
self.width = width
self.height = height
self.loc = 0
self.length = (width + height) * 2 - 4
def move(self, num: int) -> None:
self.is_begining = self.is_begining and num == 0
self.loc = (self.loc + num) % self.length
def getPos(self) -> List[int]:
if self.loc <= self.width-1:
return [self.loc, 0]
elif self.width <= self.loc <= self.width + self.height-2:
return [self.width-1, self.loc - self.width+1]
elif self.width + self.height-1 <= self.loc <= 2*self.width + self.height-3:
return [2*self.width + self.height-3 - self.loc, self.height-1]
else:
return [0, self.length - self.loc]
def getDir(self) -> str:
if self.is_begining or 0 < self.loc < self.width:
return "East"
elif self.width <= self.loc < self.width + self.height-1:
return "North"
elif self.width + self.height-1 <= self.loc <= 2*self.width + self.height-3:
return "West"
else:
return "South"
提交代码评测得到:耗时352ms,占用内存18.5MB。
给出题目三的试题链接如下:
这一题我们只需要对item进行排序,然后对于每一个item,更新其value为前序中所有的item对应的value的最大值。
然后,我们对于任意一个query,只需要找到其在item当中对应的位置就能够获得最大的value。
给出python代码实现如下:
class Solution:
def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]:
items = sorted(items)
n = len(items)
for i in range(n-1):
items[i+1][1] = max(items[i][1], items[i+1][1])
res = []
for q in queries:
idx = bisect.bisect_right(items, [q, math.inf])
if idx == 0:
res.append(0)
else:
res.append(items[idx-1][1])
return res
提交代码评测得到:耗时1328ms,占用内存71.7MB。
给出题目四的试题链接如下:
放弃了,当时没想出来,隔了一周,就不想再看了……
后面有空再说吧……