【leetcode】1042. Flower Planting With No Adjacent
题目如下:
You have
Ngardens, labelled1toN. 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 gardenxto gardeny.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, whereanswer[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 <= 100000 <= paths.size <= 20000- No garden has 4 or more paths coming into or leaving it.
- It is guaranteed an answer exists.
解题思路:可供选的花的种类只有[1,2,3,4]四种,对于任意一个待种植的花园,只需要判断相邻的花园是否已经种植花卉。如果种植了,把已种植的种类从可供选择的列表中去除,最后在剩余的种类中任选一个即可。
代码如下:
class Solution(object):
def gardenNoAdj(self, N, paths):
"""
:type N: int
:type paths: List[List[int]]
:rtype: List[int]
"""
res = [0] * (N+1)
res[1] = 1
dic = {}
for v1,v2 in paths:
dic[v1] = dic.setdefault(v1,[]) + [v2]
dic[v2] = dic.setdefault(v2,[]) + [v1]
for i in range(2,N+1):
if i not in dic:
res[i] = 1
else:
choice = [1,2,3,4]
for neibour in dic[i]:
if res[neibour] == 0:
continue
else:
if res[neibour] in choice:
inx = choice.index(res[neibour])
del choice[inx]
res[i] = choice[0]
return res[1:]
【leetcode】1042. Flower Planting With No Adjacent的更多相关文章
- 【LeetCode】1042. Flower Planting With No Adjacent 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 图 日期 题目地址:https://leetcode ...
- 【Leetcode_easy】1042. Flower Planting With No Adjacent
problem 1042. Flower Planting With No Adjacent 参考 1. Leetcode_easy_1042. Flower Planting With No Adj ...
- 1042. Flower Planting With No Adjacent
题意: 本题题意为: 寻找一个花园的涂色方案,要求 1.花园和花园之间,不能有路径连接的,不能涂成相同颜色的 一共有4中颜色,花园和花园之间,至多有三条路径 我菜了 - - ,又没做出来.. 看答案 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- 源码编译apache设置系统启动失败
文章为转载,亲试成功. Apache无法自动启动,1.将apachectl文件拷贝到/etc/rc.d/init.d 中,然后在/etc/rc.d/rc5.d/下加入链接即可.命令如下:cp /usr ...
- python-异常处理总结
一.异常处理 在程序运行的过程中,总会遇到各种各样的错误.程序一出错就停止运行了,下面的代码就不能运行了:这时候就需要捕捉异常,通过捕捉异常,再去做对应的处理. e.g: info = { " ...
- postman使用小结
需要权限验证: cookie就是存在本地的键值对,session存在服务器端的键值对 上传文件: form_data选择file ,输入传入的参数
- Linux_文件系统&磁盘分区
目录 目录 前言 文件系统 目录结构 File文件标识符 文件系统修复指令 两种磁盘格式 MBR格式 GPT格式 磁盘分区 查看分区 分区的类型 分区最小存储单元 查看当前分区的block的大小 GP ...
- Delphi控件-复合控件
http://blog.csdn.net/cml2030/article/details/3166634 Delphi控件-复合控件 标签: delphidestructorbuttonstring ...
- lazarus 2016 2月18 4:22:35 支持android开发了, 既ios,linux,macosx,window,web 后 囊括一切啦。 哈哈
Android Development Lazarus for Linux Lazarus for Mac OS X Lazarus for iOS Lazarus for Windows Lazar ...
- 类LinkedHashSet
/* * LinkedHashSet底层数据结构由哈希表和链表组成 * 哈希表保证元素的唯一性 * 链表保证元素有序(存储和取出是一致的) * */ import java.util.LinkedHa ...
- HslControls
HslControls控件库的使用demo,HslControls是一个工业物联网的控件库,基于C#开发,配套HslCommunication组件可以实现工业上位机软件的快速开发,支持常用的工业图形化 ...
- layui框架中layer父子页面交互的方法分析
本文实例讲述了layui框架中layer父子页面交互的方法.分享给大家供大家参考,具体如下: layer是一款近年来备受青睐的web弹层组件,官网地址是:http://layer.layui.com/ ...
- 000 (H5*) 常见代码
目录: 1:HTML 1:块级元素(block-level) 2:行内元素(inline-level) 3:行内块元素(inline-block) 4: img标签 5:表单元素 6:自定义列表 d ...