返回介绍

solution / 1000-1099 / 1042.Flower Planting With No Adjacent / README_EN

发布于 2024-06-17 01:03:31 字数 5460 浏览 0 评论 0 收藏 0

1042. Flower Planting With No Adjacent

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
    g = defaultdict(list)
    for x, y in paths:
      x, y = x - 1, y - 1
      g[x].append(y)
      g[y].append(x)
    ans = [0] * n
    for x in range(n):
      used = {ans[y] for y in g[x]}
      for c in range(1, 5):
        if c not in used:
          ans[x] = c
          break
    return ans
class Solution {
  public int[] gardenNoAdj(int n, int[][] paths) {
    List<Integer>[] g = new List[n];
    Arrays.setAll(g, k -> new ArrayList<>());
    for (var p : paths) {
      int x = p[0] - 1, y = p[1] - 1;
      g[x].add(y);
      g[y].add(x);
    }
    int[] ans = new int[n];
    boolean[] used = new boolean[5];
    for (int x = 0; x < n; ++x) {
      Arrays.fill(used, false);
      for (int y : g[x]) {
        used[ans[y]] = true;
      }
      for (int c = 1; c < 5; ++c) {
        if (!used[c]) {
          ans[x] = c;
          break;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {
    vector<vector<int>> g(n);
    for (auto& p : paths) {
      int x = p[0] - 1, y = p[1] - 1;
      g[x].push_back(y);
      g[y].push_back(x);
    }
    vector<int> ans(n);
    bool used[5];
    for (int x = 0; x < n; ++x) {
      memset(used, false, sizeof(used));
      for (int y : g[x]) {
        used[ans[y]] = true;
      }
      for (int c = 1; c < 5; ++c) {
        if (!used[c]) {
          ans[x] = c;
          break;
        }
      }
    }
    return ans;
  }
};
func gardenNoAdj(n int, paths [][]int) []int {
  g := make([][]int, n)
  for _, p := range paths {
    x, y := p[0]-1, p[1]-1
    g[x] = append(g[x], y)
    g[y] = append(g[y], x)
  }
  ans := make([]int, n)
  for x := 0; x < n; x++ {
    used := [5]bool{}
    for _, y := range g[x] {
      used[ans[y]] = true
    }
    for c := 1; c < 5; c++ {
      if !used[c] {
        ans[x] = c
        break
      }
    }
  }
  return ans
}
function gardenNoAdj(n: number, paths: number[][]): number[] {
  const g: number[][] = new Array(n).fill(0).map(() => []);
  for (const [x, y] of paths) {
    g[x - 1].push(y - 1);
    g[y - 1].push(x - 1);
  }
  const ans: number[] = new Array(n).fill(0);
  for (let x = 0; x < n; ++x) {
    const used: boolean[] = new Array(5).fill(false);
    for (const y of g[x]) {
      used[ans[y]] = true;
    }
    for (let c = 1; c < 5; ++c) {
      if (!used[c]) {
        ans[x] = c;
        break;
      }
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文