Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf920b28

Browse files
committed
brasil
2 parents2ca8769 +d825ddb commitf920b28

13 files changed

+413
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# leetcode submit region begin(Prohibit modification and deletion)
2+
classSolution:
3+
defisPrefixOfWord(self,sentence:str,searchWord:str)->int:
4+
words=sentence.split(" ")
5+
N=len(words)
6+
foriinrange(N):
7+
ifwords[i].startswith(searchWord):
8+
returni+1
9+
return-1
10+
11+
12+
# leetcode submit region end(Prohibit modification and deletion)
13+
14+
15+
classCheckIfAWordOccursAsAPrefixOfAnyWordInASentence(Solution):
16+
pass
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fromtypingimportList
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
6+
7+
classSolution:
8+
defminimumSize(self,nums:List[int],maxOperations:int)->int:
9+
defok(v):
10+
ops=0
11+
forvalinnums:
12+
ops+=val//v
13+
ifnot (val%v):
14+
ops-=1
15+
ifops>maxOperations:
16+
returnFalse
17+
returnTrue
18+
19+
lo,hi=1,10**9+5
20+
whilelo<=hi:
21+
mid= (lo+hi)//2
22+
ifok(mid):
23+
hi=mid-1
24+
else:
25+
lo=mid+1
26+
27+
returnlo
28+
29+
# leetcode submit region end(Prohibit modification and deletion)
30+
31+
32+
classMinimumLimitOfBallsInABag(Solution):
33+
pass
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
fromfunctoolsimportcache
2+
fromtypingimportList
3+
4+
5+
# leetcode submit region begin(Prohibit modification and deletion)
6+
classSolution:
7+
defmaxTwoEvents(self,events:List[List[int]])->int:
8+
events.sort(key=lambdax: (x[0],x[1],-x[2]))
9+
N=len(events)
10+
MAX_ALLOWED=2
11+
12+
deffind(start):
13+
left=0
14+
right=N
15+
whileleft<right:
16+
mid= (left+right)//2
17+
ifevents[mid][0]>start:
18+
right=mid
19+
else:
20+
left=mid+1
21+
returnleft
22+
23+
@cache
24+
defgo(i,remaining):
25+
ans=0
26+
ifi>=Norremaining==0:
27+
returnans
28+
# take it
29+
next_idx=find(events[i][1])
30+
ans=events[i][2]+go(next_idx,remaining-1)
31+
# skip it
32+
ans=max(ans,go(i+1,remaining))
33+
returnans
34+
35+
returngo(0,MAX_ALLOWED)
36+
37+
38+
# leetcode submit region end(Prohibit modification and deletion)
39+
40+
41+
classTwoBestNonOverlappingEvents(Solution):
42+
pass
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
importmath
2+
fromtypingimportList
3+
4+
5+
# leetcode submit region begin(Prohibit modification and deletion)
6+
classSolution:
7+
defminimizedMaximum(self,n:int,quantities:List[int])->int:
8+
N=len(quantities)
9+
left=1
10+
right=max(quantities)
11+
12+
defgo(limit,stores,i):
13+
ifi==N:
14+
returnstores>=0
15+
to_consume=math.ceil(quantities[i]/limit)
16+
returngo(limit,stores-to_consume,i+1)
17+
18+
defgood(upper):
19+
returngo(upper,n,0)
20+
21+
whileleft<right:
22+
mid= (left+right)//2
23+
ifgood(mid):
24+
right=mid
25+
else:
26+
left=mid+1
27+
28+
returnleft
29+
30+
31+
# leetcode submit region end(Prohibit modification and deletion)
32+
33+
34+
classMinimizedMaximumOfProductsDistributedToAnyStore(Solution):
35+
pass
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
importcollections
2+
fromtypingimportList
3+
4+
5+
# leetcode submit region begin(Prohibit modification and deletion)
6+
classSolution:
7+
defvalidArrangement(self,pairs:List[List[int]])->List[List[int]]:
8+
ins=collections.defaultdict(int)
9+
outs=collections.defaultdict(int)
10+
adj= {}
11+
forsrc,dstinpairs:
12+
ifsrcinadj:
13+
adj[src].append(dst)
14+
else:
15+
adj[src]=collections.deque([dst])
16+
ins[dst]+=1
17+
outs[src]+=1
18+
19+
start=None
20+
foriinouts:
21+
ifouts[i]==ins[i]+1:
22+
start=i
23+
break
24+
ifstartisNone:
25+
start=pairs[0][0]
26+
27+
ans= []
28+
stk= [start]
29+
whilestk:
30+
ifstk[-1]inadjandlen(adj[stk[-1]])>0:
31+
stk.append(adj[stk[-1]].popleft())
32+
else:
33+
ans.append(stk.pop())
34+
35+
ans=ans[::-1]
36+
res= []
37+
foriinrange(1,len(ans)):
38+
res.append([ans[i-1],ans[i]])
39+
returnres
40+
41+
42+
# leetcode submit region end(Prohibit modification and deletion)
43+
44+
45+
classValidArrangementOfPairs(Solution):
46+
pass
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
importcollections
2+
fromtypingimportList
3+
4+
5+
# leetcode submit region begin(Prohibit modification and deletion)
6+
classSolution:
7+
defminimumObstacles(self,grid:List[List[int]])->int:
8+
N=len(grid)
9+
M=len(grid[0])
10+
q=collections.deque([(0,0,grid[0][0])])
11+
ans=N*M
12+
13+
defis_valid_idx(i,j):
14+
returni>=0andi<Nandj>=0andj<M
15+
16+
visited= [[(N*M)for_inrange(M)]for_inrange(N)]
17+
visited[0][0]=grid[0][0]
18+
19+
DIRECTIONS= [
20+
(0,1),
21+
(1,0),
22+
(0,-1),
23+
(-1,0),
24+
]
25+
whileq:
26+
size=len(q)
27+
for_inrange(size):
28+
i,j,walls=q.popleft()
29+
visited[i][j]=walls
30+
fordi,djinDIRECTIONS:
31+
ni,nj=i+di,j+dj
32+
ifis_valid_idx(ni,nj):
33+
cost=walls+grid[ni][nj]
34+
if (ni,nj)== (N-1,M-1):
35+
ans=min(ans,cost)
36+
elifcost<visited[ni][nj]:
37+
q.append((ni,nj,cost))
38+
visited[ni][nj]=cost
39+
returnans
40+
41+
42+
# leetcode submit region end(Prohibit modification and deletion)
43+
44+
45+
classMinimumObstacleRemovalToReachCorner(Solution):
46+
pass
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
importheapq
2+
importmath
3+
fromtypingimportList
4+
5+
6+
# leetcode submit region begin(Prohibit modification and deletion)
7+
classSolution:
8+
defpickGifts(self,gifts:List[int],k:int)->int:
9+
gifts= [-xforxingifts]
10+
heapq.heapify(gifts)
11+
for_inrange(k):
12+
max_n=abs(heapq.heappop(gifts))
13+
heapq.heappush(gifts,-1* (math.floor(math.sqrt(max_n))))
14+
returnsum(abs(x)forxingifts)
15+
16+
17+
# leetcode submit region end(Prohibit modification and deletion)
18+
19+
20+
classTakeGiftsFromTheRichestPile(Solution):
21+
pass
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fromtypingimportList,Optional
2+
fromfunctoolsimportcache
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
classSolution:
6+
deffindScore(self,nums:List[int])->int:
7+
8+
# leetcode submit region end(Prohibit modification and deletion)
9+
10+
11+
classFindScoreOfAnArrayAfterMarkingAllElements(Solution):
12+
pass
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fromtypingimportList
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
classSolution:
6+
deffindChampion(self,n:int,edges:List[List[int]])->int:
7+
inbound= [0]*n
8+
9+
foru,vinedges:
10+
inbound[v]+=1
11+
12+
ifinbound.count(0)>1:
13+
return-1
14+
foriinrange(n):
15+
ifinbound[i]==0:
16+
returni
17+
return-1
18+
19+
20+
# leetcode submit region end(Prohibit modification and deletion)
21+
22+
23+
classFindChampionIi(Solution):
24+
pass
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fromtypingimportList
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
classSolution:
6+
defisArraySpecial(self,nums:List[int],queries:List[List[int]])->List[bool]:
7+
N=len(nums)
8+
good= [0]*N
9+
good[0]=1
10+
foriinrange(1,N):
11+
if (nums[i-1]%2)!= (nums[i]%2):
12+
good[i]=1
13+
else:
14+
good[i]=0
15+
presum= [0]*N
16+
presum[0]=good[0]
17+
foriinrange(1,N):
18+
presum[i]=presum[i-1]+good[i]
19+
ans= []
20+
forf,tinqueries:
21+
to_remove=presum[f]
22+
total_here=presum[t]-to_remove
23+
ans.append(total_here== (t-f))
24+
returnans
25+
26+
27+
# leetcode submit region end(Prohibit modification and deletion)
28+
29+
30+
classSpecialArrayIi(Solution):
31+
pass
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
importcollections
2+
fromtypingimportList
3+
4+
5+
# leetcode submit region begin(Prohibit modification and deletion)
6+
classSolution:
7+
classSolution:
8+
defshortestDistanceAfterQueries(self,n:int,queries:List[List[int]])->List[int]:
9+
graph=collections.defaultdict(list)
10+
forxinrange(n-1):
11+
graph[x].append(x+1)
12+
13+
defBFS():
14+
level,queue=0, [0]
15+
seen=set()
16+
whilequeue:
17+
next_level= []
18+
fornodeinqueue:
19+
ifnode==n-1:
20+
returnlevel
21+
forchildingraph[node]:
22+
ifchildnotinseen:
23+
seen.add(child)
24+
next_level.append(child)
25+
queue=next_level
26+
level+=1
27+
returnlevel
28+
29+
ans= []
30+
fororig,destinqueries:
31+
graph[orig].append(dest)
32+
ans.append(BFS())
33+
returnans
34+
35+
36+
# leetcode submit region end(Prohibit modification and deletion)
37+
38+
39+
classShortestDistanceAfterRoadAdditionQueriesI(Solution):
40+
pass

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp