作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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 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]

Note:

  1. 1 <= N <= 10000
  2. 0 <= paths.size <= 20000
  3. No garden has 4 or more paths coming into or leaving it.
  4. It is guaranteed an answer exists.

题目大意

每一个顶点都最多只有3条相邻的边,现在要给每个顶点编号1~4,要求相邻的顶点不能是相同的数字。给出其中任意一种方案。

N表示顶点数,paths表示这两个顶点(编号从1开始)之间有边。

解题方法

这个题目背景虽然是花园,但是我相信大家应该都明白了,其实说的是四色定理

既然是个图论的题目,那么就按照图的方法来解。先构建无向图,对于每个顶点检查其所有相邻顶点的编号,这个顶点用一个没有用过的编号,依次类推。题目也已经说了,解一定存在。

由于每个顶点最多只有三条边,所以时间复杂度是O(N)。

Python代码如下:

class Solution(object):
def gardenNoAdj(self, N, paths):
"""
:type N: int
:type paths: List[List[int]]
:rtype: List[int]
"""
res = [0] * N
graph = [[] for i in range(N)]
for path in paths:
graph[path[0] - 1].append(path[1] - 1)
graph[path[1] - 1].append(path[0] - 1)
for i in range(N):
neighbor_colors = []
for neighbor in graph[i]:
neighbor_colors.append(res[neighbor])
for color in range(1, 5):
if color in neighbor_colors:
continue
res[i] = color
break
return res

C++代码如下:

class Solution {
public:
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
vector<int> res(N, 0);
vector<vector<int>> graph(N);
for (auto& path : paths) {
graph[path[0] - 1].push_back(path[1] - 1);
graph[path[1] - 1].push_back(path[0] - 1);
}
for (int i = 0; i < N; ++i) {
unordered_set<int> neighbor_colors;
for (int neighbor : graph[i]) {
neighbor_colors.insert(res[neighbor]);
}
for (int color = 1; color < 5; ++color) {
if (neighbor_colors.count(color))
continue;
res[i] = color;
break;
}
}
return res;
}
};

参考资料:
https://leetcode.com/problems/flower-planting-with-no-adjacent/discuss/308003/Python-Clear-solution-without-one-line-code

日期

2019 年 6 月 9 日 —— 简单的题没有难度,需要挑战有难度的才行

【LeetCode】1042. Flower Planting With No Adjacent 解题报告(Python & C++)的更多相关文章

  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. 1042. Flower Planting With No Adjacent

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

  4. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

  6. 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)

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

  7. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

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

  8. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

随机推荐

  1. 执行脚本source 和 . 和sh 的区别是什么

    "source"和"."的功能是一样的,可以调用脚本,并将脚本里的函数也传递到当前的脚本或者解释器中,即不会开启新的bash而是在当前bash中运行. &quo ...

  2. 小方法——匹配ip地址

    [root@MiWiFi-R1CM-srv ~]# ifconfig |sed -n '2p' inet addr:192.168.139.128 Bcast:192.168.139.255 Mask ...

  3. 爬虫动态渲染页面爬取之Splash的介绍和使用

    Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我们同样可以实现动态渲染页面的抓取. 1. 功能介 ...

  4. 设置administrator账号密码

    设置administrator账号密码: 打开:附件->运行 输入:lusrmgr.msc 在里面的用户里修改administrator密码

  5. day14搭建博客系统项目

    day14搭建博客系统项目 1.下载代码包 [root@web02 opt]# git clone https://gitee.com/lylinux/DjangoBlog.git 2.使用pid安装 ...

  6. MVC、MVVM模式

    MVC 上个世纪70年代,美国施乐帕克研究中心,就是那个发明图形用户界面(GUI)的公司,开发了Smalltalk编程语言,并开始用它编写图形界面的应用程序. 到了Smalltalk-80这个版本的时 ...

  7. 【原创】Altium生成Gerber时跳出The Film is too small for this PCB的解决办法

    在用altium Designer画板子的时候,要生成gerber文件的时候,会出错,出现这样的提示框:"The Film is too small for this PCB" 原 ...

  8. oracle extract

    select extract(year from systimestamp) year         ,extract(month from systimestamp) month          ...

  9. java多线程 并发编程

    一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...

  10. SQLserver 2014 如何使用Datename()函数获取对应时间

    一.在本文中,GetDate()获得的日期由两部分组成,分别是今天的日期和当时的时间: Select GetDate() 二.用DateName()就可以获得相应的年.月.日,然后再把它们连接起来就可 ...