当前位置:网站首页>leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】
leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】
2022-06-30 15:49:00 【白速龙王的回眸】
分析
记录一下邻接表信息
然后从1到n
每个点看,先从1234中去掉它邻居的颜色,然后这个点就在这里选一个即可
用到的set pop就是随机从set中拿一个
ac code
class Solution:
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
# 四色问题(三条路径)
g = defaultdict(list)
for x, y in paths:
g[x].append(y)
g[y].append(x)
ans = [0] * n
# set的pop随机弹出一个
# 贪心选当前的即可,后面不会有矛盾点(最多三个路径)
for i in range(1, n + 1):
# 去掉和i相邻的j的颜色即可
colors = set(range(1, 5)) - set([ans[j - 1] for j in g[i]])
ans[i - 1] = colors.pop()
return ans
总结
按顺序每个点填色
只需要不与其相邻的点重复即可
边栏推荐
- 9: Chapter 3: e-commerce engineering analysis: 4: [general module]; (to be written...)
- Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]
- Halcon knowledge: matrix topic [02]
- 数据库系统概论习题册
- Raft介绍
- 聊聊远程办公那些事儿 | 社区征文
- [JVM] class loading related interview questions - class loading process and parental delegation model
- 【JVM】一文带你了解JVM中的垃圾回收机制(GC)——内含图解
- Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
- Exception class_ Log frame
猜你喜欢
OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义
Compile u-boot source code for stm32p157 development board
List announced - outstanding intellectual property service team in China in 2021
数据挖掘知识点整理(期末复习版)
TCP Socket与TCP 连接
Data security compliance has brought new problems to the risk control team
Research on helmet wearing detection algorithm
Mathematical modeling for war preparation 34-bp neural network prediction 2
数据安全合规之后,给风控团队带来了新的问题
山西化工园区智能化管控平台建设时间表
随机推荐
Simpleitk encountered an ITK only supports orthonormal direction cosines error while reading NII
differential analysis between different groups nichenet for silicosis成功运行!
register_ Chrdev and CDEV_ init cdev_ Add usage differences
Talk about telecommuting | community essay solicitation
RT-Thread 堆區大小設置
POJ Project Summer
IndexSearch
Hologres shared cluster helps Taobao subscribe to the extreme refined operation
[wechat applet] basic use of common components (view/scroll-view/wiper, text/rich-text, button/image)
Design of piece counter based on 51 single chip microcomputer
【JVM】类加载相关面试题——类加载过程、双亲委派模型
STL tutorial 7-set, pair pair pair group and functor
Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
《网络是怎么样连接的》读书笔记 - 汇总篇
Observation cloud reached in-depth cooperation with tdengine to optimize enterprise cloud experience
Compile - compile for itop4412 development board makefile
Property or method “approval1“ is not defined on the instance but referenced during render
阿里云盘分享压缩包
您工厂的MES再不升级,就要被淘汰啦