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