"""
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]
"""
"""
此题用贪心算法
没掌握
"""
class Solution(object):
def gardenNoAdj(self, N, paths):
res = [0] * N
m = [[] for i in range(N + 1)] # 用来存第i个结点到其他结点的路径,i为1到N
for x, y in paths:
m[x].append(y)
m[y].append(x)
for i in range(1, N + 1): # 对N个路径进行遍历
used = set() # 用来存这个结点出现的所有路径使用过的颜色
for j in m[i]:
used.add(res[j - 1]) # 把目的地的颜色放入used里
for j in range(1, 5): # 1,2,3,4代表四种颜色
if j not in used:
res[i - 1] = j # 将该结点存入新的颜色
break
return res

leetcode1042 Flower Planting With No Adjacent的更多相关文章

  1. 【Leetcode_easy】1042. Flower Planting With No Adjacent

    problem 1042. Flower Planting With No Adjacent 参考 1. Leetcode_easy_1042. Flower Planting With No Adj ...

  2. 【leetcode】1042. 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 flow ...

  3. 【LeetCode】1042. Flower Planting With No Adjacent 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 图 日期 题目地址:https://leetcode ...

  4. 1042. Flower Planting With No Adjacent

    题意: 本题题意为: 寻找一个花园的涂色方案,要求 1.花园和花园之间,不能有路径连接的,不能涂成相同颜色的 一共有4中颜色,花园和花园之间,至多有三条路径 我菜了 - - ,又没做出来.. 看答案 ...

  5. Leetcode 第136场周赛解题报告

    周日的比赛的时候正在外面办事,没有参加.赛后看了下题目,几道题除了表面要考的内容,还是有些能发散扩展的地方. 做题目不是最终目的,通过做题发现知识盲区,去研究学习,才能不断提高. 理论和实际是有关系的 ...

  6. 算法与数据结构基础 - 图(Graph)

    图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...

  7. 微服务(Microservices)——Martin Flower【翻译】

    原文是 Martin Flower 于 2014 年 3 月 25 日写的<Microservices>. 本文内容 微服务 微服务风格的特性 组件化(Componentization ) ...

  8. Autumn is a second spring when every leaf is a flower.

    Autumn is a second spring when every leaf is a flower. 秋天即是第二个春天,每片叶子都是花朵.——阿尔贝·加缪

  9. csuoj 1390: Planting Trees

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1390 1390: Planting Trees Time Limit: 1 Sec  Memory ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:强调

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 【转】IntelliJ IDEA 仿照vs2017快捷键设置,以及字体颜色设置

    因后期工作需要使用java技术栈,所以近期抽空下载了intelliJ IDEA工具,但是作为一个Net开发者,在使用了vs以后,感觉在使用别的开发工具感觉就是没法和vs相比,毕竟vs被称为宇宙最强id ...

  3. CMake构建Qt5的VS2015项目 (Hello Qt5)

    Qt5的编译 Windows下载编译Qt5 Gui CMakeLists.txt 源码 cmake_minimum_required(VERSION 2.8.11) project(HelloQt5) ...

  4. error C2664: “FILE *fopen(const char *,const char *)”: 无法将参数 1 从“LPCTSTR”转换为“const char *”

    遇到这个问题,请打开本项目的Properties(属性)-------> Configuration Properties(配置属性)-------->General(常规)------- ...

  5. 「SDOI2013」森林

    「SDOI2013」森林 传送门 树上主席树 + 启发式合并 锻炼码力,没什么好说的. 细节见代码. 参考代码: #include <algorithm> #include <cst ...

  6. 「luogu4135」作诗

    「luogu4135」作诗 传送门 分块好题. 预处理出 \(f[i][j]\) 表示 \(i\) 号块到 \(j\) 号块的答案,\(num[i][k]\) 表示 \(k\) 在前 \(i\) 块的 ...

  7. 怎样把exe程序注册成系统服务

    怎样把exe程序注册成系统服务 最近一段时间我们公司开发一款新的产品,要在服务器上运行一个服务端程序,为了方便我就希望能将这个程序注册成系统服务开机自动启动而不用每次重启系统都要手动启动程序.要实现这 ...

  8. 「JLOI2015」战争调度

    题目 [内存限制:256 MiB][时间限制:1000 ms] [标准输入输出][题目类型:传统][评测方式:文本比较] 题目描述 脸哥最近来到了一个神奇的王国,王国里的公民每个公民有两个下属或者没有 ...

  9. PaperReading20200223

    CanChen ggchen@mail.ustc.edu.cn   AdaBatch Motivation: Current stochastic gradient descend methods u ...

  10. 如何知道某个ACTIVITY是否在前台?

    本文链接:http://zengrong.net/post/1680.htm 有一个Android应用包含包含一个后台程序,该程序会定期连接服务器来实现自定义信息的推送.但是,当这个应用处于前台的时候 ...