vivo2020届校招在线编程笔试(第二、三站)

第一题:跳格子,动态规划 AC100%

def solution(step_list):
    n = len(step_list)
    dp = [-1 for i in range(n)]
    dp[n-1] = 0
    for i in range(n-1)[::-1]:
        for j in range(1,step_list[i]+1):
            if (i+j<n):
                if(dp[i+j]!=-1):
                    if(dp[i]==-1):
                        dp[i] = 1+dp[i+j]
                    elif(dp[i]>1+dp[i+j]):
                        dp[i] = 1+dp[i+j]
    return dp[0]

step_list = [int(i) for i in input().split()]
print(solution(step_list))

第二题:没测。报数出队

import collections
def solution(N,M):
    fast = collections.OrderedDict().fromkeys(range(1,N+1))
    no = 1
    i = 0
    while(len(fast.keys())>0):
        pop = False
        if (no==M):
            k = list(fast.keys())[i]
            fast.pop(k)
            print(k)
            pop = True
        no = (no%M) +1

        if not pop:
            i = i+1
        if(i>=len(fast.keys())-1 and len(fast.keys())!=0):
            i = i%(len(fast.keys()))

N,M = [int(i) for i in input().split()]
solution(N,M)

第三题:回溯法:两个矿车装石头,两边石头数量不能超过1,两边尽量平衡,求最小重量差,AC 90%

def slove_meta(stone_list,A,B,i,N):
    if(i>N or len(A)>(N+1)//2 or len(B)>(N+1)//2):
        return -1
    if(i==N):
        return abs(sum(A)-sum(B))
    else:
        if i==0:
            A.append(stone_list[i])
            a = slove_meta(stone_list,A,B,i+1,N)
            A.remove(stone_list[i])
            return a
        else:
            A.append(stone_list[i])
            a = slove_meta(stone_list,A,B,i+1,N)
            A.remove(stone_list[i])
            B.append(stone_list[i])
            b = slove_meta(stone_list,A,B,i+1,N)
            B.remove(stone_list[i])
            if a!=-1 and b!=-1:
                return min(a,b)
            elif a!=-1:
                return a
            elif b!=-1:
                return b
            else:
                return -1

def solution(stone_list):
    N = len(stone_list)
    return slove_meta(stone_list,[],[],0,N)

stone_list = [int(i) for i in input().split()]
print(solution(stone_list))

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值