ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 1042. Flower Planting With No Adjacent

2020-03-21 13:51:28  阅读:358  来源: 互联网

标签:1042 Flower Planting garden paths int graph color flower


原题链接在这里:https://leetcode.com/problems/flower-planting-with-no-adjacent/

题目:

You have N gardens, labelled 1 to N.  In each garden, you want to plant one of 4 types of flowers.

paths[i] = [x, y] describes the existence of a bidirectional path from garden x to garden y.

Also, there is no garden that has more than 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]

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]

Note:

  • 1 <= N <= 10000
  • 0 <= paths.size <= 20000
  • No garden has 4 or more paths coming into or leaving it.
  • It is guaranteed an answer exists.

题解:

Build the graph with edges. 

For one node, we could check for all its neighbors, what colors have been used. Have a color array to record which color has been used among its neighbors.

Then for this node color, it could choose one color that hasn't been used.

Time Compleixty: O(e + N). e = paths.length.

Space: O(e + N).

AC Java:

 1 class Solution {
 2     public int[] gardenNoAdj(int N, int[][] paths) {
 3         Map<Integer, Set<Integer>> graph = new HashMap<>();
 4         for(int i = 0; i < N; i++){
 5             graph.put(i, new HashSet<Integer>());
 6         }
 7         
 8         for(int [] p : paths){
 9             int x = p[0] - 1;
10             int y = p[1] - 1;
11             graph.get(x).add(y);
12             graph.get(y).add(x);
13         }
14         
15         int [] res = new int[N];
16         for(int i = 0; i < N; i++){
17             int [] color = new int[5];
18             for(int nei : graph.get(i)){
19                 color[res[nei]] = 1;
20             }
21             
22             for(int c = 4; c >= 1; c--){
23                 if(color[c] != 1){
24                     res[i] = c;
25                 }
26             }
27         }
28         
29         return res;
30     }
31 }

 

标签:1042,Flower,Planting,garden,paths,int,graph,color,flower
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/12539050.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有