Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1.BFS
- class Solution:
- def canReach(self, s: str, mx: int, my: int) -> bool:
- q=deque()
- far=0
- n=len(s)
- q.append(0)
- while q:
- t=q.popleft()
- if t==n-1:return True
- for x in range(max(t+mx,far+1),min(n,t+my+1)):
- if s[x]!='1':
- q.append(x)
- if x==n-1:return True
- far=t+my
- return False
- '''
- Here reach tells how many starting points in the current sliding window helped you to reach current index
- 2.DP+SLIDING WINDOW'''
- class Solution:
- def canReach(self, s: str, mx: int, my: int) -> bool:
- n=len(s)
- dp=[False]*n
- reach=0 #reachable zeroes
- dp[0]=(s[0]=='0')
- for i in range(1,n):
- #at index i window will be from i-maxjumps,i-minjumps
- if i>=mx:#if i-mx is +ve if dp[i-mx] is reachable
- reach = reach+(1 if dp[i-mx] else 0)#why we are adding? Because ith index is added to our sliding window.
- if i>my:
- reach = reach-(1 if dp[i-my-1] else 0) #why we substract because we are leaving i-maxjumps index from our sliding window
- if s[i]=='0' and reach>0:#If current value at index is 0 and if reachable 0s are >0 then we can reach from sliding window (i-maxjumps,i-minjumps)
- dp[i]=True
- else:
- dp[i]=False
- return dp[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement