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;
    }
}
#笔试题目#
全部评论
第二题过了56,哈哈哈
点赞
送花
回复 分享
发布于 2020-08-15 20:12
我也是递归,但是没有try catch,只对了11😂
点赞
送花
回复 分享
发布于 2020-08-15 20:14
秋招专场
校招火热招聘中
官网直投
第二题我也是
点赞
送花
回复 分享
发布于 2020-08-15 20:16
哭了,js也是,什么都不给,自己处理那恶心人的输入
点赞
送花
回复 分享
发布于 2020-08-15 20:20
第二题就java给了,我硬是拿js实现了一遍。。。
点赞
送花
回复 分享
发布于 2020-08-15 20:21

相关推荐

1 2 评论
分享
牛客网
牛客企业服务