Daily Leetcode Challenge | JAN 26 | HARD | Maximum Employees to Be Invited to a Meeting

Поділитися
Вставка
  • Опубліковано 7 лют 2025
  • Daily Leetcode Challenge- January 2025 - Day 26 - Q2127
    Approach/Topic : BFS traversal for graph
    question link: leetcode.com/p...
    optimized approach beats 90% of the test cases
    Like, share and subscribe
    ***********************
    Hi, I am a SDE , at one of the Fortune 50 companies!!
    The intent of this channel is to motivate the community to code everyday, and also to make their coding rounds-during interview, a bit easier !! :)
    Every question has a 1)brute-force approach 2)optimized approach, and the language used is simple English
    I try my best to keep my solutions beginner friendly and simple to understand.
    thanks for your support!! :)
    ***********************
    Leetcode Today Challenge,
    Daily Coding Practise,
    Leetcode 100 days of code,
    Python Daily Coding Challenge,
    Daily LeetCoding Challenge ,
    daily leetcode challenge,
    daily leetcode questions,
    daily leetcode problem,
    leetcode daily today,
    leetcode daily problem today,
    leetcode daily challenge,
    leetcode daily practise,
    leetcode solutions,
    letcode python,
    leetcode hard problems,
    leetcode easy problems,
    leetcode medium problems,
    leetcode for begineers,
    leetcode python problems,
    leetcode python,
    leetcode java,
    leetcode python solutions,
    leetcode python hard,
    leetcode python medium,
    leetcode python easy,
    leetcode python english,
    leetcode python solutions in english
    Where to learn dp
    where to learn graphs
    where to learn dsa
    how to start programming
    how to start coding
    where to learn trees
    what is memoisation
    what is tabulation
    what are graphs
    where to learn Operating systems
    where to learn dbms
    where to learn oops
    where to learn computer networks
    where to learn low level design
    where to learn cs fundamentalS
    Gate smashers
    Placement guide
    How to start programming
    where to learn cpp
    where to learn python
    where to learn javascript
    where to learn java
    Placement guide
    languages to learn
    resources to learn data structures
    Projects development
    Software developer engineer ,
    Importance of side projects ,
    How to become a machine learning engineer ,
    Associate engineer
    Data structures Algorithms
    College Life, College, Memories
    Where to learn dp
    where to learn graphs
    where to learn dsa
    how to start programming
    how to start coding
    where to learn trees
    what is memoization
    what is tabulation
    what are graphs
    where to learn programming
    how to start coding
    where to learn coding
    where to learn DSA
    resources to learn programming
    how to crack amazon
    how to crack placement
    what is blockchain
    blockchain technology explained
    blockchain technology in hindi
    web development roadmap
    android development roadmap
    MERN stack roadmap
    roadmap for 2nd years
    roadmap for opensource
    roadmap for ios development
    roadmap to learn DSA
    microsoft internship,
    microsoft intern 2022,
    microsoft internship India,
    microsoft internship experience
    microsoft,software engineer
    #leetcode #leet_preparation_tips #leetcodesolution #coding #interview
    #CodingInterview #NovemberLeetCodingChallenge #Google #Amazon #programming #march #codinglife #meta #faang #maang

КОМЕНТАРІ • 1

  • @darshankumar5546
    @darshankumar5546  13 днів тому

    from collections import deque
    class Solution:
    def maximumInvitations(self, favorite: List[int]) -> int:
    n=len(favorite)
    visitedNodes=[False for _ in range(n)]
    longestCycleLen=0
    lenInvolvinglen2Cycle=0 #sum of (extended path involving cycle of len 2)
    reversed_edges=defaultdict(list) #will be used for doing BFS of nodes in cycle of len=2
    for i in range(n):
    reversed_edges[favorite[i]].append(i)
    #print(reversed_edges)
    def BFS(node,visited):
    nonlocal visitedNodes,reversed_edges
    #traversal will be done using reversed_edges
    maxDistance=0
    queue=deque()
    queue.append((node,0))
    while(queue):
    curr_node,dist=queue.popleft()
    maxDistance=max(maxDistance,dist)
    for adj_node in reversed_edges[curr_node]:
    if(adj_node in visited):
    continue
    queue.append((adj_node,dist+1))
    return maxDistance

    # print(BFS(11,set([10,11])))
    # print(BFS(10,set([10,11])))
    for i in range(n):
    if(visitedNodes[i]):
    continue
    dist=0
    visitedNodesDistance={}
    current_node=i
    while(visitedNodes[current_node]!=True):
    visitedNodes[current_node]=True
    visitedNodesDistance[current_node]=dist
    dist+=1
    next_node=favorite[current_node]
    if(next_node in visitedNodesDistance):
    #cycle identified
    cycleLen=dist-visitedNodesDistance[next_node]
    longestCycleLen=max(longestCycleLen,cycleLen)
    if(cycleLen==2):
    v=set([next_node,current_node])
    lenInvolvinglen2Cycle+=2+BFS(current_node,v)+BFS(next_node,v)
    current_node=next_node

    return max(longestCycleLen,lenInvolvinglen2Cycle)