LeetCode, GeeksForGeeks Problem of the day solution
Open in Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Show more1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution {
public:
bool checkPalindrome(string s){
int i=0;
int j=s.size()-1;
while(i& words) {
for(int i=0;i
class Solution {
public:
Node* cloneGraph(Node* node) {
if(!node) return NULL;
unordered_map mp;
queue q;
q.push(node);
mp[node] = new Node(node->val);
while(!q.empty()){
Node* tmp = q.front();
q.pop();
for(auto &x : tmp->neighbors){
if(!mp.count(x)) mp[x] = new Node(x->val), q.push(x);
mp[x]->neighbors.push_back(mp[tmp]);
}
}
return mp[node];
}
};
class Solution {
public:
int majorityElement(vector& nums) {
int majorityIndex = 0;
int cnt = 1;
for(int i=0;i
class Solution{
public:
long long sequence(int n){
long mod=1000000007;
long sum=0;
long count=1;
for(int i=1;i<=n;i++){
long mul=1;
for(int j=1;j<=i;j++){
mul=(mul*count)%mod;
count++;
}
sum=(sum+mul)%mod;
}
return sum%mod;
}
};
class Solution {
public:
int solve(int m, int n, int i, int j1, int j2, vector> &grid, vector>>& dp){
if(j1<0 or j2<0 or j1>=n or j2>=n){
return -1e8;
}
if(i==m-1){
if(j1==j2){
return grid[i][j1];
}
else{
return grid[i][j1]+grid[i][j2];
}
}
if(dp[i][j1][j2] != -1){
return dp[i][j1][j2];
}
int maxi=-1e8;
for(int dj1=-1;dj1<=1;dj1++){
for(int dj2=-1;dj2<=1;dj2++){
int val = 0;
if(j1==j2){
val = grid[i][j1];
}
else{
val = grid[i][j1] + grid[i][j2];
}
val += solve(m , n, i+1, j1+dj1, j2+dj2, grid, dp);
maxi = max(maxi, val);
}
}
return dp[i][j1][j2]= maxi;
}
int cherryPickup(vector>& grid) {
int m=grid.size();
int n=grid[0].size();
int i=0;
int j1=0;
int j2=n-1;
vector>> dp(m, vector>(n, vector(n, -1)));
return solve(m, n, i, j1, j2, grid, dp);
}
};
class Solution{
public:
vector recamanSequence(int n){
unordered_map visited;
vector ans(n,0);
visited[ans[0]]=true;
for(int i=1;i0 && !visited[ans[i-1]-i])
ans[i]=ans[i-1]-i;
else
ans[i]=ans[i-1]+i;
visited[ans[i]]=true;
}
return(ans);
}
};
class Solution {
public:
int count{0};
bool isPalindrome(string str){
int left = 0;
int right = str.size()-1;
while(left
class Solution {
public:
long long dp[101][101][101];
long long solve(int x, int y,int n, int k, vector>& arr){
if(x >= n or y >= n or k<0) return 0ll;
if(x == n-1 and y == n-1 and k - arr[x][y] == 0) return 1ll;
if(dp[k][x][y]!=-1) return dp[k][x][y];
return dp[k][x][y] = solve(x+1,y,n,k-arr[x][y],arr) + solve(x,y+1,n,k-arr[x][y],arr);
}
long long numberOfPath(int n, int k, vector> arr){
memset(dp,-1,sizeof(dp));
return solve(0ll,0ll,n,k,arr);
}
};
class Solution {
public:
vector largestDivisibleSubset(vector nums) {
std::sort(nums.begin(), nums.end());
vector prev(nums.size(),-1);
vector size(nums.size(),1);
for(int i = 1;i=0;--j){
if(nums[i]%nums[j]==0 && size[j]+1>size[i]){
size[i] = size[j]+1;
prev[i] = j;
}
}
}
int maxIndex = -1;
int max = -1;
for(int i = 0;imax){
max = size[i];
maxIndex = i;
}
}
vector res(max);
int index = 0;
int cur = maxIndex;
while(cur!=-1){
res[index++] = nums[cur];
cur = prev[cur];
}
return res;
}
};
class Solution{
public:
//Function to check whether all nodes of a tree have the value
//equal to the sum of their child nodes.
bool check(Node *root){
if(root == NULL)
return true;
if(root->left == NULL && root->right == NULL)
return true;
int sum = 0;
if(root->left != NULL)
sum += root->left->data;
if(root->right != NULL)
sum += root->right->data;
return (sum == root->data && check(root->left) && check(root->right));
}
public:
//Function to check whether all nodes of a tree have the value
//equal to the sum of their child nodes.
int isSumProperty(Node *root)
{
return check(root);
}
};
