美团笔试8.13
有大佬ac了第四题吗😥
提供下第3,4,5题目的python代码:(1,2题没有存在本地ide)
加一个魔法外卖的代码..:
arr = [100, 101, 102, 103, 104, 105] t = 5 arr.sort() magic = 0 cur_time = 0 for order in arr: if order-t>=cur_time: cur_time += t else: magic += 1 print(magic)
第三题
arr = [1,2,3,4] ret = deque() len_cnt = 0 for val in arr[::-1]: ret.appendleft(val) if len_cnt >= 2: val1 = ret.pop() val2 = ret.pop() ret.appendleft(val1) ret.appendleft(val2) len_cnt += 1 print(ret) res = " ".join(list(map(str,ret))) print(res)
arr = [4,2,2,2,2] n = 5 index = defaultdict(list) count = 0 for id,val in enumerate(arr): index[val].append(id) for i in range(n-2): for j in range(i+1,n-1): ak = 3*arr[j]-arr[i] if ak in index: possible = index[ak] for idx in possible: if idx>j: count+=1 print(count)
class TreeNode: def __init__(self,val=None): self.val = val self.left = None self.right = None nodes = [5, 7, 8] TreeList = [] root = TreeNode(nodes[0]) TreeList.append(root) #创建二叉树 for i,node in enumerate(nodes[1:],2): pa = TreeList[i//2-1] if i%2==0:#左子树 new_node = TreeNode(node) pa.left = new_node else: new_node = TreeNode(node) pa.right = new_node TreeList.append(new_node) def solute(root): max_value = [0] def dfs(root,acc): if not root: max_value[0] = max(max_value[0],acc) return acc += root.val dfs(root.left,acc) dfs(root.right,acc) dfs(root,0) print(max_value[0]) solute(root)