"""
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. KNN-学习笔记

    仅供学习使用 练习1 # coding:utf-8 # 2019/10/16 16:49 # huihui # ref: import numpy as np from sklearn import ...

  2. 【PAT甲级】1006 Sign In and Sign Out (25 分)

    题意: 给出学生人数M,输入M组学生ID,到机房的时间,离开机房的时间.输出最早到机房的学生的ID,空格,最后离开机房的学生的ID.(M大小未给出,就用了1e5) AAAAAccepted code: ...

  3. CDC学习

    最近在建立CDC环境,在网上看到一些不错的学习链接,粘贴如下: 1.https://blog.csdn.net/u011729865/article/details/52931366 属于https: ...

  4. mysql-e选项

    -e Execute command and quit 通过-e选项,可以在命令行中操作mysql 一些mysql设置的有密码,此时可以在my.ini(my.cnf)的[client]下面给出数据库的 ...

  5. redhat 7.6 apache 服务简单安装-01

    rpm -qa | grep httpd         //该命令查看apache是否安装,下面图片是已安装,未安装不会显示任何内容 yum install   httpd   -y        ...

  6. redis5.0版本集群搭建

    模式简介 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令,比如:mget, 因 ...

  7. C/C++网络编程1——linux下实现

    网络编程就是编写程序使两台联网的计算机相互交换数据. 例子:服务器端开启一个socket,监听9999端口.客户端向服务器端发起请求,服务器端收到请求以后,给客户端发送一句:"hello w ...

  8. Write-up-Toppo

    关于 下载地址:点我 哔哩哔哩:哔哩哔哩 信息收集 vmnet8网卡,IP:192.168.131.144,开放web,ssh服务 ➜ ~ ip a show dev vmnet8 5: vmnet8 ...

  9. 一文解读SDN (转)

    一. 什么是SDN? SDN字面意思是软件定义网络,其试图摆脱硬件对网络架构的限制,这样便可以像升级.安装软件一样对网络进行修改,便于更多的APP(应用程序)能够快速部署到网络上. 如果把现有的网络看 ...

  10. Solr与tomcat搭建(搭建好)

    https://pan.baidu.com/s/1kXagNYJ  密码:hgxd