字节跳动笔试-2019-10(部分)

1、字节跳动水龙头接水问题,至少需要多少时间

"""

import heapq
n, m = list(map(int, input().split(' ')))
arr = list(map(int, input().split(' ')))
min_heap = arr[:m]
heapq.heapify(min_heap)
time = 0
for i in arr[m:]:
t = heapq.heappop(min_heap)
time += t
for j in range(m-1):
min_heap[j] -= t
heapq.heappush(min_heap,i)
time += max(min_heap)
print(time)
"""

2、字节跳动,找出最长配对括号字符串,可能有多个:比如(a)())(),它输出['(a)()()','(a())()']

"""

import copy
s = input()
l = 0
start = ''
for i in range(len(s)):
if s[i] == "(":
l = i
break
elif s[i] == ")":
continue
else:
start += s[i]
s = s[l:]
length = len(s)
arr = [[copy.deepcopy([' ',0]) for i in range(length)] for j in range(length)]
arr[0][0] = ['(',1]
for i in range(1,length):
if s[i] != '(' and s[i] != ')':
t = copy.copy(arr[0][i - 1])
t[0] += s[i]
arr[0][i] = t
arr[i][0] = ['(',1]
else:
t = copy.copy(arr[0][i - 1])
arr[0][i] = t
arr[i][0] = ['(',1]

for i in range(1,length):
for j in range(1,length):
if s[j] == '(':
arr[i][j][0] = arr[i-1][j-1][0]+'('
arr[i][j][1] = arr[i-1][j-1][1] + 1
elif s[j] == ')':
if arr[i-1][j-1][1] > 0:
arr[i][j][0] = arr[i - 1][j - 1][0] + ')'
arr[i][j][1] = arr[i - 1][j - 1][1] - 1
else:
arr[i][j][0] = arr[i - 1][j - 1][0]
arr[i][j][1] = arr[i - 1][j - 1][1]
else:
arr[i][j][0] = arr[i - 1][j - 1][0] + s[i]
arr[i][j][1] = arr[i - 1][j - 1][1]
res = set()
for i in range(length-1,-1,-1):
res.add(start+arr[length-1][i][0])
res.add(start + arr[i][length - 1][0])
print(res)

这里的res包括了答案,和其他的值,还需要进行遍历处理才能得到最后结果(找最长配对括号字符串)。

"""

3、字节,成绩变排名。成绩对应位置变成排名,前三名变成金银铜。

"""

arr = input().split(' ')
grad = list(map(int,arr))
dic = []
for i,v in enumerate(grad):
dic.append((v,i))
dic = sorted(dic,key=lambda x:x[0],reverse=True)

li = ['金','银','铜']+[str(i) for i in range(4,len(arr)+1)]
for i in range(len(arr)):
index = dic[i][1]
arr[index]=li[i]
print(arr)
"""

posted @ 2019-10-17 20:22  长安223  阅读(327)  评论(0编辑  收藏  举报