Coding Interview Practice With Anurag
Coding Interview Practice With Anurag
  • 49
  • 1 696
Minimum Window Substring | Leetcode Top Interview 150
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies.
About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer.
Problem link - leetcode.com/problems/minimum-window-substring/description/?envType=study-plan-v2&envId=top-interview-150
Problem Description - Given two strings s and t of lengths m and n respectively, return the minimum window
substring
of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".
The testcases will be generated such that the answer is unique.
Переглядів: 17

Відео

Substring With Concatenation Of All Words | Leetcode Top Interview 150
Переглядів 15День тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/substring-with-c...
Longest Substring Without Repeating Characters | Leetcode Top Interview 150
Переглядів 5414 днів тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem link - leetcode.com/problems/longest-substrin...
Minimum Size Subarray Sum | Leetcode Top Interview 150
Переглядів 3414 днів тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/minimum-size-sub...
3Sum | Leetcode Top Interview 150
Переглядів 1614 днів тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem link - leetcode.com/problems/3sum/description...
Container With Most Water | Top Interview 150
Переглядів 2721 день тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem link - leetcode.com/problems/container-with-m...
Two Sum II | Leetcode Top Interview 150
Переглядів 1721 день тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/two-sum-ii-input...
Is Subsequence | Leetcode Top Interview 150
Переглядів 1621 день тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/is-subsequence/d...
Valid Palindrome | Leetcode Top Interview 150
Переглядів 1521 день тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/valid-palindrome...
Text Justification | Leetcode Top Interview 150
Переглядів 728 днів тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/text-justificati...
Find The Index Of The First Occurence In A String | Leetcode Top Interview 150
Переглядів 328 днів тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/find-the-index-o...
Zigzag Conversion | Leetcode Top Interview 150
Переглядів 8Місяць тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/zigzag-conversio...
Reverse Words In A String | Leetcode Top Interview 150
Переглядів 5Місяць тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem link - leetcode.com/problems/reverse-words-in...
Longest Common Prefix | Leetcode Top Interview 150
Переглядів 7Місяць тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/longest-common-p...
Length of Last Word | Leetcode Top Interview 150
Переглядів 3Місяць тому
Here we will discuss the most important data structures and algorithms questions which are usually asked in the top rated product based companies. About me - My name is Anurag Gupta. I have done my B.Tech. from IIT Roorkee. I have done software engineer internship at Amazon and I have 3 years of work experience as a Senior Software Engineer. Problem Link - leetcode.com/problems/length-of-last-w...
Integer To Roman | Leetcode Top Interview 150
Переглядів 7Місяць тому
Integer To Roman | Leetcode Top Interview 150
Roman To Integer | Leetcode Top Interview 150
Переглядів 5Місяць тому
Roman To Integer | Leetcode Top Interview 150
Trapping Rain Water | Leetcode Top Interview 150
Переглядів 3Місяць тому
Trapping Rain Water | Leetcode Top Interview 150
Candy | Leetcode Top Interview 150
Місяць тому
Candy | Leetcode Top Interview 150
Gas Station | Leetcode Top Interview 150
Переглядів 6Місяць тому
Gas Station | Leetcode Top Interview 150
Product of Array Except Self | Leetcode Top interview 150
Переглядів 26Місяць тому
Product of Array Except Self | Leetcode Top interview 150
Insert Delete GetRandom O(1) | Leetcode Top Interview 150
Переглядів 38Місяць тому
Insert Delete GetRandom O(1) | Leetcode Top Interview 150
H-Index | Leetcode Top Interview 150
Переглядів 162 місяці тому
H-Index | Leetcode Top Interview 150
Jump Game II | Leetcode Top Interview 150
Переглядів 182 місяці тому
Jump Game II | Leetcode Top Interview 150
Jump Game | Leetcode Top Interview 150
Переглядів 82 місяці тому
Jump Game | Leetcode Top Interview 150
Best Time to Buy and Sell Stock II | Leetcode Top Interview 150
Переглядів 82 місяці тому
Best Time to Buy and Sell Stock II | Leetcode Top Interview 150
Best Time to Buy and Sell Stock | Leetcode Top Interview 150
Переглядів 132 місяці тому
Best Time to Buy and Sell Stock | Leetcode Top Interview 150
Rotate Array | Leetcode Top Interview 150
Переглядів 52 місяці тому
Rotate Array | Leetcode Top Interview 150
Majority Element | Leetcode Top Interview 150
Переглядів 52 місяці тому
Majority Element | Leetcode Top Interview 150
Remove Duplicates from Sorted Array II | Leetcode Top Interview 150
Переглядів 193 місяці тому
Remove Duplicates from Sorted Array II | Leetcode Top Interview 150

