Posts

Showing posts from 2024

Types of Question related to 2 Nodes of a Graph

  Existence of a Path Is there a path from node A A A to node B B B ?  1971. Find if Path Exists in Graph Does a path satisfying certain constraints (e.g., through specific nodes or avoiding certain nodes/edges) exist?  Distance Between Two Nodes Shortest distance : Using algorithms like Dijkstra, Bellman-Ford, or BFS (for unweighted graphs). 787. Cheapest Flights Within K Stops Exact distance or range : Finding if a specific distance exists. Number of Paths Between Two Nodes Simple paths : Count paths where no node is revisited. All paths : Count paths (with possible revisits). K-length paths : Paths of exactly or at most k k k edges Shortest Path Between Two Nodes Weight-based shortest path : Using weights on edges. Fewest edges : Finding paths with the smallest number of edges. Maximum Flow or Minimum Cut Between Two Nodes Maximum flow problems deal with finding the maximum "amount" of a resource that can flow between two nodes in a capacitated graph. Connectivity Qu...

Observer Design Pattern

Observer Design is one of the most important Design Pattern. It has many practical usage like notification services. The main two entities are Publisher(class) and Observer (interface) . Publisher is a class with functions like addObserver and notifyyObservers. Here class WeatherStation is extending Publisher class so that all the functions of Publisher can come to it. Here two Observers Mobile and Monitor is implementing interface  Observer. Inside Mobile and Monitor class, they are subscribing to WeatherData inside the constructors. import java.util.*; interface  Observer {  void  notifyy(); } class Mobile implements Observer{     WeatherStation ws;     public Mobile(WeatherStation ws) {         this.ws = ws;         ws.addObserver(this);     }          @Override     public void notifyy() {         System.out.println("Mobile is noti...

Divide Intervals Into Minimum Number of Groups

  Divide Intervals Into Minimum Number of Groups  Leetcode Problem  Divide Intervals Into Minimum Number of Groups   is very much similar to the number-of-the-smallest-unoccupied-chair  Here we have to divide the given intervals into minimum number of groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other. Solution: We will think each interval as combination of two events ,  startTime  and endTime. 1) We create a list of events and then sort that list.  2) After that, we Initialise a min heap. which can be thinked like different baskets or groups.  3) We iterate through the events one by one, and assign each events to groups (picked from minheap).      While iterating starting event we pick a group from min heap and keep track in an array.       While iterating ending event through our tracking array, we release the group again in min...

Insert an interval between an array of intervals

In this problem statement we have to insert a given interval between a given array of intervals.  Insert Interval Leetcode Approach:- Here we Insert the given interval in the array and then we use the same approach as we used in merge interval Leetcode question   class Pair { int x ; int y ; Pair ( int x , int y ) { this . x = x; this . y = y; } } class Solution { public int [][] insert ( int [][] intervals , int [] newInterval ) { ArrayList < Pair > p = new ArrayList <>(); for ( int i = 0 ;i< intervals . length ;i++) { p . add ( new Pair (intervals[i][ 0 ], intervals[i][ 1 ])); } p . add ( new Pair (newInterval[ 0 ], newInterval[ 1 ])); Collections . sort (p, ( Pair a, Pair b) -> { return a . x - b . x ; }); Stack < Pair > st = new Stack <>(); st . push ( p . get ( 0 )); for ( int i = ...