"""
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. SSH整合hibernate无法正常自动生成表

    检查持久化类的属性和映射文件是否正确配置,比如date格式的属性最容易配置错误

  2. nginx访问目录是没加/的重定向控制

    static 模块提供了root与alias功能:发现目标是目录时:但URI末尾未加/时:会返回301重定向:重定向后会加/ 指令 Syntax: server_name_in_redirect on ...

  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:可滚动

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  4. 【pwnable.kr】 asm

    一道写shellcode的题目, #include <stdio.h> #include <string.h> #include <stdlib.h> #inclu ...

  5. Linux centos7 Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法

    一. Linux网络相关 yum install net-tools ifconfig查看网卡ip ifup ens33开启网卡 ifdown ens33关闭网卡 设定虚拟网卡ens33:0 mii- ...

  6. JSTL fn:replace()函数替换 换行符

    转自:http://blog.163.com/chenjie_8392/blog/static/439339842010513128139/ 近日在使用textarea时,输入了回车,为了将texta ...

  7. Java中小数精度问题

    代码如下:主要是利用java中写好的DecimalFormat类进行设置(#,0,%) import java.text.DecimalFormat; import java.util.Arrays; ...

  8. Java中进行Md5加密

    java文件 https://pan.baidu.com/s/1kXcif35  密码:3cjd 代码案例: package cn.itcast.estore.utils; import java.m ...

  9. 键盘类型UIKeyboardType

    UITextField.UITextView等能够调出系统键盘的控件,通过下面这个属性可以控制弹出键盘的样式: self.priceTextField.keyboardType = UIKeyboar ...

  10. Python学习笔记之正则表达式

    本篇在写的时候大量参考了https://deerchao.cn/tutorials/regex/regex.htm的内容 一.什么是正则表达式 在编写处理字符串的程序或网页时,经常会有查找符合某些复杂 ...