RainX_69

LEETCODE- Take K of Each Character From Left and Right

Dec 26th, 2022 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | Source Code | 0 0
  1. /*
  2. You are given a string s consisting of the characters 'a', 'b', and 'c' and a non-negative integer k. Each minute, you may take either the leftmost character of s, or the rightmost character of s.
  3.  
  4. Return the minimum number of minutes needed for you to take at least k of each character, or return -1 if it is not possible to take k of each character.
  5.  
  6.  
  7.  
  8. Example 1:
  9.  
  10. Input: s = "aabaaaacaabc", k = 2
  11. Output: 8
  12. Explanation:
  13. Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
  14. Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
  15. A total of 3 + 5 = 8 minutes is needed.
  16. It can be proven that 8 is the minimum number of minutes needed.
  17. Example 2:
  18.  
  19. Input: s = "a", k = 1
  20. Output: -1
  21. Explanation: It is not possible to take one 'b' or 'c' so return -1.
  22.  
  23.  
  24. Constraints:
  25.  
  26. 1 <= s.length <= 10^5
  27. s consists of only the letters 'a', 'b', and 'c'.
  28. 0 <= k <= s.length
  29.  
  30. LINK- https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/
  31.  
  32. */
  33.  
  34.  
  35. class Solution {
  36. public:
  37.     int binarySearch(vector<int> &arr, int low, int high, int req){
  38.         int res=-1;
  39.         while(low<=high){
  40.             int mid=(low+high)/2;
  41.             if(arr[mid]>=req){
  42.                 res=mid;
  43.                 low=mid+1;
  44.             }
  45.             else{
  46.                 high=mid-1;
  47.             }
  48.         }
  49.         return res;
  50.     }
  51.    
  52.     int takeCharacters(string s, int k) {
  53.         if(k==0){
  54.             return 0;
  55.         }
  56.         int n=s.size();
  57.         vector<int> A(n,0);
  58.         vector<int> B(n,0);
  59.         vector<int> C(n,0);
  60.         for(int i=n-1;i>=0;i--){
  61.             if(i<n-1){
  62.                 A[i]+=A[i+1];                
  63.                 B[i]+=B[i+1];
  64.                 C[i]+=C[i+1];
  65.             }
  66.             s[i]=='a' ? A[i]++ : (s[i]=='b' ? B[i]++ : C[i]++);
  67.         }      
  68.         if(A[0]<k || B[0]<k || C[0]<k){
  69.             return -1;
  70.         }
  71.         int a=0;
  72.         int b=0;
  73.         int c=0;
  74.         int res=INT_MAX;
  75.         for(int i=0;i<n;i++){
  76.             s[i]=='a' ? a++ : (s[i]=='b' ? b++ : c++);
  77.            
  78.             if(a>=k && b>=k && c>=k){  // taking only from left side
  79.                 res=min(res,i+1);
  80.                 break;  // if your ith index contains all k a,b,c then it is time to break cuz you will not get any smaller than this moving ahead.
  81.             }
  82.             if(A[n-i-1]>=k && B[n-i-1]>=k && C[n-i-1]>=k){  // taking only from right side
  83.                 res=min(res,i+1);
  84.                 break;
  85.             }
  86.            
  87.             int requiredA=k-a;
  88.             int requiredB=k-b;
  89.             int requiredC=k-c;
  90.            
  91.             int indexA=binarySearch(A,i+1,n-1,requiredA);
  92.             int indexB=binarySearch(B,i+1,n-1,requiredB);
  93.             int indexC=binarySearch(C,i+1,n-1,requiredC);
  94.            
  95.             int index=min({indexA,indexB,indexC});
  96.             if(index==-1){  // not possible
  97.                 continue;
  98.             }
  99.            
  100.             res=min(res,(i+1)+(n-index));
  101.         }
  102.  
  103.         return res==INT_MAX ? -1 : res;
  104.     }
  105. };
  106.  
  107.  
  108. /*
  109. THOUGHT PROCESS-
  110.  
  111. If I have count of a,b,c till a certain point index, how can I find the rest from i+1 till n-1 in less time.
  112.  
  113. NOTE- THERE IS A SLIDING WINDOW PROBLEM SOLVING IN O(N) TIME
  114. */
Advertisement
Add Comment
Please, Sign In to add comment