记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步




9/15 1935. 可以输入的最大单词数

检查是否包含损坏字母

def canBeTypedWords(text, brokenLetters):
"""
:type text: str
:type brokenLetters: str
:rtype: int
"""
l = text.split(" ")
if len(brokenLetters)==0:
return len(l)
if len(brokenLetters)==26:
return 0
brokenset = set(brokenLetters)
ans = 0
for word in l:
s = list(set(word))
check = True
for c in s:
if c in brokenset:
check = False
break
if check:
ans +=1
return ans

9/16 2197. 替换数组中的非互质数

栈来存储已经处理的数

def replaceNonCoprimes(nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
import math
ans=[]
for num in nums:
while ans:
g=math.gcd(ans[-1], num)
if g>1:
num=ans[-1]*num//g
ans.pop()
else:
break
ans.append(num)
return ans

9/17 2349. 设计数字容器系统

用一个map存储当前每个位置存储的数值
用一个map存储每个数值的一个heap 存储的位置

import heapq
from collections import defaultdict
class NumberContainers(object):
def __init__(self):
self.num={}
self.h=defaultdict(list)
def change(self, index, number):
"""
:type index: int
:type number: int
:rtype: None
"""
self.num[index]=number
heapq.heappush(self.h[number],index)
def find(self, number):
"""
:type number: int
:rtype: int
"""
while self.h[number]:
ind=self.h[number][0]
if self.num[ind]==number:
return ind
else:
heapq.heappop(self.h[number])
return -1

9/18 3408. 设计任务管理器

map存储每个任务信息
最大堆存放优先级最高任务

import heapq
class TaskManager(object):
def __init__(self, tasks):
"""
:type tasks: List[List[int]]
"""
self.info={}
self.h=[]
for u,t,p in tasks:
self.info[t]=[p,u]
heapq.heappush(self.h, [-p,-t])
def add(self, userId, taskId, priority):
"""
:type userId: int
:type taskId: int
:type priority: int
:rtype: None
"""
self.info[taskId]=[priority,userId]
heapq.heappush(self.h, [-priority,-taskId])
def edit(self, taskId, newPriority):
"""
:type taskId: int
:type newPriority: int
:rtype: None
"""
self.info[taskId][0]=newPriority
heapq.heappush(self.h, [-newPriority,-taskId])
def rmv(self, taskId):
"""
:type taskId: int
:rtype: None
"""
self.info.pop(taskId)
def execTop(self):
"""
:rtype: int
"""
while self.h:
p,t=heapq.heappop(self.h)
p,t=-p,-t
if p==self.info.get(t,[-1,-1])[0]:
return self.info.pop(t)[1]
return -1

9/19 3484. 设计电子表格

使用map存放单元格的值

class Spreadsheet(object):
def __init__(self, rows):
"""
:type rows: int
"""
self.m={}
def setCell(self, cell, value):
"""
:type cell: str
:type value: int
:rtype: None
"""
self.m[cell]=value
def resetCell(self, cell):
"""
:type cell: str
:rtype: None
"""
self.m[cell]=0
def getValue(self, formula):
"""
:type formula: str
:rtype: int
"""
v=formula[1:]
l=v.split('+')
v1,v2=0,0
if l[0].isnumeric():
v1=int(l[0])
else:
v1=self.m.get(l[0],0)
if l[1].isnumeric():
v2=int(l[1])
else:
v2=self.m.get(l[1],0)
return v1+v2

9/20 3508. 设计路由器

  1. 设计路由器
    l为当前队列 mem记录是否已经存在
    dest记录目的地
from collections import defaultdict
from sortedcontainers import SortedList
class Router(object):
def __init__(self, memoryLimit):
"""
:type memoryLimit: int
"""
self.l=[]
self.mem=set()
self.limit=memoryLimit
self.dest=defaultdict(SortedList)
def addPacket(self, source, destination, timestamp):
"""
:type source: int
:type destination: int
:type timestamp: int
:rtype: bool
"""
value =(source,destination,timestamp)
if value in self.mem:
return False
if len(self.l)==self.limit:
self.forwardPacket()
self.mem.add(value)
self.dest[destination].add(timestamp)
self.l.append(value)
return True
def forwardPacket(self):
"""
:rtype: List[int]
"""
data=[]
if len(self.l)>0:
value = self.l.pop(0)
data = list(value)
self.mem.remove(value)
self.dest[data[1]].remove(data[2])
return data
def getCount(self, destination, startTime, endTime):
"""
:type destination: int
:type startTime: int
:type endTime: int
:rtype: int
"""
if destination not in self.dest:
return 0
l = self.dest[destination]
left=l.bisect_left(startTime)
right=l.bisect_right(endTime)
return right-left

9/21 1912. 设计电影租借系统

price记录店铺、电影的价格
valid[m]记录电影m 在店铺s内的价格p
rt记录被租借的电影

from collections import defaultdict
from sortedcontainers import SortedList
class MovieRentingSystem(object):
def __init__(self, n, entries):
"""
:type n: int
:type entries: List[List[int]]
"""
self.price={}
self.valid=defaultdict(SortedList)
self.rt=SortedList()
for s,m,p in entries:
self.price[(s,m)]=p
self.valid[m].add((p,s))
def search(self, movie):
"""
:type movie: int
:rtype: List[int]
"""
if movie not in self.valid:
return []
return [s for (p,s) in self.valid[movie][:5]]
def rent(self, shop, movie):
"""
:type shop: int
:type movie: int
:rtype: None
"""
p = self.price[(shop,movie)]
self.valid[movie].discard((p,shop))
self.rt.add((p,shop,movie))
def drop(self, shop, movie):
"""
:type shop: int
:type movie: int
:rtype: None
"""
p=self.price[(shop,movie)]
self.valid[movie].add((p,shop))
self.rt.discard((p,shop,movie))
def report(self):
"""
:rtype: List[List[int]]
"""
return [(s,m) for p,s,m in self.rt[:5]]

posted on 2025-10-12 15:07  ycfenxi  阅读(5)  评论(0)    收藏  举报