КОМЕНТАРІ

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 10 днів тому

    Solution code - class Solution { public: string minWindow(string s, string t) { int ns = s.length(); int nt = t.length(); map<char,int> initialMap,currMap; for (int i=0;i<nt;i++) { initialMap[t[i]]++; } int minLen = 1e8,ansl=-1,ansr=-1,l=0,r=0; int req = initialMap.size(); int curr = 0; while(r<ns) { if (initialMap.find(s[r]) != initialMap.end()) { currMap[s[r]]++; if (currMap[s[r]] == initialMap[s[r]]) { curr++; } if (curr == req) { while(curr == req && l<=r) { if ((r-l+1)<minLen) { minLen = r-l+1; ansl = l; ansr = r; } if (initialMap.find(s[l]) != initialMap.end()) { currMap[s[l]]--; if (currMap[s[l]]<initialMap[s[l]]) { curr--; } } l++; } } } r++; } if (minLen == 1e8) return ""; else return s.substr(ansl,minLen); } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 12 днів тому

    Solution code - class Solution { public: vector<int> findSubstring(string s, vector<string>& words) { int n = words.size(); int len = s.length(); int wordLen = words[0].length(); int l=0,r=0; int tempN = n; int reqLen = n*wordLen; map<string,int> initialMap,currMap; string temp; vector<int> ans; //Handling edge case bool allWordsSame = true; for (int i=1;i<n;i++) { if (words[i] != words[i-1]) { allWordsSame = false; break; } } bool allCharsSame = true; for (int i=1;i<len;i++) { if (s[i] != s[i-1]) { allCharsSame = false; break; } } if (allCharsSame && allWordsSame && wordLen == 1) { for (int i=0;i<=len-(reqLen);i++) { ans.push_back(i); } return ans; } for (int i=0;i<n;i++) { initialMap[words[i]]++; } int flag = 0; currMap = initialMap; for (int i=0;i<=(len-wordLen);i++) { int j=0; flag = 0; // keeps track of whether currMap has changed while(j<reqLen) { temp = s.substr((i+j),wordLen); if (currMap.find(temp) == currMap.end()) { break; } else { currMap[temp]--; flag = 1; if (currMap[temp] == 0) { currMap.erase(temp); } } j += wordLen; } if (currMap.empty()) { ans.push_back(i); } if (flag) currMap = initialMap; } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 14 днів тому

    Solution code - class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.length(); int l=0,r=0,ans=0; set<char> setOfChars; for (;r<n;r++) { if (setOfChars.find(s[r]) == setOfChars.end()) { setOfChars.insert(s[r]); ans = max(ans,(r-l+1)); } else { while(s[l] != s[r]) { setOfChars.erase(s[l]); l++; } l++; ans = max(ans,(r-l+1)); } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 18 днів тому

    Solution code - class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int n = nums.size(); int l=0,r=0,sum=nums[0]; int ans = 1e9; while (r<n) { if (sum>=target) { ans = min(ans, (r-l+1)); sum -= nums[l]; l++; } else { r++; if (r<n) sum += nums[r]; } } if (ans == 1e9) return 0; else return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 19 днів тому

    Solution code - class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { int n = nums.size(); sort(nums.begin(),nums.end()); vector<vector<int>> ans; vector<int> temp; int l,r, target; for (int k=0;k<n;k++) { while(k<n && k>0 && nums[k] == nums[k-1]) k++; if (k==n) break; temp.push_back(nums[k]); l= k+1; r=n-1; target = 0 - nums[k]; while(l<r) { if ((nums[l] + nums[r]) == target) { temp.push_back(nums[l]); temp.push_back(nums[r]); ans.push_back(temp); temp.pop_back(); temp.pop_back(); l++; while(l<n && nums[l] == nums[l-1]) l++; } else if ((nums[l] + nums[r])>target) { r--; } else { l++; } } temp.pop_back(); } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 21 день тому

    Solution code - class Solution { public: int maxArea(vector<int>& height) { int n = height.size(); int left =0, right = n-1; int ans = 0; while (left < right) { ans = max(ans, (right - left)*(min(height[left], height[right]))); if (height[left] < height[right]) left++; else right--; } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 25 днів тому

    Solution code - class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { int n = numbers.size(); int l =0, r=n-1; vector<int> ans; while(l<r) { if ((numbers[l] + numbers[r])== target) { ans.push_back(l+1); ans.push_back(r+1); return ans; } else if ((numbers[l] + numbers[r])>target) { r--; } else { l++; } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 26 днів тому

    Solution code - class Solution { public: bool isSubsequence(string s, string t) { int ns = s.length(); int nt = t.length(); int i=0; for (int j=0;j<nt;j++) { if (s[i] == t[j]) i++; if (i==ns) break; } return (i==ns); } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 27 днів тому

    Solution code - class Solution { public: bool isPalindrome(string s) { int n = s.length(); string newStr = ""; for (int i=0;i<n;i++) { int intVal = (int)s[i]; if (intVal>=65 && intVal <= 90) { intVal += 32; // converting uppercase into lowercase newStr += (char)intVal; } else if (intVal >=97 && intVal <= 122) { newStr += s[i]; } else if (intVal >=48 && intVal <= 57) { newStr += s[i]; } } bool ans = true; for (int i=0;i<newStr.length()/2;i++) { if (newStr[i] != newStr[newStr.length()-1-i]) { ans = false; break; } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 28 днів тому

    Solution code - class Solution { public: vector<string> fullJustify(vector<string>& words, int maxWidth) { int n = words.size(); int start = 0,end=0,curr=0,coveredChars=0; int spaces=0,numOfWords=0; int quo,rem,j; string temp; vector<string> ans; while(start < n) { curr = 0;coveredChars=0; for (end=start;end<n;end++) { if (end == start) { curr += words[end].length(); } else { curr += words[end].length(); curr += 1; // This 1 refers to space } if (curr > maxWidth) { break; } else { coveredChars += words[end].length(); } } spaces = maxWidth - coveredChars; numOfWords=(end-start); temp=""; if (end == n) { for (int i=start;i<end;i++) { if (i!=(end-1)) { temp += words[i]; temp += " "; spaces--; } else { temp += words[i]; } } j=spaces; while(j-- > 0) { temp += " "; } } else { if (numOfWords != 1) { quo = (spaces)/(numOfWords-1); rem = (spaces)%(numOfWords-1); for (int i=start;i<end;i++) { if (i!=(end-1)) { temp += words[i]; if (rem>0) { j = quo+1; rem--; while(j-- > 0) temp += " "; } else { j = quo; while(j-- > 0) temp += " "; } } else { temp += words[i]; } } } else { temp += words[start]; j = spaces; while(j-- > 0) { temp += " "; } } } ans.push_back(temp); start = end; } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 29 днів тому

    Solution code - class Solution { public: int strStr(string haystack, string needle) { int needleLen = needle.length(); int haystackLen = haystack.length(); for (int i=0;i<=(haystackLen - needleLen);i++) { if (haystack[i] == needle[0]) { if (haystack.substr(i,needleLen) == needle) { return i; } } } return -1; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: string convert(string s, int numRows) { numRows--; int n = s.length(); int j,flag; string ans; if (numRows == 0) { return s; } for (int i=numRows;i>=0;i--) { if (i == numRows) { j = 0; while(j<n) { ans += s[j]; j += (2*numRows); } } else if (i == 0) { j = numRows; while(j<n) { ans += s[j]; j += (2*numRows); } } else { j = (numRows - i); flag = 1; while(j<n) { ans += s[j]; if (flag) { j += (2*i); } else { j += 2*(numRows-i); } flag = 1-flag; } } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int escapeSpaces(string s, int i) { while(i>=0 && s[i] == ' ') i--; return i; } string reverseWords(string s) { int n = s.length(); string ans,temp; int i = n-1; while(i>=0) { i = escapeSpaces(s,i); if (i<0) break; temp = ""; while(i>=0 && s[i] != ' ') { temp += s[i]; i--; } reverse(temp.begin(),temp.end()); ans += temp; ans += " "; } return ans.substr(0,ans.length()-1); } };

  • @zony732
    @zony732 Місяць тому

    🔥🔥

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: string longestCommonPrefix(vector<string>& strs) { string ans; int n = strs.size(); int flag = 1; for (int j=0;j<strs[0].length();j++) { flag = 1; for (int i=1;i<n;i++) { if (strs[i][j] != strs[0][j]) { flag = 0; break; } } if (flag) { ans += strs[0][j]; } else { break; } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int lengthOfLastWord(string s) { int n = s.length(); int count = 0; int i; for (i=n-1;i>=0;i--) { if (s[i] != ' ') break; } for (;i>=0;i--) { if (s[i] != ' ') { count++; } else { break; } } return count; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int helper(int num, string &ans) { int quo; if (num>=1000) { quo = num/1000; num %= 1000; while(quo-- > 0) { ans += "M"; } } else if (num>=900) { ans += "CM"; num -= 900; } else if (num>=500) { ans += "D"; num -= 500; } else if (num>=400) { ans += "CD"; num -= 400; } else if (num>=100) { quo = num/100; num %= 100; while(quo-- > 0) { ans += "C"; } } else if (num>=90) { ans += "XC"; num -= 90; } else if (num >= 50) { ans += "L"; num -= 50; } else if (num >= 40) { ans += "XL"; num -= 40; } else if (num >= 10) { quo = num/10; num %= 10; while(quo-- > 0) { ans += "X"; } } else if (num >=9) { ans += "IX"; num -= 9; } else if (num >= 5) { ans += "V"; num -= 5; } else if (num >= 4) { ans += "IV"; num -= 4; } else { quo = num; num = 0; while(quo-- > 0) { ans += "I"; } } return num; } string intToRoman(int num) { string ans; while(num) { num = helper(num, ans); } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int romanToInt(string s) { int n = s.length(); int ans = 0; for (int i=0;i<n;i++) { if (i<(n-1)) { if (s[i] == 'I') { if (s[i+1] == 'V' || s[i+1] == 'X') { if (s[i+1] == 'V') { ans += 4; } else { ans += 9; } i++; continue; } } else if (s[i] == 'X') { if (s[i+1] == 'L' || s[i+1] == 'C') { if (s[i+1] == 'L') { ans += 40; } else { ans += 90; } i++; continue; } } else if (s[i] == 'C') { if (s[i+1] == 'D' || s[i+1] == 'M') { if (s[i+1] == 'D') { ans += 400; } else { ans += 900; } i++; continue; } } } if (s[i] == 'I') { ans++; } else if (s[i] == 'V') { ans += 5; } else if (s[i] == 'X') { ans+= 10; } else if (s[i] == 'L') { ans += 50; } else if (s[i] == 'C') { ans+= 100; } else if (s[i] == 'D') { ans += 500; } else{ ans+= 1000; } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int trap(vector<int>& height) { int n = height.size(); int maxi=0,ind=-1; int temp=0,ans=0; for (int i=0;i<n;i++) { if (height[i]>=maxi) { ans += temp; maxi = height[i]; ind=i; temp =0; } else { temp += (maxi - height[i]); } } temp = 0;maxi=0; for (int i=n-1;i>=ind;i--) { if (height[i]>=maxi) { ans += temp; maxi = height[i]; temp = 0; } else { temp += (maxi-height[i]); } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int candy(vector<int>& ratings) { int n = ratings.size(); vector<int> prefix(n,0); vector<int> suffix(n,0); int curr = 1;prefix[0] = 1; for (int i=1;i<n;i++) { if (ratings[i] > ratings[i-1]) { curr++; } else { curr = 1; // resetting value of curr as 1 } prefix[i] = curr; } suffix[n-1] = 1;curr=1; for (int i=n-2;i>=0;i--) { if (ratings[i] > ratings[i+1]) { curr++; } else { curr = 1; // resetting value of curr as 1 } suffix[i] = curr; } int ans = 0; for (int i=0;i<n;i++) { ans += max(prefix[i],suffix[i]); } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int curr = 0; int start = -1; int n = gas.size(); for (int i=0;i<n;i++) { curr += (gas[i] - cost[i]); if (start == -1) { if (curr >= 0) { start = i; } else { curr = 0; } } else { if (curr < 0) { curr = 0; start = -1; } } } if (start == -1) { return -1; } int curr2 = 0; for (int i=0;i<start;i++) { curr2 += (gas[i] - cost[i]); } if ((curr + curr2) >= 0) { return start; } else { return -1; } } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int n = nums.size(); int mul = 1; vector<int> prefix(n,0); vector<int> suffix(n,0); prefix[0] = nums[0]; for (int i=1;i<n;i++) { prefix[i] = prefix[i-1]*nums[i]; } suffix[n-1] = nums[n-1]; for (int i=n-2;i>=0;i--) { suffix[i] = suffix[i+1]*nums[i]; } vector<int> ans(n,0); ans[0] = suffix[1]; ans[n-1] = prefix[n-2]; for (int i=1;i<n-1;i++) { ans[i] = prefix[i-1]*suffix[i+1]; } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 Місяць тому

    Solution code - class RandomizedSet { public: vector<int> arr; map<int,int> map; RandomizedSet() { arr.clear(); map.clear(); } bool insert(int val) { // find number less than or equal to val if (map.find(val) == map.end()) { arr.push_back(val); map[val] = arr.size() - 1; return true; } else { return false; } } bool remove(int val) { if (map.find(val) == map.end()) { return false; } else { int lastEle = arr[arr.size() - 1]; swap(arr[arr.size() - 1], arr[map[val]]); map[lastEle] = map[val]; arr.pop_back(); map.erase(map.find(val)); return true; } } int getRandom() { int size = arr.size(); int index = rand()%size; return arr[index]; } }; /** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet* obj = new RandomizedSet(); * bool param_1 = obj->insert(val); * bool param_2 = obj->remove(val); * int param_3 = obj->getRandom(); */

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Code solution - class Solution { public: int hIndex(vector<int>& citations) { int n = citations.size(); int low = 0, high = 1000, ans= -1, mid, count; while(low<=high) { mid = (low + high)/2; count = 0; for (int i=0;i<n;i++) { if (citations[i] >= mid) { count++; } } if (count >= mid) { ans = mid; low = mid + 1; } else { high = mid - 1; } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Solution code - class Solution { public: int jump(vector<int>& nums) { int n = nums.size(); int jump = 1, maxReach = nums[0], curr = maxReach; if (n == 1) return 0; for (int i=1;i<n;i++) { curr--; maxReach = max(maxReach, i+nums[i]); if (i==n-1) break; if (curr == 0) { jump++; curr = maxReach-i; } } return jump; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Solution code - class Solution { public: bool canJump(vector<int>& nums) { int n = nums.size(); int maxi = 0; for (int i=0;i<n;i++) { if (i>maxi) return false; maxi = max(maxi,i+nums[i]); if (maxi>=n-1) return true; } return false; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Solution code - class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); vector<vector<int>> dp(2,vector<int>(n,0)); // size of dp is (2xn) dp[1][0] = (-1)*(prices[0]); // 0 represents sell state // 1 represents buy state for (int i=1;i<n;i++) { dp[0][i] = max(dp[0][i-1], dp[1][i-1] + prices[i]); dp[1][i] = max(dp[1][i-1], dp[0][i-1] - prices[i]); } return dp[0][n-1]; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Code solution - class Solution { public: int maxProfit(vector<int>& prices) { int mini = 1e9,ans=0; int n = prices.size(); for (int i=0;i<n;i++) { if ((prices[i] - mini)>0) ans = max(ans,prices[i] - mini); mini = min(prices[i],mini); } return ans; } };

  • @gagandeepsingh1533
    @gagandeepsingh1533 2 місяці тому

    why are we doing sorting here?

    • @CodingWithAnurag-1086
      @CodingWithAnurag-1086 2 місяці тому

      Thanks for pointing it out. Sorting is actually not required. I had done sorting so that solution visualisation becomes easy and even if we add sorting, it doesn't impact the overall time complexity. The overall time complexity is still O(n*n*logn)

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Solution code - class Solution { public: void reverseArray(vector<int> &nums, int start, int end) { int len = end - start + 1; for (int i=0;i<(len/2);i++) { swap(nums[start+i],nums[end-i]); } } void rotate(vector<int>& nums, int k) { int n = nums.size(); k %= n; reverseArray(nums,0,n-1); reverseArray(nums,0,k-1); reverseArray(nums,k,n-1); } };

  • @Prachikidiary
    @Prachikidiary 2 місяці тому

    Hello 😂

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 2 місяці тому

    Solution code - class Solution { public: int majorityElement(vector<int>& nums) { int n = nums.size(); int ans,count=0 ; for (int i=0;i<n;i++) { if (count == 0) ans = nums[i]; if (nums[i] == ans) count++; else count--; } return ans; } };

  • @kirant5548
    @kirant5548 3 місяці тому

    good one

  • @kirant5548
    @kirant5548 3 місяці тому

    good one

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 3 місяці тому

    Solution code - class Solution { public: int removeDuplicates(vector<int>& nums) { int n = nums.size(), counter = 1; for(int i=1;i<n;i++) { if (nums[i] != nums[i-1]) { nums[counter++] = nums[i]; } } return counter; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 3 місяці тому

    Solution code - class Solution { public: int removeDuplicates(vector<int>& nums) { int n = nums.size(), ans=1, curr=nums[0]; int count = 1; for (int i=1;i<n;i++) { if (nums[i] == curr) { if (count == 1) { nums[ans++] = nums[i]; count++; } } else { nums[ans++] = nums[i]; count = 1; curr = nums[i]; } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 3 місяці тому

    Solution code - class Solution { public: int removeElement(vector<int>& nums, int val) { int s = nums.size(), counter = 0; for ( int i =0;i<s;i++) { if(nums[i] != val) { nums[counter] = nums[i]; counter++; } } return counter; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 3 місяці тому

    Solution code - class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i =0, j=0, ind = m; while(i<m && j<n) { if (nums1[i]<nums2[j]) { nums1[(ind)%(m+n)] = nums1[i]; i++;ind++; } else { nums1[(ind)%(m+n)] = nums2[j]; j++;ind++; } } while(i<m){ nums1[(ind)%(m+n)] = nums1[i]; ind++;i++; } while(j<n) { nums1[(ind)%(m+n)] = nums2[j]; ind++;j++; } j=0; for(int k=m;k<(m+n);k++) { nums2[j] = nums1[k]; j++; } for(int k=m-1;k>=0;k--) { nums1[k+n]=nums1[k]; } for(int k=0;k<n;k++){ nums1[k]=nums2[k]; } } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 4 місяці тому

    Solution Code - class Solution { public: void dfs(vector<vector<int>> &adj, vector<bool> &visited, long long &count, int n,int ind) { visited[ind] = true; count++; int neighbour; for (int i=0;i<adj[ind].size();i++) { neighbour = adj[ind][i]; if (!visited[neighbour]) { dfs(adj,visited,count,n,neighbour); } } } long long countPairs(int n, vector<vector<int>>& edges) { vector<vector<int>> adj(n); int m = edges.size(); int u,v; for (int i=0;i<m;i++) { u = edges[i][0]; v = edges[i][1]; adj[u].push_back(v); adj[v].push_back(u); } vector<bool> visited(n,false); long long ans = 0,count=0; for (int i=0;i<n;i++) { if (!visited[i]) { count = 0; dfs(adj,visited,count,n,i); ans += (count)*(((long long) n) - count); } } ans/= (2LL); return ans; } };

  • @zakirsaifi7
    @zakirsaifi7 5 місяців тому

    public int maxSubarrayLength(int[] nums) { Deque<Integer> minIndicesStk = new ArrayDeque<>(); int n = nums.length; // Create a stack of distinct min indices in decreasing value for(int i=n-1; i>=0; i--) { if(minIndicesStk.isEmpty() || nums[i] < nums[minIndicesStk.peek()]) { minIndicesStk.push(i); } } // System.out.println(minIndicesStk); int maxLen = Integer.MIN_VALUE; for(int i = 0; i<nums.length; i++) { // Pop from stack until it is empty // or top element is greater than current element while(!minIndicesStk.isEmpty() && nums[i] > nums[minIndicesStk.peek()]) { // System.out.println("Element: " + nums[i] // + " min element: " // + nums[minIndicesStk.peek()] // + " index: " + minIndicesStk.peek()); int index = minIndicesStk.poll(); maxLen = Math.max(maxLen, index - i + 1); } } return maxLen == Integer.MIN_VALUE ? 0 : maxLen; }

  • @zakirsaifi7
    @zakirsaifi7 5 місяців тому

    Build a Stack of Minimum Indices: Iterates backwards through nums, pushing an index onto the stack if it corresponds to a new minimum element. This stack maintains indices of decreasing values from top to bottom. Compute Maximum Subarray Length: Iterates from the start of nums, popping from the stack until it finds an element smaller than the current one. This ensures the first element of the subarray is greater than the last. It updates the maximum length (maxLen) for each valid subarray found. Return Maximum Length: If no valid subarray is found, it returns 0; otherwise, it returns the maximum length of a subarray meeting the criteria. public int maxSubarrayLength(int[] nums) { Deque<Integer> minIndicesStk = new ArrayDeque<>(); int n = nums.length; // Create a stack of distinct min indices in decreasing value for(int i=n-1; i>=0; i--) { if(minIndicesStk.isEmpty() || nums[i] < nums[minIndicesStk.peek()]) { minIndicesStk.push(i); } } // System.out.println(minIndicesStk); int maxLen = Integer.MIN_VALUE; for(int i = 0; i<nums.length; i++) { // Pop from stack until it is empty // or top element is greater than current element while(!minIndicesStk.isEmpty() && nums[i] > nums[minIndicesStk.peek()]) { // System.out.println("Element: " + nums[i] // + " min element: " // + nums[minIndicesStk.peek()] // + " index: " + minIndicesStk.peek()); int index = minIndicesStk.poll(); maxLen = Math.max(maxLen, index - i + 1); } } return maxLen == Integer.MIN_VALUE ? 0 : maxLen; }

  • @zakirsaifi7
    @zakirsaifi7 5 місяців тому

    class Solution { public int maxSubarrayLength(int[] nums) { Deque<Integer> minIndicesStk = new ArrayDeque<>(); int n = nums.length; // Create a stack of distinct min indices in decreasing value for(int i=n-1; i>=0; i--) { if(minIndicesStk.isEmpty() || nums[i] < nums[minIndicesStk.peek()]) { minIndicesStk.push(i); } } // System.out.println(minIndicesStk); int maxLen = Integer.MIN_VALUE; for(int i = 0; i<nums.length; i++) { // Pop from stack until it is empty // or top element is greater than current element while(!minIndicesStk.isEmpty() && nums[i] > nums[minIndicesStk.peek()]) { // System.out.println("Element: " + nums[i] // + " min element: " // + nums[minIndicesStk.peek()] // + " index: " + minIndicesStk.peek()); int index = minIndicesStk.poll(); maxLen = Math.max(maxLen, index - i + 1); } } return maxLen == Integer.MIN_VALUE ? 0 : maxLen; } } - Build a Stack of Minimum Indices: Iterates backwards through nums, pushing an index onto the stack if it corresponds to a new minimum element. This stack maintains indices of decreasing values from top to bottom. - Compute Maximum Subarray Length: Iterates from the start of nums, popping from the stack until it finds an element smaller than the current one. This ensures the first element of the subarray is greater than the last. It updates the maximum length (maxLen) for each valid subarray found. - Return Maximum Length: If no valid subarray is found, it returns 0; otherwise, it returns the maximum length of a subarray meeting the criteria.

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution Code - class Solution { public: int findMaxArea(vector<int> arr) { int n = arr.size(); stack<int> st; int ans = 0,topEle; for (int i=0;i<n;i++) { if (st.empty()) { st.push(i); } else { while(!st.empty() && arr[i] < arr[st.top()]) { topEle = st.top(); st.pop(); if (st.empty()) { ans = max(ans,arr[topEle]*(i)); } else { ans = max(ans,arr[topEle]*(i-st.top()-1)); } } st.push(i); } } while(!st.empty()) { topEle = st.top(); st.pop(); if (st.empty()) { ans = max(ans,arr[topEle]*n); } else { ans = max(ans,arr[topEle]*(n-st.top()-1)); } } return ans; } int maximalRectangle(vector<vector<char>>& matrix) { int n = matrix.size(), m = matrix[0].size(); vector<vector<int>> grid(n,vector<int>(m,0)); for (int i=0;i<n;i++) { for (int j=0;j<m;j++) { if (matrix[i][j] == '1') { grid[i][j] = 1; } } } for (int i=1;i<n;i++) { for (int j=0;j<m;j++) { grid[i][j] = grid[i][j]*(grid[i][j] + grid[i-1][j]); } } /* 1 0 1 0 0 2 0 2 1 1 3 1 3 2 2 4 0 0 3 0 */ int ans = 0; for (int i=0;i<n;i++) { ans = max(ans,findMaxArea(grid[i])); } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution Code - class Solution { public: int largestRectangleArea(vector<int>& heights) { stack<int> st; int n = heights.size(); int ans = 0,topEle; // Iterate through each element for (int i=0;i<n;i++) { if (st.empty()) { st.push(i); } else { if (heights[i] >= heights[st.top()]) { st.push(i); } else { while(!st.empty() && heights[i] < heights[st.top()]) { topEle = st.top(); st.pop(); if (st.empty()) { ans = max(ans,(heights[topEle]*i)); } else { ans = max(ans, (heights[topEle]*(i-1-st.top()))); } } st.push(i); } } } // Empty our stack while(!st.empty()) { topEle = st.top(); st.pop(); if (st.empty()) { ans = max(ans,(heights[topEle]*n)); } else { ans = max(ans, (heights[topEle]*(n-1-st.top()))); } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution Code - class Solution { public: int maxSubarrayLength(vector<int>& nums) { int n = nums.size(); vector<pair<int,int>> dec; int ans = 0; for (int i=n-1;i>=0;i--) { int temp = nums[i]; int low=0,high = dec.size()-1; while(low <= high) { int mid = (low+high)/2; if (temp <= dec[mid].first) { low = mid + 1; // move to the right of dec } else { ans = max(dec[mid].second - i + 1,ans); high = mid - 1; // move to the left of dec } } if (dec.size() == 0 || temp < dec[dec.size()- 1].first) { dec.push_back({temp,i}); } } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution code - #define ll long long class Solution { public: ll power(ll x, int y, int p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p ll modInverse(ll n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's little // theorem. ll nCrModPFermat(ll n, int r, int p) { // If n<r, then nCr should return 0 if (n < r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r ll fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } int countKSubsequencesWithMaxBeauty(string s, int k) { if (k>26) { return 0; } int n = s.length(); ll mod = 1e9+7; vector<ll> count(26,0); for (int i=0;i<n;i++) { count[s[i] - 'a']++; } sort(count.begin(),count.end()); reverse(count.begin(),count.end()); ll ans = 1; for (int i= 0;i<k && i<26;i++) { ans = (ans * count[i])%mod; } ll temp = count[k-1]; int covered = 0,curr= 0; for (int i= 0;i<26;i++) { if (curr == 0) { if (count[i] == temp) { covered = i; curr++; } } else { if (count[i] == temp) { curr++; } else { break; } } } // curr C k-covered ans = (ans * nCrModPFermat(curr,k-covered,mod))%mod; return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution Code - #define ll long long class Solution { public: int maximumDetonation(vector<vector<int>>& bombs) { int n = bombs.size(); vector<vector<int>> adj(n); for (int i=0;i<n;i++) { for (int j=i+1;j<n;j++) { ll dist = pow((bombs[i][0] - bombs[j][0]), 2) + pow((bombs[i][1] - bombs[j][1]), 2); ll r1 = pow(bombs[i][2],2); ll r2 = pow(bombs[j][2],2); if (r1 >= dist) { adj[i].push_back(j); } if (r2 >= dist) { adj[j].push_back(i); } } } int ans = 0,count=0; vector<bool> visited(n,false); for (int i =0;i<n;i++) { for (int j=0;j<n;j++) visited[j] = false; count = 0; queue<int> qu; qu.push(i); count++; visited[i] = true; while(!qu.empty()) { int fr = qu.front(); qu.pop(); for (int j=0;j<adj[fr].size();j++) { if (!visited[adj[fr][j]]) { qu.push(adj[fr][j]); count++; visited[adj[fr][j]] = true; } } } ans = max(ans,count); } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution Code: class Solution { public: int countTime(string time) { int ans = 1; if(time[0] == '?') { if (time[1] == '?') { ans = 24; } else { if (time[1] == '0' || time[1] == '1' || time[1] == '2' || time[1] == '3') { ans = 3; // time[0] can take 3 values which are 0,1,2 } else { ans = 2; // time[0] can take 2 values which are 0,1 } } } else if (time[1] == '?') { if (time[0] == '?') { ans = 24; } else { if (time[0] == '2') { ans = 4; // time[1] can take 4 values which are 0,1,2,3. } else { ans = 10; // time[1] can take 10 values from 0 to 9 } } } if (time[3] == '?') { ans*= 6; // time[3] can take 6 values which are 0 to 5 } if (time[4] == '?') { ans*= 10; // time[4] can take 10 values which are 0 to 9 } return ans; } };

  • @CodingWithAnurag-1086
    @CodingWithAnurag-1086 5 місяців тому

    Solution Code - class Solution { public: int earliestAcq(vector<vector<int>>& logs, int n) { sort(logs.begin(),logs.end()); vector<int> par(n,0); vector<int> si(n,1); for (int i=0;i<n;i++) { par[i]= i; } int connections = 0; for (int i=0;i<logs.size();i++) { int timestamp = logs[i][0]; int node1 = logs[i][1]; int node2 = logs[i][2]; // Calculate parent of node1 while(node1 != par[node1]) { node1 = par[node1]; } // Calculate parent of node2 while(node2 != par[node2]) { node2 = par[node2]; } if (node1 != node2) { connections++; if (si[node1] > si[node2]) { si[node1] += si[node2]; par[node2] = node1; } else { si[node2] += si[node1]; par[node1] = node2; } if (connections == (n-1)) { return timestamp; } } } return -1; } };

  • @divyagupta9314
    @divyagupta9314 5 місяців тому

    Wowww….Carry on…