fa
Feedback
LeetCode, GeeksForGeeks Problem of the day solution

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: int DivisibleByEight(string s){ int n = s.size(); if(n<=3){ int a = stoi(s); if(a%8==0)return 1; else return -1; } string lastThreeDigit = s.substr(n-3,3); int a = stoi(lastThreeDigit); return a%8==0?1:-1; } };

GFG | Problem of the day :

class Solution { public: pair solve(TreeNode* root){ if(root==NULL)return {0,0}; pair left=solve(root->left); pair right=solve(root->right); int a=left.first; int b=right.first; int c=left.second+right.second; pair ans; ans.first=max(a,max(b,c)); ans.second=max(left.second,right.second)+1; return ans; } int diameterOfBinaryTree(TreeNode* root) { return solve(root).first; } };

LeetCode | Daily challenge :

int* game_with_number(int arr[], int n) { for(int i=0;i

GFG | Problem of the day :

class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p==NULL || q==NULL){ return p==q; } return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };

LeetCode | Daily challenge :

class Solution{ void solve(int ind, string &s, string &ds, vector &ans){ if(ind >= s.size()){ if(ds.size() > 0) ans.push_back(ds); return; } ds.push_back(s[ind]); solve(ind + 1, s, ds, ans); ds.pop_back(); solve(ind + 1, s, ds, ans); } public: vector AllPossibleStrings(string s){ // Code here vector ans; string ds; solve(0, s, ds, ans); sort(ans.begin(), ans.end()); return ans; } };

GFG | Problem of the day :

#define ll long long const ll MAX = 1e5 + 5; bool prime[MAX]; ll spf[MAX]; void sieve(){ fill(prime, prime+MAX, true); for(int i=1;i<MAX;i++){ spf[i] = i; } prime[0] = prime[1] = false; for(ll i=2;i*i<MAX;i++){ if(prime[i]){ for(ll j=i*i;j<MAX;j=j+i){ if(prime[j]){ spf[j]=i; } prime[j] = false; } } } } vector<ll> getFactorization(ll x){ vector<ll> ret; unordered_map<ll, ll> mp; while(x != 1){ mp[spf[x]]++; x = x/spf[x]; } for(auto it:mp){ ret.push_back(it.first); // ret.push_back(it.second); } return ret; } class Solution { public: void dfs(int node, vector<ll> adj[], ll& cnt, vector<int>& vis){ vis[node] = 1; cnt++; for(auto it:adj[node]){ if(vis[it]==0){ dfs(it, adj, cnt, vis); } } } bool canTraverseAllPairs(vector<int>& nums) { sieve(); int n = nums.size(); unordered_map<ll, vector<ll>> mp; for(ll i=0;i<n;i++){ mp[i] = getFactorization(nums[i]); } unordered_map<ll, vector<ll>> rmp; for(auto it:mp){ ll ind = it.first; for(auto prime:it.second){ rmp[prime].push_back(ind); } } vector<ll> adj[n]; for(auto it:rmp){ vector<ll> ind=it.second; if(ind.size()<2){ continue; } for(ll i=1;i<ind.size();i++){ int u = ind[i-1]; int v = ind[i]; adj[u].push_back(v); adj[v].push_back(u); } } ll cnt=0; vector<int> vis(n, 0); dfs(0, adj, cnt, vis); return cnt==n; } };

LeetCode | Daily challenge :

class Solution { public: // Complete this function long long int count(long long int n) { // Your code here vectordp(n+1,0); dp[0] = 1; for(int i=3;i<=n;i++){ dp[i] +=dp[i-3]; } for(int i=5;i<=n;i++){ dp[i] +=dp[i-5]; } for(int i=10;i<=n;i++){ dp[i] +=dp[i-10]; } return dp[n]; } };

GFG | Problem of the day :

class Solution { public: vector findAllPeople(int n, vector>& meetings, int firstPerson) { vector ans; ans.push_back(0); vector secretTime(n,INT_MAX); secretTime[0]=0; secretTime[firstPerson]=0; unordered_map>> adj; for(int i=0;i q; q.push(0); q.push(firstPerson); while(!q.empty()){ int person1=q.front(); q.pop(); for(auto i : adj[person1]){ int person2=i.first; int meetingTime=i.second; if(meetingTime>=secretTime[person1]&&meetingTime < secretTime[person2]){ secretTime[person2]=meetingTime; q.push(person2); } } } for(int i=1;i

LeetCode | Daily challenge :

class Solution { public: int maxSum(int n) { if((n/2+n/3+n/4)<=n) return n; return maxSum(n/2)+maxSum(n/3)+maxSum(n/4); } };

GFG | Problem of the day :

class Solution { public: int findCheapestPrice(int n, vector>& flights, int src, int dst, int k) { vector> adjList[n]; for(vector edge: flights) { adjList[edge[0]].push_back({edge[1], edge[2]}); } vector distance(n, 1e7); queue> Queue; Queue.push({src, 0}); distance[src] = 0; while(k >= 0 && !Queue.empty()) { int sz = Queue.size(); for(int i = 0; i < sz; i++) { pair curr = Queue.front(); Queue.pop(); for(pair edge: adjList[curr.first]) { int next = edge.first; int weight = edge.second; int cost = curr.second + weight; if(cost <= distance[next]) { distance[next] = cost; Queue.push({next, cost}); } } } k--; } return distance[dst] == 1e7? -1: distance[dst]; } };

LeetCode | Daily challenge :