LeetCode笔记:Biweekly Contest 108

本文是LeetCodeBiweeklyContest108的解题笔记,涵盖了四道题目的解题思路和Python代码实现,涉及贪婪算法、动态规划等编程技巧。

1. 题目一

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

1. 解题思路

这一题我的思路就是使用贪婪算法,不断地考虑每一个点作为起点时可以组成的最长情况即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def alternatingSubarray(self, nums: List[int]) -> int:
        res, cnt, pre, delta = 0, 0, -1, 1
        for x in nums:
            if x == pre + delta:
                cnt += 1
                delta = -delta
            elif x == pre + 1:
                res = max(res, cnt)
                delta = -1
                cnt = 2
            else:
                res = max(res, cnt)
                delta = 1
                cnt = 1
            pre = x
        res = max(res, cnt)
        return res if res > 1 else -1

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

2. 题目二

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

1. 解题思路

这一题我们只需要记录下每一个时刻所有占据的位置,然后按照题目的描述走一遍流程即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def relocateMarbles(self, nums: List[int], moveFrom: List[int], moveTo: List[int]) -> List[int]:
        positions = set(nums)
        for x, y in zip(moveFrom, moveTo):
            if x in positions:
                positions.remove(x)
                positions.add(y)
        return sorted(positions)

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

3. 题目三

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

1. 解题思路

这一题就是一个动态规划的思路,不断地考察以每一个点作为起点时后续能够构成的合法切分的最小个数然后返回即可。

2. 代码实现

给出python代码实现如下:

class Solution:
    def minimumBeautifulSubstrings(self, s: str) -> int:
        n = len(s)
        
        def is_power5(x):
            while x % 5 == 0:
                x = x // 5
            return x == 1
        
        @lru_cache(None)
        def dp(idx):
            if idx >= n:
                return 0
            if s[idx] == "0":
                return math.inf
            res = math.inf
            x = 0
            for i in range(idx, n):
                x = x * 2 + int(s[i])
                if is_power5(x):
                    res = min(res, 1 + dp(i+1))
            return res
        
        res = dp(0)
        return res if res != math.inf else -1

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

4. 题目四

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

1. 解题思路

这一题其实整体还是很简单的,就是从黑色格子开始考察,考虑每一个包含黑盒子的block当中含有的黑盒子的个数,然后剩余的就是完全不包含黑盒子的block的个数。

2. 代码实现

给出python代码实现如下:

class Solution:
    def countBlackBlocks(self, m: int, n: int, coordinates: List[List[int]]) -> List[int]:
        cnt = defaultdict(int)
        black_cells = {(x, y) for x, y in coordinates}
        seen = set()
        for x, y in coordinates:
            for i in [-1, 0]:
                for j in [-1, 0]:
                    if 0 <= x+i < m-1 and 0 <= y+j < n-1 and (x+i, y+j) not in seen:
                        s = [1 for a in range(2) for b in range(2) if (x+i+a, y+j+b) in black_cells]
                        cnt[len(s)] += 1
                        seen.add((x+i, y+j))
        cnt[0] = (n-1) * (m-1) - cnt[1] - cnt[2] - cnt[3] - cnt[4]
        return [cnt[i] for i in range(5)]

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值