Take K of Each Character From Left and Right | 2 Approaches | Leetcode 2516 | codestorywithMIK

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2024

ความคิดเห็น • 55

  • @rohanraj2604
    @rohanraj2604 4 วันที่ผ่านมา +14

    Jb first time probelm dekha to i was like ye kya ques h 😢 Don't know but jese bhaii ki awaz suno ek motivation sa feel ata h 🎉❤ #Thankyou #MIK_Bhai 😅

  • @gui-codes
    @gui-codes 4 วันที่ผ่านมา +5

    The motivation part is so good. I love it. Thanks man for adding it in your videos.
    And I got the idea in the middle of the video (I skipped brute force) and coded on my own. THANK YOU SO MUCH

  • @manavrajthakor8175
    @manavrajthakor8175 4 วันที่ผ่านมา +1

    Hi MIK , thanks for making this video. This was my approach using a sliding window with circular array concept and passed 142 test cases 10 were remaining but couldn't find the issue.class Solution {
    public:
    int takeCharacters(string s, int k) {
    int n = s.length();

    // Step 1: Count the total occurrences of 'a', 'b', and 'c' in the string
    unordered_map total_count;
    for (char c : s) {
    total_count[c]++;
    }

    // If we don't have enough of any character, return -1
    if (total_count['a'] < k || total_count['b'] < k || total_count['c'] < k) {
    return -1;
    }
    // Step 2: Sliding window with circular logic
    unordered_map window_count;
    int left = 0, right = 0;
    int result = n + 1; // Initialize result as a large number
    // Step 3: Try taking characters from the right end
    while (right < 2 * n) { // We loop until we have gone through two full passes (wrap around)
    window_count[s[right % n]]++; // Circular access using modulo
    right++;
    // Step 4: Once we have enough 'a', 'b', and 'c', start shrinking from the left
    while (window_count['a'] >= k && window_count['b'] >= k && window_count['c'] >= k) {
    result = min(result, right - left); // Calculate the window size
    // Try to shrink the window from the left
    window_count[s[left % n]]--; // Circular access using modulo
    if (window_count[s[left % n]] == 0) {
    window_count.erase(s[left % n]);
    }
    left++;
    }
    }
    // After processing the whole string, check the result
    return result == n + 1 ? -1 : result;
    }
    };

  • @jain5184
    @jain5184 4 วันที่ผ่านมา +11

    Just search kar rh tha aaj ka potd ka and here we go
    Hatsoff mik bhaiya for motivation at the strt the intuition for the problem thnku so much 🫡

  • @amanpaliwalvlogs6860
    @amanpaliwalvlogs6860 4 วันที่ผ่านมา +2

    Radhe Radhe ❤

  • @kishan.17
    @kishan.17 4 วันที่ผ่านมา +29

    I have a interview today mik bro wish me luck and pls give some tips for interview ❤

    • @gui-codes
      @gui-codes 4 วันที่ผ่านมา +3

      All the best bro. Which company ?
      Confident raho bas.

    • @meetdobariya8777
      @meetdobariya8777 4 วันที่ผ่านมา +1

      All the best bhai

    • @amolshinde8607
      @amolshinde8607 4 วันที่ผ่านมา +1

      All the very best buddy!

    • @kishan.17
      @kishan.17 4 วันที่ผ่านมา +1

      Thanks bro ❤

    • @kishan.17
      @kishan.17 4 วันที่ผ่านมา +3

      ​@@gui-codes deloite

  • @Satya-g5t
    @Satya-g5t 3 วันที่ผ่านมา +1

    like your quotation, i mean motivation. also solution as well.

  • @harjotanand834
    @harjotanand834 4 วันที่ผ่านมา +3

    Mujhe yeh brute force hi laga tha aur laga ki shayad dp hai 😅😅

  • @Aditya-qx7nf
    @Aditya-qx7nf 4 วันที่ผ่านมา

    Thanks for the approach, it was easy to code after understanding the approach

  • @YashSinghal
    @YashSinghal 3 วันที่ผ่านมา +1

    very good question

  • @rahulharsh545
    @rahulharsh545 3 วันที่ผ่านมา +2

    just brilliant

  • @thekindspill
    @thekindspill 4 วันที่ผ่านมา

    Fantastic explanation and the solution

  • @hellsterkun8764
    @hellsterkun8764 3 วันที่ผ่านมา +1

    #optimization #codetostorywithmik
    while(j

  • @SurajGupta-gc9tz
    @SurajGupta-gc9tz 4 วันที่ผ่านมา +1

    Thank you bhaiya

  • @faizanalam8823
    @faizanalam8823 3 วันที่ผ่านมา +1

    I have used Binary search + sliding window

  • @fr4nkyyy720
    @fr4nkyyy720 4 วันที่ผ่านมา

    great explaination

  • @aws_handles
    @aws_handles 4 วันที่ผ่านมา

    You are too good MIK

  • @moveahead7367
    @moveahead7367 4 วันที่ผ่านมา +2

    119/142 test case passed by own

  • @harshmishra7890
    @harshmishra7890 4 วันที่ผ่านมา +6

    Hi MiK can you please make a video explaining the leetcode problem 2539. It's quite confusing or can be said to be a big problem. Was recently asked by Swiggy.
    PS: Not a single video on TH-cam of it 🥲

    • @dhruvchopra26
      @dhruvchopra26 4 วันที่ผ่านมา

      Bhai ye to leetcode premium problem hai not accessible by all

    • @harshmishra7890
      @harshmishra7890 4 วันที่ผ่านมา

      @dhruvchopra26 I know but it was asked in Swiggy coding round that's why I asked

  • @DrOnZeR2022
    @DrOnZeR2022 4 วันที่ผ่านมา

    Bhaiya tcs codevita ke previous year question pe vedios bna do plz❤ khi se ache resources nhi mil rhe

  • @sumitpatil4732
    @sumitpatil4732 3 วันที่ผ่านมา

    Instead why can't we apply dp (index,counta,countb ,count c, minutes) and return minutes 5d dp but less than 10 ^ 8 so will run

  • @Andrew-tk1su
    @Andrew-tk1su 4 วันที่ผ่านมา +1

    Why dp cannot be used for this question. Why not converting recursion to memoization

    • @amanpatel8575
      @amanpatel8575 3 วันที่ผ่านมา

      Constraints are 1e5. Will Give TLE.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 4 วันที่ผ่านมา +4

    First 🎉❤

    • @gui-codes
      @gui-codes 4 วันที่ผ่านมา

      second 😁

  • @chandan1929
    @chandan1929 4 วันที่ผ่านมา

    Hi, did someone do the code which told to do it in the beginning (with recusion), please post it.

  • @vishwashsoni610
    @vishwashsoni610 4 วันที่ผ่านมา +1

    class Solution {
    public:
    void solve(string& s,int i,int j,int& count,vector& store,int k,int n){
    if(store[0]>=k && store[1]>=k && store[2]>=k){
    count = min(count,store[0]+store[1]+store[2]);
    return;
    }
    if(i=0){
    store[s[j]-'a']++;
    solve(s,i,j-1,count,store,k,n);
    store[s[j]-'a']--;
    }
    else{
    count=-1;
    return;
    }
    }
    int takeCharacters(string s, int k) {
    int n = s.size();
    int i=0;
    int j = n-1;
    int count = INT_MAX;
    vectorstore(3,0);
    solve(s,i,j,count,store,k,n);
    return count;
    }
    };
    sir ye to recursion wala sol. to tle chal gaya 😓ye code sahi hai ya nahi ?

  • @amolshinde8607
    @amolshinde8607 4 วันที่ผ่านมา

    i could write this much only lol -> class Solution {
    public:
    int takeCharacters(string s, int k) {
    int n = s.length();
    unordered_map charCount;
    for (char &ch : s) {
    charCount[ch]++;
    }
    for (int i = 0; i < charCount.size(); i++) {
    if (i.second < k) {
    return -1;
    }
    }
    }
    };

  • @crazygamerrohan9899
    @crazygamerrohan9899 4 วันที่ผ่านมา +3

    #Recursion
    class Solution {
    public:
    void solve(string s,int k,int i,int j,int &minstep,int a,int b,int c){
    if(a>=k && b>=k && c>=k){
    int val=(s.size()-j)+i-1;
    minstep=min(minstep,val);
    return ;
    }
    if(i>j){
    return ;
    }
    if(s[i]=='a'){
    a++;
    solve(s,k,i+1,j,minstep,a,b,c);
    a--;
    }
    if(s[i]=='b'){
    b++;
    solve(s,k,i+1,j,minstep,a,b,c);
    b--;
    }
    if(s[i]=='c'){
    c++;
    solve(s,k,i+1,j,minstep,a,b,c);
    c--;
    }
    if(s[j]=='a'){
    a++;
    solve(s,k,i,j-1,minstep,a,b,c);
    a--;
    }
    if(s[j]=='b'){
    b++;
    solve(s,k,i,j-1,minstep,a,b,c);
    b--;
    }
    if(s[j]=='c'){
    c++;
    solve(s,k,i,j-1,minstep,a,b,c);
    c--;
    }

    }
    int takeCharacters(string s, int k) {
    int i=0,j=s.size()-1;
    int minstep=INT_MAX;
    solve(s,k,i,j,minstep,0,0,0);
    if(minstep==INT_MAX){
    return -1;
    }
    return minstep;
    }
    };
    Recursion Code Getting MLE But AMAZING TO CODE🤎

    • @faizanmohammed7687
      @faizanmohammed7687 4 วันที่ผ่านมา

      Have you uploaded its explanation in Leet code discussion ? if yes then can you say your username on leetcode?

    • @crazygamerrohan9899
      @crazygamerrohan9899 4 วันที่ผ่านมา

      ____loki
      you can refer .

    • @laxmannarkhede8116
      @laxmannarkhede8116 4 วันที่ผ่านมา

      how u r able to find solution bro.. i am not able to solve it ny self.. i have to look solution for each problem..

  • @ramlakhangupta856
    @ramlakhangupta856 3 วันที่ผ่านมา

    bhaiya aap ye konsa app use karte ho ipad pe hame samzane ke liye pls bta do @codestorywithMIK

    • @codestorywithMIK
      @codestorywithMIK  3 วันที่ผ่านมา

      It’s the default NOTES app in ipad

  • @Madaraa777
    @Madaraa777 4 วันที่ผ่านมา +3

    I hate this problem..

  • @aayushguptamnit6307
    @aayushguptamnit6307 4 วันที่ผ่านมา

    Knuth algo ? Just reminding

  • @pradeepranjan8226
    @pradeepranjan8226 4 วันที่ผ่านมา +1

    I paused the video and tried with rec :
    class Solution {
    public int takeCharacters(String s, int k) {
    int[] freq = new int[3];
    for(char ch: s.toCharArray()){
    freq[ch - 'a']++;
    }
    for(int n : freq){
    if(n < k) return -1;
    }
    return sol(0, s.length() - 1, s, new int[3],k );
    }
    private int sol(int start, int end, String s, int[] freq, int k ){
    if(freq[0] >= k && freq[1] >= k && freq[2] >= k){
    return 0; //no more moves needed
    }
    if(start > end){
    //invalid path to cover as other way we have covered already
    //return invalid data and as we are comparing min, return infi
    return Integer.MAX_VALUE/2;
    }
    freq[s.charAt(start) - 'a']++;
    int left = 1 + sol(start + 1, end,s, freq, k);
    freq[s.charAt(start) - 'a']--;
    freq[s.charAt(end) - 'a']++;
    int right = 1 + sol(start, end - 1,s, freq, k);
    freq[s.charAt(end) - 'a']--;
    return Math.min(left, right);
    }
    }
    This is giving TLE, even if i add cache here(memo) , i can not excute this in O(n) times
    so i need to try sliding window.

  • @manavmody5236
    @manavmody5236 4 วันที่ผ่านมา +1

    "you may take either the leftmost character of s, or the rightmost character of s."
    although your solution is correct, this is being violated i feel.
    Why cant we keep one pointer at 0th index and other at s.size()-1

    • @codestorywithMIK
      @codestorywithMIK  4 วันที่ผ่านมา +4

      Note that sliding window is finding the window of not_deleted characters. Hence starting from i = 0 and j = 0 and then moving forward

    • @manavmody5236
      @manavmody5236 3 วันที่ผ่านมา

      @@codestorywithMIK okay, understood...thankssss

  • @ritikgupta1196
    @ritikgupta1196 4 วันที่ผ่านมา +1

    class Solution {
    public:
    int takeCharacters(string s, int k) {
    int n=s.length();
    vectorfreq(3,0);
    for(auto ch:s){
    freq[ch-'a']++;
    }
    if(freq[0]

    • @codestorywithMIK
      @codestorywithMIK  4 วันที่ผ่านมา +1

      Yep yours is a cleaner one 👌❤️
      This is another good way.
      Thank you for sharing with all of us

    • @ritikgupta1196
      @ritikgupta1196 4 วันที่ผ่านมา

      @@codestorywithMIK Thank you for the appreciation❤