当前位置:网站首页>leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】
leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】
2022-06-12 18:36:00 【白速龙王的回眸】

分析
首先以第一个字母为key,后面的字母为value(搞一个set出来存一下)
然后不同的首字母分为不同组
然后通过product笛卡尔积,考虑不同组,只考虑选出的第一个字母小于第二个字母的情况(相反的情况会*2处理,相同不符合)
然后这两组有两个set,如果set中有相同的,那这个值显然时不能取得,否则就会交换后得到在另一个set中
因此此时两个组得贡献就是(len(s1) - len(s1 & s2)) * (len(s2) - len(s1 & s2)) * 2
注意py中的set是无序hashtable做的,所以增删改查都是o1,所以总的来说这句话拼起来就是on复杂度
所以总复杂度是26 ** 2 * on满足的
ac code
class Solution:
def distinctNames(self, ideas: List[str]) -> int:
d = defaultdict(set)
for name in ideas: d[name[0]].add(name[1:])
print(d)
print(list(product(d, repeat = 2))) # 长度为2的笛卡儿积
#return sum((len(d[a]) - len(d[a] & d[b])) * (len(d[b]) - len(d[a] & d[b])) * 2 for a, b in product(d, repeat=2) if ord(a) < ord(b))
ans = 0
for a, b in product(d, repeat=2): # 26 * 26
if ord(a) < ord(b): # in order and * 2
#print(d[a])
#print(d[b])
#print(d[a] & d[b])
# set -> hashtable, & -> o(n)
ans += (len(d[a]) - len(d[a] & d[b])) * (len(d[b]) - len(d[a] & d[b])) * 2
return ans
总结
拆分题目
根据首字母分组
不同组的话后面的一定不能取一样的
然后记得乘2即可(这样就只需要考虑前小于后的情况了)
边栏推荐
- CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
- js将数组中的0移动到末尾
- How to modify the authorization of sina Weibo for other applications
- Stack in JS (including leetcode examples) < continuous update ~>
- 基于Halcon的矩形卡片【手动绘制ROI】的自由测量
- 干货 | 一文搞定 pytest 自动化测试框架(二)
- 用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
- Review of MySQL (3): query operation
- 01 complexity
- Pytest automated testing framework (II)
猜你喜欢

Typescript type declaration file (III)

基于Halcon的矩形卡片【手动绘制ROI】的自由测量

Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment

Review of MySQL (3): query operation

PHP:Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocat

Basic SQL statement - select (single table query)

Partial scratch and corrosion detection of bolts and screws based on Halcon

When openharmony meets openeuler

Installation and configuration of window version pytorch entry depth learning environment
![Interior design style type, rendering 100 invitation code [1a12]](/img/90/8bbfbe33c5b412498744c0ea0ed559.jpg)
Interior design style type, rendering 100 invitation code [1a12]
随机推荐
GD32F4xx 与符合DLT645的电能表通信_2
Title 54: take 4 ~ 7 bits of an integer a from the right end.
C language operation database (SQLite3) call interface function
Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
HTTP缓存<强缓存与协商缓存>
torch. New usage of where (old but ignored usage)
笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
静态内存分配和动态内存分配小结
【矩阵论 & 图论】期末考试复习思维导图
Difference between rxjs of() and of ({})
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
Still using Microsoft office, 3 fairy software, are you sure you don't want to try?
VirtualLab basic experiment tutorial -6 Blazed grating
2022.6.12-----leetcode. eight hundred and ninety
To understand Devops, you must read these ten books!
dumi 搭建文档型博客
HTTP cache < strong cache and negotiation cache >
SCI Writing - Methodology
Gospel of audio and video developers, rapid integration of AI dubbing capability
Enhanced version of unit test code displayed by SAP e-commerce cloud Spartacus UI checkout spinner