English Explanation: kzbin.info/www/bejne/p6KceKl5gLCBocU
@darshankumar554617 сағат бұрын
Hindi Explanation: kzbin.info/www/bejne/p6KceKl5gLCBocU
@darshankumar554617 сағат бұрын
class Solution: def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]: graph={} for i in range(n-1): graph[i]=[i+1] print(graph) def bfs(graph): nonlocal n queue=deque([(0,0)]) visited=set() visited.add(0) print(queue) while(queue): node,path=queue.popleft() for neighbour in graph[node]: if(neighbour not in visited): if(neighbour==n-1): return path+1 queue.append((neighbour,path+1)) visited.add(neighbour) print(queue) return -1 ans=[] for i,j in queries: print(f"adding edge{i}->{j}") graph[i].append(j) ans.append(bfs(graph)) return ans
@darshankumar554617 сағат бұрын
class Solution: def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]: graph={} for i in range(n-1): graph[i]=[i+1] print(graph) def bfs(graph): nonlocal n queue=deque([(0,0)]) visited=set() visited.add(0) print(queue) while(queue): node,path=queue.popleft() for neighbour in graph[node]: if(neighbour not in visited): if(neighbour==n-1): return path+1 queue.append((neighbour,path+1)) visited.add(neighbour) print(queue) return -1 ans=[] for i,j in queries: print(f"adding edge{i}->{j}") graph[i].append(j) ans.append(bfs(graph)) return ans
@lowspecster9236Күн бұрын
Good solution 👍 Need more videos like this.
@darshankumar5546Күн бұрын
English Explanation: kzbin.info/www/bejne/nX_acmR3f66Hgtk
@darshankumar5546Күн бұрын
Hindi Explanation: kzbin.info/www/bejne/r6qzmJ2siqifbbM
@darshankumar5546Күн бұрын
class Solution: def findChampion(self, n: int, edges: List[List[int]]) -> int: winner=set([i for i in range(n)]) looser=set() for i,j in edges: looser.add(j) ans=winner-looser print(ans) ansLen=len(ans) if(ansLen>1): return -1 return ans.pop()
@darshankumar5546Күн бұрын
class Solution: def findChampion(self, n: int, edges: List[List[int]]) -> int: winner=set([i for i in range(n)]) looser=set() for i,j in edges: looser.add(j) ans=winner-looser print(ans) ansLen=len(ans) if(ansLen>1): return -1 return ans.pop()
@GlynnisGarabedianКүн бұрын
Thanks for the analysis! I need some advice: My OKX wallet holds some USDT, and I have the seed phrase. (alarm fetch churn bridge exercise tape speak race clerk couch crater letter). Could you explain how to move them to Binance?
@darshankumar5546Күн бұрын
just do a google search, there are multiple videos, available in youtube, as well
@MohamedAasif-vb9xi2 күн бұрын
Good❤
@darshankumar55462 күн бұрын
English Explanation: kzbin.info/www/bejne/q6OnkKOvfdmHqtE
@darshankumar55462 күн бұрын
Hindi Explanation: kzbin.info/www/bejne/qYW5cqRml6ishMk
Bro you are doing good but i hope you will continue till you reach the end of leetcode
@darshankumar55464 күн бұрын
Hindi Explanation: kzbin.info/www/bejne/fn21iaylf86qa5Y
@darshankumar55464 күн бұрын
English Explanation: kzbin.info/www/bejne/d4PRc4GkaJeYg9U
@darshankumar55464 күн бұрын
//java import java.lang.Math; class Solution { public long maxMatrixSum(int[][] matrix) { int n=matrix.length; int countOfNegEle=0; long positiveSum=0; int smallestEle=Integer.MAX_VALUE; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ int ele=matrix[i][j]; if(ele<0){ countOfNegEle+=1; ele=ele*(-1); } positiveSum+=ele; smallestEle=Math.min(smallestEle,ele); } } if(countOfNegEle%2==0){ return positiveSum; } return positiveSum-(smallestEle*2); } }
@darshankumar55464 күн бұрын
//java import java.lang.Math; class Solution { public long maxMatrixSum(int[][] matrix) { int n=matrix.length; int countOfNegEle=0; long positiveSum=0; int smallestEle=Integer.MAX_VALUE; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ int ele=matrix[i][j]; if(ele<0){ countOfNegEle+=1; ele=ele*(-1); } positiveSum+=ele; smallestEle=Math.min(smallestEle,ele); } } if(countOfNegEle%2==0){ return positiveSum; } return positiveSum-(smallestEle*2); } }
@darshankumar55464 күн бұрын
# python # TC=O(n2) # SC=O(1) class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: n=len(matrix) countOfNegEle=0 positiveSum=0 smallestEle=float('inf') for i in range(n): for j in range(n): ele=matrix[i][j] if(ele<0): countOfNegEle+=1 ele=ele*(-1) positiveSum+=ele smallestEle=min(smallestEle,ele) if(countOfNegEle%2==0): return positiveSum return positiveSum-(smallestEle*2) # [ # [1,2,3], # [1,2,3], # [1,2,3] # ]
@darshankumar55464 күн бұрын
# python # TC=O(n2) # SC=O(1) class Solution: def maxMatrixSum(self, matrix: List[List[int]]) -> int: n=len(matrix) countOfNegEle=0 positiveSum=0 smallestEle=float('inf') for i in range(n): for j in range(n): ele=matrix[i][j] if(ele<0): countOfNegEle+=1 ele=ele*(-1) positiveSum+=ele smallestEle=min(smallestEle,ele) if(countOfNegEle%2==0): return positiveSum return positiveSum-(smallestEle*2) # [ # [1,2,3], # [1,2,3], # [1,2,3] # ]
@darshankumar55464 күн бұрын
Hindi Explanation: kzbin.info/www/bejne/faG0aYR3qcqWjc0si=ryQhALiDZ8itE958
@darshankumar55464 күн бұрын
English Explanation: kzbin.info/www/bejne/mKDXlYupfpmXpq8si=RAwzrjNjdMi4OLdB
@darshankumar55464 күн бұрын
# Approach-2 OPTIMAL # TC=O(m*n) # SC=O(1) class Solution: def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]: m=len(box) n=len(box[0]) rotatedBox=[['' for _ in range(m)] for _ in range(n)] #print(rotatedBox) #arrange stones for row in box: lowestEmptySpace=-1 for i in range(n-1,-1,-1): #empty if(row[i]=='.'): lowestEmptySpace=max(i,lowestEmptySpace) #obstacle elif(row[i]=='*'): lowestEmptySpace=-1 #stone else: #check if it has empty places towards right if(lowestEmptySpace!=-1): row[lowestEmptySpace]='#' #mark index 'i' as empty row[i]='.' #move pointer above by 1, from previous loc lowestEmptySpace-=1 print(box) for i in range(m): for j in range(n): rotatedBox[j][m-1-i]=box[i][j] return rotatedBox # 00 01 02 03 # 10 11 12 13 # 20 21 22 23 # 00 01 02 # 10 11 12 # 20 21 22 # 30 31 32
@darshankumar55464 күн бұрын
# Approach-2 OPTIMAL # TC=O(m*n) # SC=O(1) class Solution: def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]: m=len(box) n=len(box[0]) rotatedBox=[['' for _ in range(m)] for _ in range(n)] #print(rotatedBox) #arrange stones for row in box: lowestEmptySpace=-1 for i in range(n-1,-1,-1): #empty if(row[i]=='.'): lowestEmptySpace=max(i,lowestEmptySpace) #obstacle elif(row[i]=='*'): lowestEmptySpace=-1 #stone else: #check if it has empty places towards right if(lowestEmptySpace!=-1): row[lowestEmptySpace]='#' #mark index 'i' as empty row[i]='.' #move pointer above by 1, from previous loc lowestEmptySpace-=1 print(box) for i in range(m): for j in range(n): rotatedBox[j][m-1-i]=box[i][j] return rotatedBox # 00 01 02 03 # 10 11 12 13 # 20 21 22 23 # 00 01 02 # 10 11 12 # 20 21 22 # 30 31 32
@darshankumar55464 күн бұрын
# Approach -1 # TC=O(m*n*log(n)) # SC=O(n) import heapq class Solution: def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]: m=len(box) n=len(box[0]) rotatedBox=[['' for _ in range(m)] for _ in range(n)] #print(rotatedBox) #arrange stones for row in box: maxHeap=[] for i in range(n-1,-1,-1): #empty if(row[i]=='.'): heapq.heappush(maxHeap,-i) #obstacle elif(row[i]=='*'): maxHeap=[] #stone else: #check if it has empty places towards right if(len(maxHeap)!=0): index=heapq.heappop(maxHeap)*(-1) row[index]='#' #mark index 'i' as empty row[i]='.' heapq.heappush(maxHeap,-i) print(box) for i in range(m): for j in range(n): rotatedBox[j][m-1-i]=box[i][j] return rotatedBox # 00 01 02 03 # 10 11 12 13 # 20 21 22 23 # 00 01 02 # 10 11 12 # 20 21 22 # 30 31 32
@darshankumar55464 күн бұрын
# Approach -1 # TC=O(m*n*log(n)) # SC=O(n) import heapq class Solution: def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]: m=len(box) n=len(box[0]) rotatedBox=[['' for _ in range(m)] for _ in range(n)] #print(rotatedBox) #arrange stones for row in box: maxHeap=[] for i in range(n-1,-1,-1): #empty if(row[i]=='.'): heapq.heappush(maxHeap,-i) #obstacle elif(row[i]=='*'): maxHeap=[] #stone else: #check if it has empty places towards right if(len(maxHeap)!=0): index=heapq.heappop(maxHeap)*(-1) row[index]='#' #mark index 'i' as empty row[i]='.' heapq.heappush(maxHeap,-i) print(box) for i in range(m): for j in range(n): rotatedBox[j][m-1-i]=box[i][j] return rotatedBox # 00 01 02 03 # 10 11 12 13 # 20 21 22 23 # 00 01 02 # 10 11 12 # 20 21 22 # 30 31 32
@darshankumar55465 күн бұрын
Proud to say, my solution is better than, leetcode's official solution😇
@darshankumar55465 күн бұрын
Proud to say, my solution is better than, leetcode's official solution😇
@darshankumar55465 күн бұрын
Hindi Explanation: kzbin.info/www/bejne/inquh3iqm9-dnsk
@darshankumar55465 күн бұрын
# solution better than leetcode's official solution class Solution: def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int: m=len(matrix) n=len(matrix[0]) nums=[] for num in matrix: l=len(num) val=0 for i in range(l): val+=(num[i])*(2**(l-i-1)) nums.append(val) print(nums) visited=set() eligible=set([0,(2**(n))-1]) MaxAns=0 for i in range(m): if(i in visited): continue ans=1 for j in range(i+1,m): if(j in visited): continue if((nums[i]^nums[j]) in eligible): ans+=1 visited.add(j) MaxAns=max(MaxAns,ans) visited.add(i) print(f"{ans} nos has same BITpattern as that of:{nums[i]}") return MaxAns # [ # [0,0,0], # [0,0,1], # [1,1,0] # ] # [0,0,1] # -->[0,0,1] # -->[1,1,0] # xor result shld be: # [0,0,0] or [1,1,1] # decimal: 0 or 7 (2**3 -1)
@darshankumar55465 күн бұрын
English Explanation: kzbin.info/www/bejne/gZvJlnZuopmrb7c
@darshankumar55465 күн бұрын
# SOLUTION better than leetcode's official solution: class Solution: def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int: m=len(matrix) n=len(matrix[0]) nums=[] for num in matrix: l=len(num) val=0 for i in range(l): val+=(num[i])*(2**(l-i-1)) nums.append(val) print(nums) visited=set() eligible=set([0,(2**(n))-1]) MaxAns=0 for i in range(m): if(i in visited): continue ans=1 for j in range(i+1,m): if(j in visited): continue if((nums[i]^nums[j]) in eligible): ans+=1 visited.add(j) MaxAns=max(MaxAns,ans) visited.add(i) print(f"{ans} nos has same BITpattern as that of:{nums[i]}") return MaxAns # [ # [0,0,0], # [0,0,1], # [1,1,0] # ] # [0,0,1] # -->[0,0,1] # -->[1,1,0] # xor result shld be: # [0,0,0] or [1,1,1] # decimal: 0 or 7 (2**3 -1)
@darshankumar55466 күн бұрын
English Explanation: kzbin.info/www/bejne/bJSthoeqe82nacU
@darshankumar55466 күн бұрын
Hindi Explanation: kzbin.info/www/bejne/b6arlXaGitKepsk
@darshankumar55466 күн бұрын
# TC=O(m.n+g(m+n)) # SC=O(M.n) class Solution: def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int: grid=[[0 for _ in range(n)] for _ in range(m)] # mark the walls for i,j in walls: grid[i][j]=-1 for i,j in guards: grid[i][j]=-1 def markGuardedCells(i,j): nonlocal grid print(i,j) #go right for e in range(j+1,n): print('right->',i,e) if(grid[i][e]==-1): break elif(grid[i][e]==1): pass else: grid[i][e]=1 #go left for e in range(j-1,-1,-1): print('left->',i,e) if(grid[i][e]==-1): break elif(grid[i][e]==1): pass else: grid[i][e]=1 #go up for e in range(i-1,-1,-1): print('up->',e,j) if(grid[e][j]==-1): break elif(grid[e][j]==1): pass else: grid[e][j]=1 #go down for e in range(i+1,m): print('down->',e,j) if(grid[e][j]==-1): break elif(grid[e][j]==1): pass else: grid[e][j]=1 for i,j in guards: markGuardedCells(i,j) print(grid) ans=0 for i in range(m): for j in range(n): if(grid[i][j]==0): ans+=1 return ans
@darshankumar55466 күн бұрын
# TC=O(m.n+g(m+n)) # SC=O(M.n) class Solution: def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int: grid=[[0 for _ in range(n)] for _ in range(m)] # mark the walls for i,j in walls: grid[i][j]=-1 for i,j in guards: grid[i][j]=-1 def markGuardedCells(i,j): nonlocal grid print(i,j) #go right for e in range(j+1,n): print('right->',i,e) if(grid[i][e]==-1): break elif(grid[i][e]==1): pass else: grid[i][e]=1 #go left for e in range(j-1,-1,-1): print('left->',i,e) if(grid[i][e]==-1): break elif(grid[i][e]==1): pass else: grid[i][e]=1 #go up for e in range(i-1,-1,-1): print('up->',e,j) if(grid[e][j]==-1): break elif(grid[e][j]==1): pass else: grid[e][j]=1 #go down for e in range(i+1,m): print('down->',e,j) if(grid[e][j]==-1): break elif(grid[e][j]==1): pass else: grid[e][j]=1 for i,j in guards: markGuardedCells(i,j) print(grid) ans=0 for i in range(m): for j in range(n): if(grid[i][j]==0): ans+=1 return ans
@darshankumar55467 күн бұрын
English Explanation: kzbin.info/www/bejne/mHPTmXidoqZ5b6c
@darshankumar55467 күн бұрын
HINDI explanation: kzbin.info/www/bejne/hJzOZGODmq-CpbM
@darshankumar55467 күн бұрын
class Solution: def takeCharacters(self, s: str, k: int) -> int: charFreq_Array=[0,0,0] for i in s: charFreq_Array[ord(i)-ord('a')]+=1 print(charFreq_Array) if(min(charFreq_Array)<k): return -1 charFreq_window=[0,0,0] start=0 maxWindowLen=0 n=len(s) for end in range(n): index=ord(s[end])-ord('a') charFreq_window[index]+=1 print(s[start:end+1],start,end,charFreq_window,charFreq_Array) while((charFreq_Array[index]-charFreq_window[index])<k and (start<=end)): charFreq_window[ord(s[start])-ord('a')]-=1 start+=1 print(f"removing 1st elelemt of window, till {chr(index+ord('a'))} is removed:",s[start:end+1]) maxWindowLen=max(maxWindowLen,end-start+1) print('max Len of window seen till now=',maxWindowLen) return n-maxWindowLen # s = "aabaaaacaabc", k = 2 # [a-8, b-2,c-2] # window=[] curWinLen=4 maxWinLen=4 # n=12 # 12-4=8 # # n
@darshankumar55467 күн бұрын
class Solution: def takeCharacters(self, s: str, k: int) -> int: charFreq_Array=[0,0,0] for i in s: charFreq_Array[ord(i)-ord('a')]+=1 print(charFreq_Array) if(min(charFreq_Array)<k): return -1 charFreq_window=[0,0,0] start=0 maxWindowLen=0 n=len(s) for end in range(n): index=ord(s[end])-ord('a') charFreq_window[index]+=1 print(s[start:end+1],start,end,charFreq_window,charFreq_Array) while((charFreq_Array[index]-charFreq_window[index])<k and (start<=end)): charFreq_window[ord(s[start])-ord('a')]-=1 start+=1 print(f"removing 1st elelemt of window, till {chr(index+ord('a'))} is removed:",s[start:end+1]) maxWindowLen=max(maxWindowLen,end-start+1) print('max Len of window seen till now=',maxWindowLen) return n-maxWindowLen # s = "aabaaaacaabc", k = 2 # [a-8, b-2,c-2] # window=[] curWinLen=4 maxWinLen=4 # n=12 # 12-4=8 # # n
@darshankumar55468 күн бұрын
English Explanation: kzbin.info/www/bejne/eH_ainx3mNqfiK8
@darshankumar55468 күн бұрын
Hindi explanation: kzbin.info/www/bejne/l4nGfnSKZqyefqc
@darshankumar55468 күн бұрын
class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: n=len(nums) a={} maxSubArraySum=0 SubArraySum=0 begin=0 end=0 i=0 while(i<n): #window has space to insert the new ele if(nums[i] in a): print(f"{nums[i]} already in window, remove elements from start of window, till {nums[i]} is present") while(begin<a[nums[i]]): print(f"removing {nums[begin]} of index:{begin} from window") SubArraySum-=nums[begin] a.pop(nums[begin]) begin+=1 print('window=',nums[begin:end]) SubArraySum-=nums[begin] print(f"removing {nums[begin]} of index:{begin} from window") a.pop(nums[begin]) begin+=1 a[nums[i]]=i end+=1 SubArraySum+=nums[i] print('window=',nums[begin:end],f"begin={begin},end={end}",a) #if window is already having K elements, make space for next element if((end-begin)==k): maxSubArraySum=max(maxSubArraySum,SubArraySum) print(f"max Len-K sub array sum={maxSubArraySum}; curreent subArray sum={SubArraySum}") SubArraySum-=nums[begin] print(f"removing {nums[begin]} of index:{begin} from window,to make space for next ele") a.pop(nums[begin]) begin+=1 print('window=',nums[begin:end]) i+=1 return maxSubArraySum # nums = [1,5,4,2,9,9,9], k = 3 # window=[9] maxSubArraySum(len_k) =15
@darshankumar55468 күн бұрын
class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: n=len(nums) a={} maxSubArraySum=0 SubArraySum=0 begin=0 end=0 i=0 while(i<n): #window has space to insert the new ele if(nums[i] in a): print(f"{nums[i]} already in window, remove elements from start of window, till {nums[i]} is present") while(begin<a[nums[i]]): print(f"removing {nums[begin]} of index:{begin} from window") SubArraySum-=nums[begin] a.pop(nums[begin]) begin+=1 print('window=',nums[begin:end]) SubArraySum-=nums[begin] print(f"removing {nums[begin]} of index:{begin} from window") a.pop(nums[begin]) begin+=1 a[nums[i]]=i end+=1 SubArraySum+=nums[i] print('window=',nums[begin:end],f"begin={begin},end={end}",a) #if window is already having K elements, make space for next element if((end-begin)==k): maxSubArraySum=max(maxSubArraySum,SubArraySum) print(f"max Len-K sub array sum={maxSubArraySum}; curreent subArray sum={SubArraySum}") SubArraySum-=nums[begin] print(f"removing {nums[begin]} of index:{begin} from window,to make space for next ele") a.pop(nums[begin]) begin+=1 print('window=',nums[begin:end]) i+=1 return maxSubArraySum # nums = [1,5,4,2,9,9,9], k = 3 # window=[9] maxSubArraySum(len_k) =15
@darshankumar55469 күн бұрын
if you felt, this question is easy, try this Day 17 question : kzbin.info/www/bejne/p4G5lYdojJuSqKM ( very proud to say my solution is even more optimal than official leetcode solution, see the video for day 17 for details )
@darshankumar55469 күн бұрын
if you solved this question easily, try this question: kzbin.info/www/bejne/p4G5lYdojJuSqKM
@darshankumar55469 күн бұрын
ENGLISH explanation: kzbin.info/www/bejne/jmWUqGV4bt6rldUsi=Zv7LJ2kXLv3ZEKgp
@darshankumar55469 күн бұрын
HINDI explanation: kzbin.info/www/bejne/bXKrZ4Jsi7WDba8