首页 > 815携程研发A笔试题,过1.55
头像
爱玩矢量
编辑于 2020-08-15 20:14
+ 关注

815携程研发A笔试题,过1.55

第一题

  • 直接遍历,用堆维护顺序,AC
import heapq

def divingBoard(a, b, k):
    result = []
    heapq.heapify(result)

    for i in range(k + 1):
        heapq.heappush(result, i * a + (k - i) * b)

    start = heapq.heappop(result)
    out = '[' + str(start)
    for _ in range(k):
        cur = heapq.heappop(result)
        if start < cur:
            out += (',' + str(cur))
        start = cur
    out += ']'
    return out

_a = int(input())
_b = int(input())
_k = int(input())

if _k == 0:
    print('[]')
else:
    print(divingBoard(_a, _b, _k))

第二题

  • 递归求解,过了55%,不知道哪错了
  • Java给写了这么多代码,输入都不用处理
  • Python几行就没了,气抖冷,Python什么时候才能站起来
import java.util.*;
import java.util.stream.Collectors;

class WorkflowNode {
    String nodeId;
    int timeoutMillis;
    List<WorkflowNode> nextNodes;
    boolean initialised;

    public static WorkflowNode load(String value) {
        // Create head node;
        Map<String, WorkflowNode> map = new HashMap<>();
        WorkflowNode head = new WorkflowNode("HEAD", 0, null);
        map.put(head.nodeId, head);

        try {
            for (String nodeValue : value.split("\\|")) {
                String[] properties = nodeValue.split("\\`");
                // if (!map.containsKey(properties[0])) return null;
                WorkflowNode node = map.get(properties[0]);

                node.timeoutMillis = Integer.parseInt(properties[1]);
                node.initialised = true;

                // Check next nodes
                if (properties[2].equals("END")) {
                    continue;
                }
                node.nextNodes = Arrays.stream(properties[2].split(","))
                        .map(p -> new WorkflowNode(p, 0, null))
                        .collect(Collectors.toList());
                node.nextNodes.forEach(p -> map.put(p.nodeId, p));

                map.put(node.nodeId, node);
            }
        } catch (Exception e) {
            return null;
        }

        return head;
    }

    public WorkflowNode(String nodeId, int timeoutMillis, List<WorkflowNode> nextNodes) {
        this.nodeId = nodeId;
        this.timeoutMillis = timeoutMillis;
        this.nextNodes = nextNodes;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        StringBuilder input = new StringBuilder();
        while (cin.hasNext()) {
            input.append(cin.next());
        }
        WorkflowNode node = WorkflowNode.load(input.toString());
        if (node == null) System.out.println(-1);
        else System.out.println(calculate(node));
    }

    public static int calculate(WorkflowNode head) {
        if (head.nextNodes == null || head.nextNodes.size() == 0) return head.timeoutMillis;
        int maxTime = 0;
        for (WorkflowNode next : head.nextNodes) {
            maxTime = Math.max(maxTime, calculate(next));
        }
        return maxTime + head.timeoutMillis;
    }
}

全部评论

(5) 回帖
加载中...
话题 回帖

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