LeetCode, GeeksForGeeks Problem of the day solution
Ir al canal en Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Mostrar más1 250
Suscriptores
+224 horas
+147 días
+2930 días
Archivo de publicaciones
class Solution {
public:
string convertToTitle(int n) {
string res="";
while(n>0){
res=char('A'+(n-1)%26)+res;
n=(n-1)/26;
}
return res;
}
};
class Solution
{
public:
//Function to find minimum number of operations that are required
//to make the matrix beautiful.
int findMinOpeartion(vector > matrix, int n)
{
// code here
vector v;
int count=0;
for(int i=0;i v1;
for(int i=0;i
class Solution {
public:
bool repeatedSubstringPattern(string s) {
return (s + s).find(s, 1) < s.size();
}
};
class Solution {
public:
int Count(vector >& matrix) {
int n=matrix.size();
int m=matrix[0].size();
int ans=0;
for(int i=0;i=0 && newRow=0 && newCol0) ans++;
}
}
}
return ans;
}
};
class Solution {
public:
vector sortItems(int n, int m, vector& group, vector>& before)
{
vector > adjV(n),adjG(m),grp(m);
vector inDegV(n,0), inDegG(m,0);
int sz=before.size();
for(int i=0;ivisG(m,false);
queueq;
for(int i=0;igroupOrder;
while(!q.empty())
{
int node=q.front();
q.pop();
groupOrder.push_back(node);
visG[node]=true;
for(auto itr: adjG[node])
{
if(!visG[itr])
{
inDegG[itr]--;
if(inDegG[itr]==0)
{
q.push(itr);
}
}
}
}
if(groupOrder.size()visV(n,false);
vector ans;
int cnt=0;
for(int i=0;i
class Solution{
public:
/* if x is present in arr[] then returns the count
of occurrences of x, otherwise returns 0. */
int count(int arr[], int n, int x) {
unordered_map mp;
for(int i=0;i
class DSU {
public:
vector parent, rank;
DSU(int n) {
parent.resize(n, 0);
rank.resize(n, 0);
// Initialize parent array with initial values
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
// Find operation with path compression
int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
// Union operation with rank optimization
bool Union(int x, int y) {
int parentX = find(x), parentY = find(y);
if (parentX == parentY) return false;
if (rank[parentX] > rank[parentY]) {
parent[parentY] = parentX;
} else if (rank[y] > rank[x]) {
parent[parentX] = parentY;
} else {
parent[parentX] = parentY;
rank[parentY]++;
}
return true;
}
};
int getMST(int n, vector> &edges, int exclude = -1, int include = -1) {
DSU dsu(n);
int weight = 0;
if (include != -1) {
weight += edges[include][2];
dsu.Union(edges[include][0], edges[include][1]);
}
for (int i = 0; i < edges.size(); i++) {
if (i == exclude) continue;
if (dsu.find(edges[i][0]) == dsu.find(edges[i][1])) continue;
dsu.Union(edges[i][0], edges[i][1]);
weight += edges[i][2];
}
// Check if all nodes are in the same connected component
for (int i = 0; i < n; i++) {
if (dsu.find(i) != dsu.find(0)) return 1e9 + 7;
}
return weight;
}
class Solution {
public:
vector> findCriticalAndPseudoCriticalEdges(int n, vector>& edges) {
vector> ans;
// Add the edge index to each edge for tracking
for (int i = 0; i < edges.size(); i++) {
edges[i].push_back(i);
}
// Sort edges based on weight
sort(edges.begin(), edges.end(), [](const vector& a, const vector& b) {
return a[2] < b[2];
});
int original = getMST(n, edges, -1);
vector crital, psuedo;
// Find critical and pseudo-critical edges
for (int i = 0; i < edges.size(); i++) {
if (original < getMST(n, edges, i)) {
crital.push_back(edges[i][3]); // Store index of critical edge
} else if (original == getMST(n, edges, -1, i)) {
psuedo.push_back(edges[i][3]); // Store index of pseudo-critical edge
}
}
return {crital, psuedo};
}
};
class Solution
{
public:
//Function to find a continuous sub-array which adds up to a given number.
vector subarraySum(vectorarr, int n, long long s)
{
int i=0,j=0,sum=arr[0];
vector v;
while(j
class Solution {
public:
int maximalNetworkRank(int n, vector>& roads) {
std::vector degrees(n, 0);
std::unordered_map> directlyConnected;
for (const auto& road : roads) {
degrees[road[0]]++;
degrees[road[1]]++;
directlyConnected[road[0]].insert(road[1]);
directlyConnected[road[1]].insert(road[0]);
}
int result = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (directlyConnected[i].count(j)) {
result = std::max(result, degrees[i] + degrees[j] - 1);
} else {
result = std::max(result, degrees[i] + degrees[j]);
}
}
}
return result;
}
};
class Solution{
//Function to find the leaders in the array.
public:
vector leaders(int a[], int n){
vector ans;
ans.push_back(a[n-1]);
for(int i=n-2;i>=0;i--){
if(ans.back()<=a[i]){
ans.push_back(a[i]);
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
