Showing posts with label Leetcode. Show all posts
Showing posts with label Leetcode. Show all posts

Thursday, 11 April 2024

links for Data Structure

 


1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭: https://lnkd.in/gXQux4zj

2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬: https://lnkd.in/gKja_D5H

3) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐑𝐞𝐜𝐮𝐫𝐬𝐢𝐨𝐧: https://lnkd.in/gQiasy8H

4) 𝐀 𝐆𝐞𝐧𝐞𝐫𝐚𝐥 𝐚𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐭𝐨 𝐁𝐚𝐜𝐤𝐭𝐫𝐚𝐜𝐤𝐢𝐧𝐠 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬: https://lnkd.in/gVkQX5vA

5) 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐒𝐭𝐫𝐢𝐧𝐠 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 𝐏𝐚𝐭𝐭𝐞𝐫𝐧: https://lnkd.in/gkNvEi8j

6) 10-𝐥𝐢𝐧𝐞 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞 𝐭𝐡𝐚𝐭 𝐜𝐚𝐧 𝐬𝐨𝐥𝐯𝐞 𝐦𝐨𝐬𝐭 '𝐬𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠' 𝐩𝐫𝐨𝐛𝐥𝐞𝐦𝐬: https://lnkd.in/giASrwds

7) 𝐒𝐥𝐢𝐝𝐢𝐧𝐠 𝐖𝐢𝐧𝐝𝐨𝐰 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞: https://lnkd.in/gjatQ5pK

8) 𝐓𝐰𝐨 𝐏𝐨𝐢𝐧𝐭𝐞𝐫𝐬 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gBfWgHYe

9) 𝐏𝐨𝐰𝐞𝐫𝐟𝐮𝐥 𝐔𝐥𝐭𝐢𝐦𝐚𝐭𝐞 𝐁𝐢𝐧𝐚𝐫𝐲 𝐒𝐞𝐚𝐫𝐜𝐡 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞: https://lnkd.in/gKEm_qUK

10) 𝐓𝐞𝐦𝐩𝐥𝐚𝐭𝐞 𝐟𝐨𝐫 𝐌𝐨𝐧𝐨𝐭𝐨𝐧𝐢𝐜 𝐒𝐭𝐚𝐜𝐤 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬: https://lnkd.in/gdYahWVN

11) 𝐆𝐫𝐞𝐞𝐝𝐲 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gw8CgMkC

12) 𝐀𝐥𝐥 𝐓𝐲𝐩𝐞𝐬 𝐨𝐟 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬 𝐟𝐨𝐫 𝐁𝐢𝐭𝐬 𝐌𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧𝐬: https://lnkd.in/gXzegWuU

13) 𝐆𝐫𝐚𝐩𝐡 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gKE6w7Jb

14) 𝐃𝐲𝐧𝐚𝐦𝐢𝐜 𝐏𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐢𝐧𝐠 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬: https://lnkd.in/gbpRU46g

15) 14 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬 𝐭𝐨 𝐀𝐜𝐞 𝐂𝐨𝐝𝐢𝐧𝐠 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬: https://lnkd.in/gMZJVkFf

Repost to help others in your network ♻️

Join 6100+ readers of my free newsletter to master coding and system design using simple explanations and visuals: https://lnkd.in/dXtb8SwU

Friday, 19 August 2022

LeetCode 75 problems

 

Array


Binary


Dynamic Programming


Graph


Interval


Linked List


Matrix


String


Tree


Heap

Sunday, 16 May 2021

Minimum Subarray Of Sumk

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.


Input: target = 7, nums = [2,3,1,2,4,3]
Output:

Explanation: The subarray [4,3] has the minimal length under the problem constraint. 


public class MinimumSubarrayOfSumk {

    public static void main(String[] args) {

        int[] arr = {

                10,5,13,4,8,4,5,11,14,9,16,10,20,8};

        int target  =80;


        int i =-1;

        int j=-1;

        int sum = 0;

        int length = Integer.MAX_VALUE;

        while (true){

        boolean f1 = false,f2 = false;

            while (i<arr.length-1 && sum<target){

                f1 = true;

                i++;

                sum+=arr[i];

                if(sum>=target){

                    length = Math.min(length,i-j);

                    break;

                }

               // f1 = true;

            }


            while (j<i && sum>=target){

                f2 = true;

                j++;

                sum-=arr[j];

                if(sum>=target){

                    length = Math.min(length,i-j);

                }else{

                    break;

                }

               // f2 = true;


            }


            if(!f1 && !f2){

                break;

            }


        }


        System.out.println(length);



    }

}


Result: 6

Permutation in String

 Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words,

one of the first string’s permutations is the substring of the second string.

For example:

Input: s1 = "ab" s2 = "eidbaooo"

Output: True

Explanation: s2 contains one permutation of s1 ("ba").


public class PermutationInString {

    public static void main(String[] args) {

        boolean bool = checkInclusion();

        System.out.println(bool);

    }


    private static boolean checkInclusion(){

        String str1 = "eidbaooo";

        String str2 = "ab";


        Map<Character, Integer> map2 = new HashMap<>();

        for(int i = 0;i<str2.length();i++){

            char ch = str2.charAt(i);

            map2.put(ch,map2.getOrDefault(ch,0)+1);

        }


        Map<Character, Integer> map1 = new HashMap<>();

        int i = -1;

        int j=-1;

        int desireCount = str2.length();

        int mCount = 0;

        boolean resultFlag= false;

        while (true){

            boolean f1 = false,f2 = false;


            while (i<str1.length()-1 && mCount<desireCount){

                f1= true;

                i++;

                char ch = str1.charAt(i);


                map1.put(ch,map1.getOrDefault(ch,0)+1);


                if(map1.getOrDefault(ch,0)<=map2.getOrDefault(ch,0)){

                    mCount++;

                }


                if(mCount==desireCount){

                    break;

                }


            }


            while (j<i && mCount==desireCount){

                f2 = true;

                j++;

                if(i+1-j==desireCount){ //check if size of string is same as desired count

                    //System.out.println("result");

                    resultFlag = true;

                    break;

                }

                char ch = str1.charAt(j);

                Integer val =map1.get(ch);

                if(val>1){

                    map1.put(ch,map1.get(ch)-1);

                }else{

                    map1.remove(ch);

                }


                if(map1.getOrDefault(ch,0)<map2.getOrDefault(ch,0)){

                    mCount--;

                }


                if(mCount<desireCount){

                    break;

                }


            }


            if(resultFlag){

                System.out.println("result found");

                break;

            }

            if(!f1 && !f2){

                System.out.println("result not found");


                break;

            }



        }


        if(resultFlag){

            //System.out.println("result found");

            return true;

        }else{

            //System.out.println("not found");

            return false;

        }


     //   return true;


    }

}


Result:true

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...