"""
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. Mybatis的三种批量操作数据的方法

    方法1: 使用for循环在java代码中insert (不推荐) 方法2: 使用 在Mapper.xml当中使用 foreach循环的方式进行insert PersonDao.java文件 publi ...

  2. static的特性

    1,static方法就是没有this的方法.在static方法内部不能调用非静态方法,反过来是可以的.而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法.这实际上正是static ...

  3. Caffe2 玩玩回归(Toy Regression)[5]

    前言 这一节将讲述如何使用Caffe2的特征进行简单的线性回归学习.主要分为以下几步: - 生成随机数据作为模型的输入 - 用这些数据创建网络 - 自动训练模型 - 查看梯度递减的结果和学习过程中网络 ...

  4. 科普:为什么 String hashCode 方法选择数字31作为乘子

    作者:coolblog 此文章转载自:https://segmentfault.com/a/1190000010799123 1. 背景 某天,我在写代码的时候,无意中点开了 String hashC ...

  5. css中class后面跟两个类,这两个类用空格隔开

    css中class后面跟两个类,这两个类用空格隔开,那么这两个类对这个元素都起作用,如果产生冲突,那么后面的类将替代前面的类.

  6. 轉:StackOverflow2016最新架构探秘

    轉載:http://www.infoq.com/cn/news/2016/03/Stack-Overflow-architecture-insi?utm_source=tuicool&utm_ ...

  7. Python 基础之if if else

    1.代码块 以冒号作为开始,用缩进来划分区域,这个整体叫做代码块 if 5 == 5:   print(1)   print(2)   if True:   print(3)   print(4)   ...

  8. .Net后台实现微信小程序支付

    最近一直再研究微信支付和支付宝支付,官方支付文档中一直在讲与第三方支付打交道的原理,却没有介绍我们自己项目中的APP与后台该怎么交互(哈哈,人家也没必要介绍这一块).拜读了官方文档和前辈们的佳作,自己 ...

  9. 126、Java面向对象之引用传递实例四,修改类成员的属性值

    01.代码如下: package TIANPAN; class Message { private String info = "此内容无用"; // 定义String类型属性 p ...

  10. 解题报告:luogu P1516 青蛙的约会

    题目链接:P1516 青蛙的约会 考察拓欧与推式子\(qwq\). 题意翻译? 求满足 \[\begin{cases}md+x\equiv t\pmod{l}\\nd+y\equiv t\pmod{l ...