LeetCode笔记:Biweekly Contest 66

LeetCode笔记:快速解决四题挑战

1. 题目一

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

1. 解题思路

这一题只要按照题意找一下两个list当中出现次数均为1的单词即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def countWords(self, words1: List[str], words2: List[str]) -> int:
        cnt1 = Counter(words1)
        cnt2 = Counter(words2)
        return len([s for s in cnt1 if cnt1[s] == 1 and cnt2[s] == 1])

提交代码评测得到:耗时68ms,占用内存14.8MB。

2. 题目二

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

1. 解题思路

这一题我的思路其实比较暴力,因为首先就是判断是否存在某个房子两侧没有空隙,然后就是统计可以共用同一个盒子的情况,即所有的H.H.……的情况。

2. 代码实现

给出python代码实现如下:

class Solution:
    def minimumBuckets(self, street: str) -> int:
        n = len(street)
        
        def is_valid(idx):
            if street[idx] == "H":
                if (idx > 0 and street[idx-1] == ".") or (idx+1 < n and street[idx+1] == "."):
                    return True
                else:
                    return False
            return True
        
        if any(not is_valid(idx) for idx in range(n)):
            return -1
        
        res = 0
        i = 0
        while i < n:
            if street[i] == "H":
                cnt = 1
                while i+1 < n and street[i+1] == "." and i+2 < n and street[i+2] == "H":
                    i += 2
                    cnt += 1
                res += (cnt+1) // 2
            i += 1
        return res

提交代码评测得到:耗时214ms,占用内存15.4MB。

3. 题目三

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

1. 解题思路

这一题想清楚了有点蠢,因为事实上最后的线路就是直接最短距离的走到终点。

因此,我们事实上就是稍微求个和即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
        res = 0
        x0, y0 = startPos
        x1, y1 = homePos
        while x0 < x1:
            res += rowCosts[x0+1]
            x0 += 1
        while x0 > x1:
            res += rowCosts[x0-1]
            x0 -= 1
        while y0 < y1:
            res += colCosts[y0+1]
            y0 += 1
        while y0 > y1:
            res += colCosts[y0-1]
            y0 -= 1
        return res

提交代码评测得到:耗时1448ms,占用内存29.4MB。

4. 题目四

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

这一题看了一下,只有一个暴力求解的思路,结果遇到了超时,49个样例最后一个通不过,没啥其他的改进思路了,读者们有兴趣的话可以直接看看其他大佬们的解答,我这边暂时放弃了,后面有时间再看一下吧……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值