【leetcode系列】【算法】2020春季全国编程大赛-个人赛

题目一:

 

解题思路:

奇数需要拿/ 2 + 1次,偶数需要拿/ 2次

 

代码实现:

class Solution:
    def minCount(self, coins: List[int]) -> int:
        res = 0
        for a in coins:
            res += a // 2
            res += a % 2
            
        return res

 

题目二:

 

解题思路:

回溯 + 剪枝

走到固定步数,如果是n - 1,则结果个数 + 1,

如果不是,则不继续遍历查找

 

代码实现:

class Solution:
    def numWays(self, n: int, relation: List[List[int]], k: int) -> int:
        rec = collections.defaultdict(list)
        for a in relation:
            rec[a[0]].append(a[1])
            
        res = set()
        def numWays_help(k, curr_idx, path, n):
            nonlocal rec, res
            if k == 0:
                if curr_idx == n - 1:
                    res.add((a for a in path))
                return
            
            for a in rec[curr_idx]:
                path.append(a)
                numWays_help(k - 1, a, path, n)
                path.pop()
                
        numWays_help(k, 0, [], n)
        return len(res)

 

题目三:

 

解题思路:

先根据increase计算出每一天的资源情况

然后对requirements根据求和结果排序,并对排序结果开始遍历

如果一天中的资源之和小于requirements中的资源之和,一定不会符合要求,也一定不会符合requirements后面的要求

 

代码实现:

class Solution:
    def getTriggerTime(self, increase: List[List[int]], requirements: List[List[int]]) -> List[int]:
        state_lst = [[0,0,0]]
        for a in increase:
            b = state_lst[-1][:]
            b[0] += a[0]
            b[1] += a[1]
            b[2] += a[2]
            state_lst.append(b[:])
            
        rec = {idx : a for idx, a in enumerate(requirements)}
        idx = 0
        res = [-1] * len(requirements)
        start = 0
        for key in sorted(rec, key = lambda x : sum(rec[x])):
            last_bigger_start = -1
            while start < len(state_lst):
                curr_sum = sum(state_lst[start])
                if curr_sum < sum(rec[key]):
                    start += 1
                    continue
                    
                if last_bigger_start == -1:
                    last_bigger_start = start
                    
                if state_lst[start][0] >= rec[key][0] and state_lst[start][1] >= rec[key][1] and state_lst[start][2] >= rec[key][2]:
                    res[key] = start
                    break
                    
                start += 1
            
            start = last_bigger_start
                
        return res

 

题目四:

 

解题思路:

根据题意,jump[i] >= 1,所以最多经过N步,一定可以跳出jump

初始化一个步数列表,初始化的值为N,个数为N

然后开始遍历每一步能够走到的位置

如果某一步跳出了jump数组,则返回K

否则继续遍历

 

代码实现:

class Solution:
    def minJump(self, jump: List[int]) -> int:
        jump_len = len(jump)
        step_lst = [0] + [jump_len] * jump_len
        stack = [0]
        left = 0
        for step in range(1, jump_len + 1):
            new_stack = []
            right = max(stack)
            while left < right:
                if step < step_lst[left]:
                    step_lst[left] = step
                    new_stack.append(left)
                left += 1
                
            left = right + 1
            for pos in stack:
                next_pos = pos + jump[pos]
                if next_pos >= jump_len:
                    return step
                elif step < step_lst[next_pos]:
                    step_lst[next_pos] = step
                    new_stack.append(next_pos)
                    
            stack = new_stack[:]
            
        return 0

 

 

题目五:

 

解题思路:

暂无...

在研究,先标记

之后再更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值