字节跳动0414笔试编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/89302418

第一题

在这里插入图片描述
leetcode994. 腐烂的橘子一样的,AC解答:

# -*- coding:utf-8 -*-
__author__ = 'ShawDa'

def get_ret(res):
    x, y, ret = len(res), len(res[0]), 0
    locs, stack = [[-1, 0], [0, -1], [0, 1], [1, 0]], []
    for i in range(x):
        for j in range(y):
            if res[i][j] == 2:
                stack.append((i, j, 0))
    while stack:
        i, j, ret = stack.pop(0)
        for loc in locs:
            loc_i, loc_j = i+loc[0], j+loc[1]
            if 0 <= loc_i < x and 0 <= loc_j < y and res[loc_i][loc_j] == 1:
                res[loc_i][loc_j] = 2
                stack.append((loc_i, loc_j, ret+1))
    for i in range(x):
        for j in range(y):
            if res[i][j] == 1:
                return -1
    return ret

res = []
print(int(1.0) == float(1))
import sys
for line in sys.stdin:
    a = line.split()
    res.append(list(map(int, a)))
print(get_ret(res))

第二题

在这里插入图片描述
没有全部通过:

# -*- coding:utf-8 -*-
__author__ = 'ShawDa'

test_num  = int(input())
for _ in range(test_num):
    frame_num = int(input())
    frame_dict, res = {}, 0
    for i in range(frame_num):
        frame = list(map(int, input().split()))
        if frame[0] == 0:
            frame_dict = {}
        elif not frame_dict:
            for j in range(frame[0]):
                # print(frame[2*j+1], frame[2*j+2])
                one_frame= (frame[2*j+1], frame[2*j+2])
                frame_dict[one_frame] = 1
        else:
            tmp = {}
            for j in range(frame[0]):
                one_frame= (frame[2*j+1], frame[2*j+2])
                if one_frame in frame_dict:
                    tmp[one_frame] = frame_dict[one_frame]+1
            frame_dict = tmp
        # print(frame_dict)
        if list(frame_dict.values()):
            res = max(res, max(list(frame_dict.values())))
    if res <= 2:
        print(1)
    else:
        print(res)

第三题

在这里插入图片描述
动态规划,从后到前:

# -*- coding:utf-8 -*-
__author__ = 'ShawDa'

def up_int(num):
    if int(num) == num:
        return int(num)
    else:
        return int(num)+1

n = int(input())
data = list(map(int, input().split()))
dp  = [0] * (n+1)  # 最后一个位置最小为0
for i in range(n-1, -1, -1):
    dp[i] = up_int((dp[i+1]+data[i])/2)
print(dp[0])

第四题

在这里插入图片描述

这题待解决 (20190414)

第五题

在这里插入图片描述
这题要注意船要两头跑,待修正代码

# -*- coding:utf-8 -*-
__author__ = 'ShawDa'
num_test = int(input())
for _ in range(num_test):
    n = int(input())
    data = list(map(int, input().split()))
    data.sort()
    dp = [0]*n   # 要返回
    dp[1:3] = data[1:3]
    for i in range(3, n):
        dp[i] = min(dp[i-1]+data[0]+data[i], dp[i-2]+data[0]+data[i]+2*data[1])
    print(dp[-1])

猜你喜欢

转载自blog.csdn.net/sinat_36811967/article/details/89302418
今日推荐