找到旅程的起点

时间:2019-01-30 19:22:04

标签: java time-complexity

我最近接受了一次采访,问我是否得到了一系列构成旅程的旅行,整个旅程的出发点是什么?

例如,我以列表的形式随机提供了一些行程,其中行程从"A"开始并结束到"B",依此类推。最终,所有旅程构成了整个旅程,其中"A"将成为整个旅程的起点,而"C"将成为目的地。问题是如何找到起点(城市)?

Trips := [ ["A","B"], ["B", "C"], ["C", "D"] ]

我为O(n)提供了以下解决方案,但在10分钟内被拒绝。我感到自己做错了什么,他们必须尽快做出决定。这是我最初的解决方法,

/*
     * function to find the starting city of a
     * given journey from a list of trips
     *
     * time complexity for the solution is O(n)
     * */
    private static String findStartCity(Map<String, String> trips) {

        Map<String, String> reverseTrips = new HashMap<String, String>();


        /*
         * create a reverse map for the trips where
         * start and destination inter-changed
         * */
        for (Map.Entry<String, String> entry : trips.entrySet()) {
            reverseTrips.put(entry.getValue(), entry.getKey());
        }

        String startCity = null;

        /*
         * if a start city for a trip is not present as the destination
         * of any other trip, the city of the starting point for the whole
         * journey
         * */
        for (Map.Entry<String, String> entry : trips.entrySet()) {

            if (!reverseTrips.containsKey(entry.getKey())) {
                startCity = entry.getKey();
                break;
            }
        }

        // the inputs are not valid
        if (startCity == null) {
            return null;
        }

        return startCity;
    }

我后来考虑了一个解决方案,该解决方案的运行速度比以前快一倍。

private static class Pair<A, B> {

    private final A start;
    private final B destination;

    public Pair(A start, B destination) {
        super();
        this.start = start;
        this.destination = destination;
    }

    public A getStart() {
        return start;
    }

    public B getDestination() {
        return destination;
    }

    public int hashCode() {

        int hashFirst = start != null ? start.hashCode() : 0;
        int hashSecond = destination != null ? destination.hashCode() : 0;

        return (hashFirst + hashSecond) * hashSecond + hashFirst;
    }

    public boolean equals(Object other) {

        if (other instanceof Pair) {
            Pair otherPair = (Pair) other;
            return
                    ((this.start == otherPair.start ||
                            (this.start != null && otherPair.start != null &&
                                    this.start.equals(otherPair.start))) &&
                            (this.destination == otherPair.destination ||
                                    (this.destination != null && otherPair.destination != null &&
                                            this.destination.equals(otherPair.destination))));
        }

        return false;
    }

    public String toString() {
        return "(" + start + ", " + destination + ")";
    }

}


private static String findStartCity1(List<Pair<String, String>> trips) {

    List<String> cities = new ArrayList<>();

    trips.forEach(trip -> {

        String start = trip.getStart();
        String destination = trip.getDestination();

        if (cities.contains(start)) {
            cities.remove(start);
        } else {
            cities.add(0, start);
        }

        if (cities.contains(destination)) {
            cities.remove(destination);
        } else {
            cities.add(destination);
        }

    });

    return cities.get(0);
}

但是,对于给定的问题,是否有指数解决方案可用?我不提供电话,但是很容易假设。

0 个答案:

没有答案