LeetCode笔记:Biweekly Contest 65

1. 题目一

给出题目一的试题链接如下:

1. 解题思路

这一题思路很直接,就是统计一下两个string种所有字母的个数,然后一一比较两者之间的差值。

2. 代码实现

给出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。

2. 题目二

给出题目二的试题链接如下:

1. 解题思路

这一题事实上机器人只能走在最外围,因此,我们只需要计算出周长就能准确定位出任意时间其处在的位置,由此也就能定位其当时的方式。

唯一麻烦一点的就是一些边界条件的判断。

2. 代码实现

给出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。

3. 题目三

给出题目三的试题链接如下:

1. 解题思路

这一题我们只需要对item进行排序,然后对于每一个item,更新其value为前序中所有的item对应的value的最大值。

然后,我们对于任意一个query,只需要找到其在item当中对应的位置就能够获得最大的value。

2. 代码实现

给出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。

4. 题目四

给出题目四的试题链接如下:

放弃了,当时没想出来,隔了一周,就不想再看了……

后面有空再说吧……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值