【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-十大经典排序算法
随机推荐
- leetcode-mid-Linked list-94 Binary Tree Inorder Traversal
mycode 95% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): ...
- an ordered dict within the ordered dict
w http://stackoverflow.com/questions/20166749/how-to-convert-an-ordereddict-into-a-regular-dict-in-p ...
- java操作JSON字符串转换成对象的时候如何可以不建立实体类也能获取数据
引入依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson& ...
- AWK之随心所欲-基础篇
一.简介 awk 是一个处理文本的编程语言工具,能用简短的程序处理标准输入或文件.数据排序.计算以及 生成报表等等. 在 Linux 系统下默认 awk 是 gawk,它是 awk 的 GNU 版本. ...
- 测开之路八十七:HTML之a标签的用法
初始化的HTML结构为,只需要在body里面加网页的标签和要显示的内容即可 <!DOCTYPE html><html lang="en"><head& ...
- Binder的Native实现libbinder
libbinder – Binder的Native实现 出于性能和代码统一性的角度考虑,Binder IPC并不Java和Native环境里各实现一次,而只是分别在不同的执行环境里提供使用的接口.使用 ...
- PHP 常用函数-url函数
urlencode 和 rawurlencode urlencode 和 rawurlencode 两个函数都用来编码 URL 字符串.除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后 ...
- 16/7/7_PHP-Static静态关键字
Static静态关键字 静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用. class Car { private stat ...
- 【openstf】自己的云测平台——mac安装openstf
openstf的github地址:https://github.com/openstf/stf 上图可以清晰看出openstf的使用场景和效果 openstf是一个web应用程序,用于远程调试智能 ...
- 编译的时候出现"/usr/bin/ld: cannot find -lz
编译的时候出现"/usr/bin/ld: cannot find -lz"错误,需要安装zlib-dev这个包,在线安装命令为:apt-get install zlib1g-dev ...