VIVO:笔试题(20190911)

拆礼盒

设立一个栈,判断到字符0为止仍剩余的左括号。所以当匹配到右括号的时候应该出一个左括号与之匹配。

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Welcome to vivo !
'''


def solution(s):
    # TODO Write your code here
    stack = []
    for su in s:
        if su == '(':
            stack.append(su)
        elif su == ')':
            stack.pop()
        elif su == '0':
            break
    return len(stack)


if __name__ == '__main__':
    input = input()
    print(solution(input))

服务部署

动态规划,每一个值在符合要求的情况下只有两种可能,要么跳过它,要么包含它,所以要把其它不符合要求的情况列举完全。

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Welcome to vivo !
'''


def solution(total_disk, total_memory, app_list):
    # TODO Write your code here
    if total_disk <= 0 or total_memory <= 0:
        return 0
    if not app_list:
        return 0
    disk, memory, user = app_list[0]
    # print(disk, memory, user, type(user))
    if total_disk < disk or total_memory < memory:
        return 0
    else:
        # print(total_disk-disk, total_memory-memory, app_list[1:])
        return max(user+solution(total_disk-disk, total_memory-memory, app_list[1:]),
                   solution(total_disk, total_memory, app_list[1:]))


if __name__ == "__main__":
    input1 = input()
    disk = int(input1.split()[0])
    memory = int(input1.split()[1])
    input2 = input1.split()[2]
    app_list = [[int(j) for j in i.split(',')] for i in input2.split('#')]
    print(solution(disk, memory, app_list))

消消乐

思路采用重复覆盖的方法,始终在可能集中寻找索引递增的键值,弹出这个键值则依次将这个键值对应的索引加到其它值所对应的键值,但是要注意是在它们的取值范围内。这里没考虑嵌套的情况,是如何取舍?比如:3 1 2 2 4 2 4 5,到最后可能集中的值为{32: [2, 3, 5], 24: [4, 6]}

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Welcome to vivo !
'''


def solution(boxes):
    # TODO Write your code here
    values = set(boxes)
    possible = dict()
    for vu in values:
        count = boxes.count(vu)
        current = []
        for bi in range(len(boxes)):
            if boxes[bi] == vu:
                current.append(bi)
        possible[count*10+vu] = current

    res = 0
    visited = find(possible)
    while visited:
        cur = visited.pop()
        res += (cur//10) * (cur//10)

        possible = add(possible, cur)
        visited = find(possible)
    return res


def find(values):
    # print('find', values)
    res = []
    for vk, vu in values.items():
        if judge(vu):
            res.append(vk)
    return res


def judge(seq):
    if len(seq) < 2:
        return True
    pre = seq[0]
    for su in seq[1:]:
        if pre + 1 == su:
            pre = su
        else:
            return False
    return True


def add(values, key):
    value = values[key]
    values.pop(key)
    for vk, vu in values.items():
        if len(vu) < 2:
            continue
        vl, vr = vu[0], vu[-1]
        for val in value:
            if val >= vl and val <= vr:
                vu.append(val)
        tmp = list(set(vu))
        tmp.sort()
        values[vk] = tmp
    # print('add', value, values)
    return values


if __name__ == '__main__':
    x = input()
    boxes = list(map(int, x.split()))
    print(solution(boxes))

(最近更新:2019年09月14日)

你可能感兴趣的:(PROGRAM,VIVO,笔试题)