当前位置: 首页 > news >正文

LeetCode 每日一题 2025/6/2-2025/6/8

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


目录

      • 6/2 135. 分发糖果
      • 6/3 1298. 你能从盒子里获得的最大糖果数
      • 6/4 3403. 从盒子中找出字典序最大的字符串 I
      • 6/5 1061. 按字典序排列最小的等效字符串
      • 6/6 2434. 使用机器人打印字典序最小的字符串
      • 6/7 3170. 删除星号以后字典序最小的字符串
      • 6/8


6/2 135. 分发糖果

初始化每人一个糖
从1开始遍历队列 与前一个位置比较如果 分数比前一个位置大 那么糖的数量为前一个位置+1
倒序队列和糖果list 再一次从1开始遍历队列 相当于倒的在比较一次
此时如果分数比前一个位置大 那么糖的数量为max(当前数量,前一个位置+1)
最后求糖果list的和

def candy(ratings):""":type ratings: List[int]:rtype: int"""l = len(ratings)if l==0:return 0candys =[1]*lfor i in range(1,l):if ratings[i]>ratings[i-1]:candys[i] = candys[i-1]+1candys.reverse()ratings.reverse()for i in range(1,l):if ratings[i]>ratings[i-1]:candys[i] = max(candys[i],candys[i-1]+1)return sum(candys)def candy2(ratings):""":type ratings: List[int]:rtype: int"""n = len(ratings)if n==0:return 0candy = [1]*nfor i in range(1,n):if ratings[i]>ratings[i-1]:candy[i] = candy[i-1]+1for i in range(n-1,0,-1):if ratings[i]<ratings[i-1]:candy[i-1] = max(candy[i-1],candy[i]+1)return sum(candy)

6/3 1298. 你能从盒子里获得的最大糖果数

def maxCandies(status, candies, keys, containedBoxes, initialBoxes):""":type status: List[int]:type candies: List[int]:type keys: List[List[int]]:type containedBoxes: List[List[int]]:type initialBoxes: List[int]:rtype: int"""import collectionsn=len(status)canopen = [status[i]==1 for i in range(n)]hasbox,used=[False]*n,[False]*nl=collections.deque()ans=0for box in initialBoxes:hasbox[box]=Trueif canopen[box]:l.append(box)used[box]=Trueans+=candies[box]while len(l)>0:cur = l.popleft()for k in keys[cur]:canopen[k]=Trueif not used[k] and hasbox[k]:l.append(k)used[k]=Trueans+=candies[k]for box in containedBoxes[cur]:hasbox[box]=Trueif not used[box] and canopen[box]:l.append(box)used[box]=Trueans+=candies[box]return ans

6/4 3403. 从盒子中找出字典序最大的字符串 I

find找到整个字符串中字典序最大的的字符串
根据朋友数量看是否需要去掉一部分尾部

def answerString(word, numFriends):""":type word: str:type numFriends: int:rtype: str"""def find(s):i,j=0,1n=len(s)while j<n:k=0while j+k<n and s[i+k]==s[j+k]:k+=1if j+k<n and s[i+k]<s[j+k]:i,j=j,max(j+1,i+k+1)else:j=j+k+1return s[i:]if numFriends==1:return wordsub=find(word)n=len(word)m=len(sub)return sub[:min(m,n-numFriends+1)]

6/5 1061. 按字典序排列最小的等效字符串

并查集

def smallestEquivalentString(s1, s2, baseStr):""":type s1: str:type s2: str:type baseStr: str:rtype: str"""class UnionFind:def __init__(self,n):self.l = list(range(n))def find(self,x):if self.l[x]!=x:self.l[x]=self.find(self.l[x])return self.l[x]def union(self,x,y):x=self.find(x)y=self.find(y)if x==y:returnif x>y:x,y=y,xself.l[y]=xuf=UnionFind(26)for x,y in zip(s1,s2):uf.union(ord(x)-ord('a'), ord(y)-ord('a'))return ''.join(chr(ord('a')+uf.find(ord(c)-ord('a'))) for c in baseStr)

6/6 2434. 使用机器人打印字典序最小的字符串

将s中的字符依次入栈
如果当前栈顶的字符小于等于 剩余字符中最小的
则可以出栈

def robotWithString(s):""":type s: str:rtype: str"""from collections import Counter cnt = Counter(s)st=[]ret=[]minc='a'for c in s:st.append(c)cnt[c]-=1while minc!='z' and cnt[minc]==0:minc = chr(ord(minc)+1)while st and st[-1]<=minc:ret.append(st.pop())return ''.join(ret)

6/7 3170. 删除星号以后字典序最小的字符串

用一个26位的list 记录每个字符的位置
遇到*时 从小到大找到最小字典序字符 去除最靠后位置的字符

def clearStars(s):""":type s: str:rtype: str"""l=list(s)cnt = [[] for _ in range(26)]for i,c in enumerate(s):if c!='*':cnt[ord(c)-ord('a')].append(i)else:for j in range(26):if cnt[j]:l[cnt[j].pop()]='*'breakreturn ''.join(c for c in l if c!='*')

6/8


相关文章:

  • Redis故障转移
  • YOLOv8 升级之路:主干网络嵌入 SCINet,优化黑暗环境目标检测
  • 《绩效管理》要点总结与分享
  • Deepseek基座:Deepseek-v2核心内容解析
  • 优化器 (torch.optim) 与学习率调度器 (lr_scheduler)
  • [学习] GNSS信号跟踪环路原理、设计与仿真(仿真代码)
  • 随访系统安装的记录
  • 手机号段数据库与网络安全应用
  • NPOI Excel用OLE对象的形式插入文件附件以及插入图片
  • 用电脑通过USB总线连接控制keysight示波器
  • Android第十五次面试总结(第三方组件和adb命令)
  • 嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
  • JAVA学习 DAY3 注释与编码规范讲解
  • Kerberos面试内容整理-未来发展趋势
  • SQL进阶之旅 Day 20:锁与并发控制技巧
  • 霍夫变换(Hough Transform)原理简要介绍
  • 基于51单片机的多功能洗衣机仿真
  • 食品计算—Food Portion Estimation via 3D Object Scaling
  • 力扣HOT100之二分查找:153. 寻找旋转排序数组中的最小值
  • 第二十八课:深度学习及pytorch简介
  • 网站开发前台和后台/营销培训机构哪家最专业
  • wordpress 仿站步骤/seowhy官网
  • 中山网站建设公司/定制网站建设
  • 电商网站seo公司/做什么推广最赚钱
  • 广州网站建设费用/武汉seo搜索引擎
  • 网站建设电话销售话术实例/莱阳seo外包