"""
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. nginx 网络层的优化

    TCP三次握手四次挥手 系统层的优化,主动建立连接时的重试次数 net.ipv4.tcp_syn_retries = 6 建立连接时本地端口可用范围:手动可以tiaoz net.ipv4.ip_loc ...

  2. Atcoder Grand Contest 037C(贪心,优先队列,思维)

    #define HAVE_STRUCT_TIMESPEC//编译器中time.h和phread.h头文件中timespec结构体重名,故加此行#include<bits/stdc++.h> ...

  3. 「Luogu P3168 [CQOI2015]任务查询系统」

    介绍本题的两种做法: 方法1 前置芝士 线段树:一个很重要的数据结构. 树状数组:一个很重要的数据结构. 具体实现 区间修改,单点查询很容易就会想到树状数组了,至于查询前k个数的和又可以丢给权值线段树 ...

  4. JS操作网页中的iframe

    /* *parent.html */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  5. Python 基础之字符串操作,函数及格式化format

    一.字符串的相关操作 1.字符串的拼接 + strvar1 = "我爱你,"strvar2 = "亲爱的姑凉"res = strvar1 + strvar2pr ...

  6. python如何在文件每一行前面加字符串?

    对于python中原来的文件,需要在每一行前面添加一个特舒符号,比如逗号或者“--”,需要先把原来的文件内容记录下之后,进行清空,再进行写入,另外需要注意的是r+和a+都是可写可读,不过a+是从文件末 ...

  7. LeetCode 804 唯一摩尔斯密码词

    package com.lt.datastructure.Set; import java.util.TreeSet; /* * 一个摩斯码,对应一个字母.返回我们可以获得所有词不同单词翻译的数量. ...

  8. primecoin 全节点日常维护操作

    primecoin 全节点日常维护操作: 一.关于primecoin维护,每天检查这6个地址是否能正常访问: http://api.primecoin.org/rest/pcoin/syncblock ...

  9. 六、ibatis1.2.8查询性能优化,实现百万数据zip导出

    经测试发现将查询的结果100万数据(池子中共有大概14亿的数据)写入Excle文件并进行压缩导出zip文件最耗时的地方竟然在查询,因此本篇文章主要是针对如何在spring+ibatis1.2.8中优化 ...

  10. tcpdump 获取SQL

    tcpdump [-aAdDefhIJKlLnNOpqRStuUvxX] [ -B size ] [ -c count ] [ -C file_size ] [ -E algo:secret ] [ ...