LeetCode每日一题(1042. Flower Planting With No Adjacent)

You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers.

All gardens have at most 3 paths coming into or leaving it.

Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.

Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.

Example 1:

Input: n = 3, paths = [[1,2],[2,3],[3,1]]
Output: [1,2,3]

Explanation:
Gardens 1 and 2 have different types.
Gardens 2 and 3 have different types.
Gardens 3 and 1 have different types.
Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].

Example 2:

Input: n = 4, paths = [[1,2],[3,4]]
Output: [1,2,1,2]

Example 3:

Input: n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
Output: [1,2,3,4]

Constraints:

  • 1 <= n <= 104
  • 0 <= paths.length <= 2 * 104
  • paths[i].length == 2
  • 1 <= xi, yi <= n
  • xi != yi
  • Every garden has at most 3 paths coming into or leaving it.

读完题目,大体有个概念,需要用广度优先的方式来遍历 graph, 深度优先的方式不是太好找终止条件

由于一个 graph 可能有多个 components, 所以我们先用 union-find 的方法把每个 component 找出来

对于一个 component, 我们从 root 入手, 广度优先进行遍历, 同时给 node 进行标号, 每遍历一层号码+1。遍历的过程中要注意,如果遇到下一层的 node 已被标号且标号与当前 node 的标号相同, 我们需要将下一层的 node 的标号+1, 但是该 node 不会放到下一层要遍历的 nodes 当中。

最终, 我们将每个 node 的(标号 % 4) + 1 作为答案输出即可。

之所以我们可以在遇到相邻且标号相同的 node 时简单的将相邻 node 的标号+1, 是因为一个 node 最多有 3 条路径, 假设遍历每条路径时都会遇到相同的情况, 该 node 的标号也就只会+3, 在%4 之后不会与之前 node 的标号重合



impl Solution {
    fn find(parents: &mut Vec<usize>, i: usize) -> usize {
        if parents[i] == i {
            return i;
        }
        let parent = Solution::find(parents, parents[i]);
        parents[i] = parent;
        return parent;
    }

    fn union(parents: &mut Vec<usize>, i: usize, j: usize) {
        let pi = Solution::find(parents, i);
        let pj = Solution::find(parents, j);
        parents[pi] = pj;
    }

    pub fn garden_no_adj(n: i32, paths: Vec<Vec<i32>>) -> Vec<i32> {
        let mut parents: Vec<usize> = (0..=n).into_iter().map(|v| v as usize).collect();
        for path in &paths {
            Solution::union(&mut parents, path[0] as usize, path[1] as usize);
        }
        for i in 1..=n as usize {
            let parent = Solution::find(&mut parents, i);
            parents[i] = parent;
        }
        let paths = paths
            .into_iter()
            .fold(vec![Vec::new(); n as usize + 1], |mut l, p| {
                l[p[0] as usize].push(p[1] as usize);
                l[p[1] as usize].push(p[0] as usize);
                l
            });
        let mut ans = vec![-1; n as usize + 1];
        for i in 1..=n as usize {
            if parents[i] == i {
                ans[i] = 0;
                let mut queue = vec![i];
                let mut next_queue = Vec::new();
                let mut v = 0;
                loop {
                    while let Some(curr) = queue.pop() {
                        for &next in &paths[curr] {
                            if ans[next] == -1 {
                                ans[next] = v;
                                next_queue.push(next);
                                continue;
                            }
                            if ans[next] % 4 == ans[curr] % 4 {
                                ans[next] += 1;
                            }
                        }
                    }
                    if next_queue.is_empty() {
                        break;
                    }
                    v += 1;
                    queue = next_queue;
                    next_queue = Vec::new();
                }
            }
        }
        ans.into_iter().skip(1).map(|v| v % 4 + 1).collect()
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值