【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-十大经典排序算法
随机推荐
- 建立起BI的支撑团队
Bobby Luo 罗如意(18907295660@189.cn) 2011年7月 http://weibo.com/cquptvlry 电子商务中的BI应用初探 系统架构 对整个数据仓库的架构进行规 ...
- VMware 虚拟化编程(15) — VMware 虚拟机的恢复方案设计
目录 目录 前文列表 将已存在的虚拟机恢复到指定时间点 恢复为新建虚拟机 灾难恢复 恢复细节 恢复增量备份数据 以 RDM 的方式创建虚拟磁盘 创建虚拟机 Sample of VirtualMachi ...
- delphi 遍历窗口
http://blog.163.com/t_form/blog/static/12348523220115132155814/ function EnumWindowsProc_2(hwnd: HWN ...
- Spring学习01——HelloSpring
这是一个spring入门demo: package com.su.test; public class HelloWorld { public void say(){ System.out.print ...
- Java 与 C++ 的比较
参考 Java 中,一切皆是类 Java 中,所有数据或方法都要放在类中.如果想获得与全局函数等价的功能,可将static方法和static数据放在类里.而 C++ 中有 struct 结构.enum ...
- 【ABAP系列】SAP 业务界面同时显示KEY和文本
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 业务界面同时显示KEY和 ...
- Mbox-React Native
Mbox-React Native 学习网址:https://www.jianshu.com/p/bbf9837443f3 MboX环境配置: .npm i mobx mobx-react --sav ...
- python3.5+django2.0快速入门(二)
昨天写了python3.5+django2.0快速入门(一)今天将讲解配置数据库,创建模型,还有admin的后台管理. 配置数据库 我们打开mysite/mysite/settings.py这个文件. ...
- CEPH安装(CentOS 7)
以包含四个节点的集群为例,其中包括一个 ceph-deploy 管理节点和一个三节点的Ceph存储集群. 下图中每个节点代表一台机器. 安装 CEPH 部署工具 执行如下命令: sudo yum in ...
- python+selenium链接对象操作
对于链接对象常见的操作有:单击.获取链接文字.获取链接地址等: from selenium import webdriverfrom time import sleep driver = webdri ...