This file contains hidden or 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 precomputePrefixSum(vector<int>& prefixSumA, vector<int>& prefixSumB, | |
const string& s, const int& n, const int& a, const int& b) { | |
prefixSumA[0] = prefixSumB[0] = 0; | |
for (int i = 0; i < n; ++i) { | |
if (s[i] - '0' == a) prefixSumA[i+1] = 1 + prefixSumA[i]; | |
else prefixSumA[i+1] = prefixSumA[i]; | |
if (s[i] - '0' == b) prefixSumB[i+1] = 1 + prefixSumB[i]; | |
else prefixSumB[i+1] = prefixSumB[i]; |
This file contains hidden or 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 clearStars(string s) { | |
vector<vector<int>> char_pos(26); | |
for(int i=0;s[i];++i){ | |
if(s[i]=='*'){ | |
for(int j=0;j<26;++j){ | |
if(char_pos[j].size()>0){ | |
s[char_pos[j].back()]='*'; | |
char_pos[j].pop_back(); |
This file contains hidden or 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 { | |
inline char smallest(vector<int>& right_freq){ | |
for(int i=0;i<26;++i){ | |
if(right_freq[i]>0) | |
return char(97+i); | |
} | |
return 'z'; | |
} | |
public: | |
string robotWithString(string s) { |
This file contains hidden or 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 { | |
int find(vector<int>& dsuf,int v){ | |
if(dsuf[v]==-1) | |
return v; | |
return dsuf[v]=find(dsuf,dsuf[v]); | |
} | |
public: | |
string smallestEquivalentString(string s1, string s2, string baseStr) { | |
vector<int> dsuf(26,-1); |
This file contains hidden or 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 answerString(string word, int numFriends) { | |
int n = word.size(); | |
if(numFriends==1) return word; | |
int i = 0; | |
int j = 1; | |
int gap = 0; |
This file contains hidden or 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 maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) { | |
int n=status.size(); | |
//Apply BFS from start points: Multi-Source BFS (DAG: Since Box-A inside Box-B then opposite is NOT True) | |
queue<int> q; | |
for(int start: initialBoxes) | |
q.push(start); | |
This file contains hidden or 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 candy(vector<int>& ratings) { | |
int n=ratings.size(); | |
vector<int> candies(n,1); | |
//Step-1: Assign increasing curve with increasing candies | |
for(int i=1;i<n;++i){ | |
if(ratings[i]>ratings[i-1]) | |
candies[i] = 1 + candies[i-1]; |
This file contains hidden or 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 | |
ll combinations(ll n){ | |
if(n<0) | |
return 0; | |
return 1LL*(n+1)*(n+2)/2; | |
} | |
public: |
This file contains hidden or 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 snakesAndLadders(vector<vector<int>>& board) { | |
int n = board.size(); | |
int MAX = n * n; | |
queue<int> q; | |
q.push(1); | |
vector<bool> visited(MAX + 1, false); | |
visited[1] = true; | |
This file contains hidden or 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 pii pair<int,int> | |
public: | |
int closestMeetingNode(vector<int>& edges, int node1, int node2) { | |
int n=edges.size(); | |
/* | |
dist[A][0]=X: Distance from node1(source1) to 'node A' is X | |
dist[A][1]=Y: Distance from node2(source2) to 'node A' is Y | |
*/ | |
vector<vector<int>> dist(n,vector<int>(2,-1)); |
NewerOlder