1042. Flower Planting With No Adjacent
题意:
本题题意为:
寻找一个花园的涂色方案,要求
1.花园和花园之间,不能有路径连接的,不能涂成相同颜色的
一共有4中颜色,花园和花园之间,至多有三条路径
我菜了 - - ,又没做出来。。
看答案
使用贪心:
idea: 对每一个花园依次进行染色时,必定可以直接染色,因为相临接的最多最多就是三个,一共4中颜色,所以一定可以直接染色了
具体想法:
每次对节点染色时,新建一个colors数组,colors[j] , 代表节点i旁边临接的节点赋值的颜色是否有,如果有,就赋值为1 然后遍历,colors,给节点i进行染色
代码如下:
public int[] gardenNoAdj(int N, int[][] paths) {
Map<Integer, Set<Integer>> G = new HashMap<>();
for (int i = 0; i < N; i ++) G.put(i, new HashSet<>());
for (int[] p: paths){
G.get(p[0] - 1).add(p[1] - 1);
G.get(p[1] - 1).add(p[0] - 1);
}
int[] res = new int[N];
for (int i = 0; i < N ; i++){
int[] colors = new int[5];
for (int j : G.get(i))
colors[res[j]] = 1;
for (int c = 4; c > 0; --c)
if (colors[c] == 0)
res[i] = c;
}
return res;
}
1042. Flower Planting With No Adjacent的更多相关文章
- 【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 ...
- 【LeetCode】1042. Flower Planting With No Adjacent 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 图 日期 题目地址:https://leetcode ...
- leetcode1042 Flower Planting With No Adjacent
""" You have N gardens, labelled 1 to N. In each garden, you want to plant one of 4 t ...
- 算法与数据结构基础 - 图(Graph)
图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...
- Leetcode 第136场周赛解题报告
周日的比赛的时候正在外面办事,没有参加.赛后看了下题目,几道题除了表面要考的内容,还是有些能发散扩展的地方. 做题目不是最终目的,通过做题发现知识盲区,去研究学习,才能不断提高. 理论和实际是有关系的 ...
- 微服务(Microservices)——Martin Flower【翻译】
原文是 Martin Flower 于 2014 年 3 月 25 日写的<Microservices>. 本文内容 微服务 微服务风格的特性 组件化(Componentization ) ...
- Autumn is a second spring when every leaf is a flower.
Autumn is a second spring when every leaf is a flower. 秋天即是第二个春天,每片叶子都是花朵.——阿尔贝·加缪
- csuoj 1390: Planting Trees
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1390 1390: Planting Trees Time Limit: 1 Sec Memory ...
随机推荐
- Excel导入导出DataGridView
/// <summary> /// excel表保存到dataTable中 /// </summary> /// <param name="path" ...
- goweb- 对请求的处理
对请求的处理 Go 语言的 net/http 包提供了一系列用于表示 HTTP 报文的结构,我们可以使用它 处理请求和发送相应,其中 Request 结构代表了客户端发送的请求报文,下面让我们看 一下 ...
- Oracle数据库之第一篇
1 : Oracle 简介 : 是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器IP,端口,用户名.密码,点击:连接 (CLIENT/SERVER) ...
- [转]Sumifs函数多条件求和的9个实例
本文转自:http://m.officezhushou.com/sumif/5187.html 第一部分:sumifs函数用法介绍 excel中sumifs函数是Excel2007以后版本新增的多条件 ...
- iOS开发中,获取图片之后保存或下载到本地相册中
#pragma mark 先获取本地图片或者网络图片 - (void)saveHeaderImageWith:(NSString *)path { UIImage *img = [UIImage im ...
- Swift设置只读(readOnly)属性
class ReadOnly { private(set) var name: String init(_ name: String) { self.name = name } } let obj = ...
- Automatic Tuning of Undo Retention 常见问题 (Doc ID 1579779.1)
Automatic Tuning of Undo Retention Common Issues (Doc ID 1579779.1) APPLIES TO: Oracle Database - En ...
- Linux—chattr 命令详解
chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files…最关键的是[mode]部分,[mode]部分是由+-=和[ASacDdIijsTtu] ...
- appium+robotframework+python连接真机定位不到元素的问题处理
这几天遇到了一个比较奇怪的问题,使用RF框架进行自动化测试的时候定位不到部分元素 并且这个元素的是有id的,更换了xpath定位也行不通,冥思苦想,加上谷歌百度,终于解决了 解决步骤如下: 1.定位问 ...
- Mac下编译libpomelo静态库,并在cocos2dx项目中引用
最近在学习cocos2dx的过程中需要和服务器进行交互,所以这几天在学习libpomelo静态库的编译和使用.之前在windows系统下编译libpomelo,并在VS中引入比较顺利:但是,目前对Ma ...