Skip to content

Latest commit

 

History

History
171 lines (143 loc) · 4.72 KB

File metadata and controls

171 lines (143 loc) · 4.72 KB

中文文档

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

Python3

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 u in range(n):
            colors = set(ans[v] for v in g[u])
            for c in range(1, 5):
                if c not in colors:
                    ans[u] = c
                    break
        return ans

Java

class Solution {
    public int[] gardenNoAdj(int n, int[][] paths) {
        List<Integer>[] g = new List[n];
        for (int i = 0; i < n; ++i) {
            g[i] = new ArrayList<>();
        }
        for (int[] p : paths) {
            int x = p[0] - 1, y = p[1] - 1;
            g[x].add(y);
            g[y].add(x);
        }
        int[] ans = new int[n];
        for (int u = 0; u < n; ++u) {
            Set<Integer> colors = new HashSet<>();
            for (int v : g[u]) {
                colors.add(ans[v]);
            }
            for (int c = 1; c < 5; ++c) {
                if (!colors.contains(c)) {
                    ans[u] = c;
                    break;
                }
            }
        }
        return ans;
    }
}

C++

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);
        for (int u = 0; u < n; ++u) {
            unordered_set<int> colors;
            for (int v : g[u]) colors.insert(ans[v]);
            for (int c = 1; c < 5; ++c) {
                if (!colors.count(c)) {
                    ans[u] = c;
                    break;
                }
            }
        }
        return ans;
    }
};

Go

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 u := 0; u < n; u++ {
		colors := make(map[int]bool)
		for _, v := range g[u] {
			colors[ans[v]] = true
		}
		for c := 1; c < 5; c++ {
			if !colors[c] {
				ans[u] = c
				break
			}
		}
	}
	return ans
}

...