LeetCode, GeeksForGeeks Problem of the day solution
رفتن به کانال در Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
نمایش بیشتر1 250
مشترکین
+224 ساعت
+147 روز
+2930 روز
آرشیو پست ها
class Solution {
public:
vector splitListToParts(ListNode* head, int k) {
int size = 0;
ListNode* temp = head;
vector ans;
while(temp!= NULL){
temp=temp->next;
size++;
}
int total_parts=size/k;
int extra_parts=size%k;
while(k--){
int s=total_parts;
if(extra_parts){
s++;
extra_parts--;
}
if(s==0){
ans.push_back(NULL);
continue;
}
ListNode* start=head;
ListNode* prev=NULL;
while(s-- && head!= NULL){
prev=head;
head=head->next;
}
if(prev!= NULL) prev->next=NULL;
ans.push_back(start);
}
return ans;
}
};
class Solution
{
public:
//Function to find a Mother Vertex in the Graph.
void dfs(int i,set&v2,vector&vis,vectoradj[]){
vis[i] = 1;
v2.insert(i);
for(auto adjNode : adj[i]){
if(v2.find(adjNode)==v2.end()) dfs(adjNode,v2,vis,adj);
}
}
int findMotherVertex(int V, vectoradj[])
{
// Code here
vectorvis(V,0);
for(int i=0;iv2;
if(!vis[i]){
dfs(i,v2,vis,adj);
}
if(v2.size()==V) return i;
}
return -1;
}
};
class Solution {
public:
Node* copyRandomList(Node* head) {
map m;
int i=0;
Node* ptr = head;
while (ptr) {
m[ptr] =new Node(ptr->val);
ptr = ptr->next;
}
ptr = head;
while (ptr) {
m[ptr]->next = m[ptr->next];
m[ptr]->random = m[ptr->random];
ptr = ptr->next;
}
return m[head];
}
};
class Solution {
public:
// Function to return the adjacency list for each vertex.
vector> printGraph(int V, vector>edges) {
vector> adj(V);
for(int i=0;i
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * slow = head;
ListNode * fast = head;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
if(slow==fast){
return true;
}
}
return false;
}
};
class Solution{
public:
void dfs(int row, int col, vector>& mat, vector>& vis, int n, int m){
vis[row][col] = 1;
int ra[] = {-1, 0, 1, 0};
int rc[] = {0, 1, 0, -1};
for(int i=0;i<4;i++){
int r = row+ra[i];
int c = col + rc[i];
if(r>=0 && r=0 && c> fill(int n, int m, vector> mat)
{
vector> vis(n, vector(m, 0));
for(int i=0;i
class Solution {
public:
int solve (int m, int n,vector>&dp)
{
if(m==0 && n==0)
return 1;
if(m<0||n<0)
return 0;
if(dp[m][n]!=-1)
return dp[m][n];
int up=solve(m-1,n,dp);
int left=solve(m,n-1,dp);
return dp[m][n]= up+left;
}
int uniquePaths(int m, int n) {
vector>dp(m,vector(n,-1));
return solve(m-1,n-1,dp);
}
};
class Solution{
public:
// Return True if the given trees are isomotphic. Else return False.
bool isIsomorphic(Node *root1,Node *root2)
{
if (root1 == NULL && root2 == NULL)
return true;
if (!root1 || !root2)
return false;
if(root1->data!=root2->data) return false;
bool leftIsomorphic = isIsomorphic(root1->left, root2->left) && isIsomorphic(root1->right, root2->right);
bool mirrorIsomorphic = isIsomorphic(root1->left, root2->right) && isIsomorphic(root1->right, root2->left);
return leftIsomorphic || mirrorIsomorphic;
}
};
class Solution {
public:
unordered_set st;
int dp[51];
int solve(string& s, int index){
if(index>=s.size()){
return 0;
}
if(dp[index] != -1){
return dp[index];
}
int len = INT_MAX;
for(int i=1;i<=s.size();i++){
string str = s.substr(index, i);
if(st.find(str) != st.end()){
len = min(len, solve(s, index+i));
}
}
len = min(len, 1+solve(s, index+1));//Skipping the char which may be the the probable extra char
return dp[index] = len;
}
int minExtraChar(string s, vector& d) {
for(int i=0;i
class Solution
{
public:
int getCount(Node *root, int k)
{
int level=0;
int count=0;
queueq;
q.push(root);
while(!q.empty()){
int size=q.size();
level++;
while(size>0){
Node* temp=q.front();
q.pop();
if(!temp->left && !temp->right){
if(level<=k){
count++;
k=k-level;
}
}
if(temp->left) q.push(temp->left);
if(temp->right)q.push(temp->right);
size--;
}
}
return count;
}
};
