This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Followup with K increasing subsequence | |
class Solution { | |
public: | |
bool increasingTriplet(vector<int>& nums) { | |
int n=nums.size(); | |
int k=3; | |
vector<long long> increasing(k,LONG_MAX); | |
//Find an increasing subsequence of length k | |
for(int i=0;i<n;++i){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> closestPrimes(int left, int right) { | |
//Apply sieve of eratosthenes | |
vector<bool> isPrime(right+1,true); | |
for(int i=2;i*i<=right;++i){ | |
if(isPrime[i]==true){ | |
for(int j=2;i*j<=right;++j) | |
isPrime[i*j]=false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
bool isPalindrome(int no){ | |
string s = to_string(no); | |
int left=0,right=s.size()-1; | |
while(left<right){ | |
if(s[left]!=s[right]) | |
return false; | |
left++; | |
right--; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
long long permutation(int count,int MOD){ | |
long long ans = 1; | |
for(int i=2;i<=count;++i) | |
ans = (ans*i)%MOD; | |
return ans; | |
} | |
public: | |
int numPrimeArrangements(int n) { | |
int MOD = 1e9+7; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
void calculate_longestPrefixSuffix(string &needle,int m,vector<int>& lps){ | |
int l=0,r=1; | |
while(r<m){ | |
//If R-th char matches with L-th char | |
if(needle[l]==needle[r]){ | |
lps[r] = 1+l; | |
l++; | |
r++; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int bagOfTokensScore(vector<int>& tokens, int power) { | |
int n = tokens.size(); | |
sort(tokens.begin(),tokens.end());//sort to make choice-maing obvious | |
//2-pointer approach | |
int left=0,right=n-1; | |
int score = 0,ans = 0; | |
while(left<=right){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> findSubstring(string s, vector<string>& words) { | |
unordered_map<string,int> freq,curr; | |
for(string& word: words) | |
freq[word]++; | |
int len = s.size(); | |
int n = words.size(); | |
int wordSize = words[0].size(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string maximumOddBinaryNumber(string s) { | |
int count = 0; | |
int n = s.size(); | |
for(int i=0;i<n;++i){ | |
if(s[i]=='1'){ | |
count++; | |
s[i]='0'; //Reset value to 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
#define ll long long | |
public: | |
long long largestSquareArea(vector<vector<int>>& bottomLeft, vector<vector<int>>& topRight) { | |
int n = bottomLeft.size(); | |
vector<vector<int>> rectangles(n,vector<int>(4)); | |
for(int i=0;i<n;i++){ | |
rectangles[i][0] = bottomLeft[i][0]; | |
rectangles[i][1] = bottomLeft[i][1]; | |
rectangles[i][2] = topRight[i][0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxNumberOfFamilies(int n, vector<vector<int>>& rseats) { | |
rseats.push_back({0,10}); | |
rseats.push_back({n+1,1}); | |
sort(rseats.begin(),rseats.end()); | |
int size = rseats.size(); | |
int count = 0; | |
int skipRows,skipCols; |
NewerOlder