【LeetCode】1042. Flower Planting With No Adjacent 解题报告(Python & C++)
作者: 负雪明烛
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 <= N <= 10000
0 <= paths.size <= 20000
- No garden has 4 or more paths coming into or leaving it.
- 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;
}
};
日期
2019 年 6 月 9 日 —— 简单的题没有难度,需要挑战有难度的才行
【LeetCode】1042. Flower Planting With No Adjacent 解题报告(Python & C++)的更多相关文章
- 【Leetcode_easy】1042. Flower Planting With No Adjacent
problem 1042. Flower Planting With No Adjacent 参考 1. Leetcode_easy_1042. Flower Planting With No Adj ...
- 【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 ...
- 1042. Flower Planting With No Adjacent
题意: 本题题意为: 寻找一个花园的涂色方案,要求 1.花园和花园之间,不能有路径连接的,不能涂成相同颜色的 一共有4中颜色,花园和花园之间,至多有三条路径 我菜了 - - ,又没做出来.. 看答案 ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
随机推荐
- 【模板】网络最大流(EK、Dinic、ISAP)(网络流)/洛谷P3376
题目链接 https://www.luogu.com.cn/problem/P3376 题目大意 输入格式 第一行包含四个正整数 \(n,m,s,t\),分别表示点的个数.有向边的个数.源点序号.汇点 ...
- 巩固javaweb的第二十一天
巩固内容:对输入信息进行验证 JavaScript 语言 在 Web 应用中需要在客户端执行的功能可以使用 JavaScript 语言编写,在使用的时候 需要把 JavaScript 代码放在下面的两 ...
- Hbase与Phoenix整合
目录 一.简介 二.安装 三.Phoenix Shell操作 SCHEMA操作 1.创建schema 2.使用schema 3.删除schema 表操作 1.显示所有表 2.创建表 3.表数据的增删改 ...
- jenkins之代码回滚
#:通过传参数方式 #:保存后就会看到这样 #;:我们在jenkins服务器写一个脚本 root@ubuntu:~# mkdir /root/script/web1 -pv mkdir: create ...
- EBS 抓trace 文件
如果要对FORM的操作做TRACE操作,可以使用 帮助->诊断->跟踪 中启用跟踪功能来实现. 但是如果要实现对并发请求的trace,需要在 系统管理员->并发->方案-> ...
- ReactiveCocoa操作方法-重复
retry重试 只要失败,就会重新执行创建信号中的block,直到成功. __block int i = 0; [[[RACSignal createSignal:^RACDisposabl ...
- Socket通信和多线程的总结
1.ServerSocket进行多线程接收 package com.yh.chat; import java.io.IOException; import java.net.ServerSocket; ...
- 【C/C++】学生排队吃饭问题
问题: 有n个学生,学生们都在排队取餐,第个学生在L国时刻来到队尾,同一时刻来的学生编号小的在前,每个时刻当队列不为空时,排在队头的同学就可以拿到今天的中餐并离开队伍,若第个学生R团时刻不能拿到中餐, ...
- 【C/C++】子数组的最大累加和问题
#include <bits/stdc++.h> using namespace std; class Solution { public: /** * max sum of the su ...
- 【C#】【MySQL】【GridView】删除出现Parameter index is out of range
[编程语言]C# [数据库]MySQL [控件]GridView [问题描述]GridView控件中自带[删除],[编辑],[选择],三个按钮[编辑],[选择]正常使用,但是在使用删除时,却报错Par ...