Valid Arrangement of Pairs

13 篇文章 0 订阅

You are given a 0-indexed 2D integer array pairs where pairs[i] = [starti, endi]. An arrangement of pairs is valid if for every index i where 1 <= i < pairs.length, we have endi-1 == starti.

Return any valid arrangement of pairs.

Note: The inputs will be generated such that there exists a valid arrangement of pairs.

Example 1:

Input: pairs = [[5,1],[4,5],[11,9],[9,4]]
Output: [[11,9],[9,4],[4,5],[5,1]]
Explanation:
This is a valid arrangement since endi-1 always equals starti.
end0 = 9 == 9 = start1 
end1 = 4 == 4 = start2
end2 = 5 == 5 = start3

Example 2:

Input: pairs = [[1,3],[3,2],[2,1]]
Output: [[1,3],[3,2],[2,1]]
Explanation:
This is a valid arrangement since endi-1 always equals starti.
end0 = 3 == 3 = start1
end1 = 2 == 2 = start2
The arrangements [[2,1],[1,3],[3,2]] and [[3,2],[2,1],[1,3]] are also valid.

Example 3:

Input: pairs = [[1,2],[1,3],[2,1]]
Output: [[1,2],[2,1],[1,3]]
Explanation:
This is a valid arrangement since endi-1 always equals starti.
end0 = 2 == 2 = start1
end1 = 1 == 1 = start2

Constraints:

  • 1 <= pairs.length <= 105
  • pairs[i].length == 2
  • 0 <= starti, endi <= 109
  • starti != endi
  • No two pairs are exactly the same.
  • There exists a valid arrangement of pairs.

思路:这题跟 Reconstruct Itinerary 十分类似,关键是找到start的点,有个特殊情况需要考虑就是入度全部没有>0的点,那么就从第一个点开始;然后再用dfs去收集 path。最后整理输出;

class Solution {
    public int[][] validArrangement(int[][] pairs) {
        HashMap<Integer, ArrayDeque<Integer>> graph = new HashMap<>();
        HashMap<Integer, Integer> indegree = new HashMap<>();
        int start = -1;
        for(int[] pair : pairs) {
            int a = pair[0];
            int b = pair[1];
            // a -> b;
            indegree.put(a, indegree.getOrDefault(a, 0) + 1);
            indegree.put(b, indegree.getOrDefault(b, 0) - 1);
            
            // build graph;
            graph.putIfAbsent(a, new ArrayDeque<Integer>());
            graph.get(a).offer(b);
        }
        
        for(Integer key: indegree.keySet()) {
            if(indegree.get(key) > 0) {
                start = key;
            }
        }
        //这题关键是如果没有indegree >0的点,就从第一个start开始;
        if(start == -1) {
            start = pairs[0][0];
        }
        
        List<Integer> list = new ArrayList<>();
        dfs(graph, list, start);
        int[][] res = new int[list.size() - 1][2];
        for(int i = 1; i < list.size(); i++) {
            res[i - 1][0] = list.get(i - 1);
            res[i - 1][1] = list.get(i);
        }
        return res;
    }

    private void dfs(HashMap<Integer, ArrayDeque<Integer>> graph,
                    List<Integer> list,
                    int start) {
        ArrayDeque<Integer> deque = graph.get(start);
        while(deque != null && !deque.isEmpty()) {
            Integer neighbor = deque.poll();
            dfs(graph, list, neighbor);
        }
        list.add(0, start);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值