Posts

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 heap.      During this process, we track what is the maximum group number poped fr

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 = 1 ;i<

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-go/events" "github.com/aws/aws-lambda-go/lambda" ) func main () { lambda. Start (handler) } func handler (request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error ) { var person Person //json request body se nikaal ke person var me rakh denge err := json. Unmarshal ([] byte (request.Body), &person) if err != nil { return events.APIGatewayProxyResponse{}, err } //ab person se firstname aur lastname nikaal ke msg bana lenge msg := fmt. Sprintf ( "Hello %v %v " , *person.FirstName, *person.LastName) responseBody := ResponseBody{ Message: &msg, } jbytes , err := json. Marshal (responseBody) if err != nil { return events.APIGatewayProxyResponse{}, err } log. Println ( "hello world Nihal" )

Subset Sum with Recursion | memorization | DP

Given a set of non-negative integers, and a value sum , determine if there is a subset of the given set with sum equal to given sum . https://www.geeksforgeeks.org/subset-sum-problem-dp-25/             Recursion                                            class Solution {          static boolean isSumK(int[] nums, int curin, int curSum) {                  if(curSum==0) {             return true;         }                  if(curin<0) {             return false;         }                  return isSumK(nums, curin-1, curSum-nums[curin])              || isSumK(nums, curin-1, curSum);     } With Memorization class Solution{     static int issum(int arr[], int indx, int sum, int dp[][]) {                  if(sum==0)          return 1;                   if(indx<0)          return 0;                  if(dp[indx][sum]!=-1)          return dp[indx][sum];                   int pick = 0;         if(sum>=arr[indx])            pick = issum(arr, indx-1, sum-arr[indx],dp);          

Subset sum K [Recursion]

 https://www.geeksforgeeks.org/subset-sum-problem-dp-25/ class Solution {          static boolean isSumK(int[] nums, int curin, int curSum) {                  if(curSum==0) {             return true;         }                  if(curin<0) {             return false;         }                  return isSumK(nums, curin-1, curSum-nums[curin])              || isSumK(nums, curin-1, curSum);     }     static Boolean isSubsetSum(int N, int arr[], int sum) {         // code here         return isSumK(arr, N-1, sum);     } }

Java Practice [HashMap]

Group Anagrams https://leetcode.com/problems/group-anagrams/  class Solution {     public List<List<String>> groupAnagrams(String[] strs) {                  HashMap<String, List<String>> hm = new HashMap<>();                  for(int i=0;i<strs.length;i++) {             char[] s = strs[i].toCharArray();             Arrays.sort(s);             String k = new String(s);             if(hm.containsKey(k)) {                 hm.get(k).add(strs[i]);             } else {                 List<String> ls = new ArrayList<String>();                 ls.add(strs[i]);                 hm.put(k,ls);             }         }                  List<List<String>> ans = new ArrayList<List<String>>();;                  for (String p:hm.keySet()) {           ans.add(hm.get(p));            }                  return ans;     } }

Linked List in Java without Collections

Linked List in Java without Collections class Node { int data ; Node next ; Node ( int data) { this . data = data ; this . next = null; } } public class Main { public static void main (String[] args) { Node n1 = new Node( 5 ) ; Node n2 = new Node( 6 ) ; Node n3 = new Node( 7 ) ; Node n4 = new Node( 8 ) ; n1. next = n2 ; n2. next = n3 ; n3. next = n4 ; Node start = n1 ; while (start!= null ) { System. out .println(start. data + " " ) ; start = start. next ; } } }