記錄了初步解題思路 以及本地實現代碼;并不一定為最優 也希望大家能一起探討 一起進步
目錄
- 4/28 2302. 統計得分小于 K 的子數組數目
- 4/29 2962. 統計最大元素出現至少 K 次的子數組
- 4/30 1295. 統計位數為偶數的數字
- 5/1 2071. 你可以安排的最多任務數目
- 5/2 838. 推多米諾
- 5/3 1007. 行相等的最少多米諾旋轉
- 5/4 1128. 等價多米諾骨牌對的數量
4/28 2302. 統計得分小于 K 的子數組數目
滑動窗口 固定右側端點r
找到左側端點l 使得[l,r]第一次滿足條件
那么以l右側的位置為左端點必定滿足
def countSubarrays(nums, k):""":type nums: List[int]:type k: int:rtype: int"""n=len(nums)ans=0total=0l=0for r in range(n):total+=nums[r]while l<=r and total*(r-l+1)>=k:total-=nums[l]l+=1ans+=r-l+1return ans
4/29 2962. 統計最大元素出現至少 K 次的子數組
遍歷記錄最大元素出現的位置 ind
對于最大元素位置i1 在(i0,i1]間的所有位置都需要到ik為止才能滿足
此時有(i1-i0)*(n-ik)個子數組
def countSubarrays(nums, k):""":type nums: List[int]:type k: int:rtype: int"""n=len(nums)maxv=max(nums)ind=[-1]for i in range(n):if nums[i]==maxv:ind.append(i)l,r=1,kans=0while r<len(ind):ans+=(ind[l]-ind[l-1])*(n-ind[r])l+=1r+=1return ans
4/30 1295. 統計位數為偶數的數字
依次判斷
def findNumbers(nums):""":type nums: List[int]:rtype: int"""def check(num):b = 0while num>0:num=num//10b+=1return b%2==0ans = 0for num in nums:if check(num):ans+=1return ans
5/1 2071. 你可以安排的最多任務數目
假設完成k個任務 選擇k個值最小的任務 和k個力量最大的工人
二分來找到最大的k
check(mid)用來判斷mid個是否滿足
def maxTaskAssign(tasks, workers, pills, strength):""":type tasks: List[int]:type workers: List[int]:type pills: int:type strength: int:rtype: int"""from sortedcontainers import SortedListn=len(tasks)m=len(workers)tasks.sort()workers.sort()def check(mid):p=pillswk=SortedList(workers[m-mid:])for i in range(mid-1,-1,-1):if wk[-1]>=tasks[i]:wk.pop()else:if p==0:return Falserep=wk.bisect_left(tasks[i]-strength)if rep==len(wk):return Falsep-=1wk.pop(rep)return Truel,r,ans=1,min(m,n),0while l<=r:mid=(l+r)//2if check(mid):ans=midl=mid+1else:r=mid-1return ans
5/2 838. 推多米諾
廣搜BFS
使用l,r兩個集合記錄當前向左向右傾倒的位置
每一個向左的位置-1 如果位置上的骨牌狀態為.則暫時標記可以傾倒
向右的一樣
判斷向左向右傾倒的位置是否有重復 如果有重復
這個位置將不會傾倒 去除這些位置
將可以傾倒的位置標記后 下一輪重新操作
def pushDominoes(dominoes):""":type dominoes: str:rtype: str"""dmn = list(dominoes)l,r = set(),set()for loc,c in enumerate(dmn):if c=="R":r.add(loc)elif c=="L":l.add(loc)n = len(dominoes)while l or r:tmpl,tmpr = set(),set()for loc in l:tmp = loc-1if tmp>=0 and dmn[tmp]==".":tmpl.add(tmp)for loc in r:tmp = loc+1if tmp<n and dmn[tmp]==".":tmpr.add(tmp)same = tmpl&tmprtmpl -= sametmpr -= same for loc in tmpl:dmn[loc]="L"for loc in tmpr:dmn[loc]="R"l = tmplr = tmprreturn "".join(dmn)
5/3 1007. 行相等的最少多米諾旋轉
遍歷記錄數值在top出現的次數t[x] 在bottoms出現b[x]次
并統計每個位置數值出現次數nums[x]如果某個位置top,bottoms相同 則只在nums中統計一次
如果需要滿足條件則必定存在某個數值x nums[x]=len(tops)
如果交換到top需要n-t[x]次 到下層需要n-b[x] 取小值
def minDominoRotations(tops, bottoms):""":type tops: List[int]:type bottoms: List[int]:rtype: int"""n=len(tops)nums=[0]*7t,b=[0]*7,[0]*7for i in range(n):t[tops[i]]+=1b[bottoms[i]]+=1nums[tops[i]]+=1if tops[i]!=bottoms[i]:nums[bottoms[i]]+=1for i in range(1,7):if nums[i]==n:return min(n-t[i],n-b[i])return -1
5/4 1128. 等價多米諾骨牌對的數量
依次遍歷
def numEquivDominoPairs(dominoes):""":type dominoes: List[List[int]]:rtype: int"""m = {}ret = 0for a,b in dominoes:if a>b:a,b=b,atmp = m.get((a,b),0)m[(a,b)] = tmp+1for v in m.values():if v>=2:ret += (v-1)*v/2return ret