GeeksForGeeks - POTD | GFG POTD Answer
قناة بسيطة
1 218
المشتركون
لا توجد بيانات24 ساعات
-97 أيام
-5730 أيام
أرشيف المشاركات
class Solution{
public:
int f(Node *root)
{
if(root==NULL)return 0;
int left=1+f(root->left);
int right=1+f(root->right);
return max(left,right);
}
int maxDepth(Node *root) {
return f(root);
}
};
class Solution
{
public:
struct Node* makeUnion(struct Node* head1, struct Node* head2)
{
sets;
Node* curr=head1;
while(curr){
s.insert(curr->data);
curr=curr->next;
}
curr=head2;
while(curr){
s.insert(curr->data);
curr=curr->next;
}
Node *head= new Node(0);
curr=head;
for(auto it : s){
curr->next=new Node(it);
curr=curr->next;
}
return head->next;
}
};
class Solution{
public:
const int mod = 1e9 + 7;
int nCr(int n, int r){
if(n < r)
{
return n - n;
}
// USING PASCAL TRIANGLE APPROACH
vector> v(n + 1, vector (r + 1, 0));
// vector> vv;
for (int i = 0; i <= n; i++)
{
// vector v;
for (int j = 0; j <= min(r, i); j++)
{
if(j == 0 || j == i)
{
v[i][j] = 1;
}
else
{
v[i][j] = (v[i - 1][j] + v[i - 1][j - 1]) % mod;
}
}
}
return v[n][r];
}
};
class Solution
{
public:
// #define MAX 1000
vector<vector<int>> uniqueRow(int M[MAX][MAX],int row,int col)
{
vector<vector<int>> res;
map<vector<int> , int> mp;
for(int i =0; i<row; i++){
vector<int> temp;
for(int j=0; j<col; j++){
temp.push_back(M[i][j]);
mp[temp]++;
}
if(mp[temp]<2){
res.push_back(temp);
}
}
return res;
}
};
class Solution{
public:
int klengthpref(string arr[], int n, int k, string str){
int count=0;
for(int i=0;i
class Solution {
public:
int leastInterval(int N, int K, vector &tasks) {
vectormp(26);
int ans=0;
for(auto &i:tasks){
ans=max(ans,++mp[i-'A']);
}
int ct=0;
for(int i=0;i<26;i++){
if(ans==mp[i])ct++;
}
return max(((ans - 1) * (K + 1)) + ct,N);
}
};
class Solution {
public:
bool lemonadeChange(int n, vector &v) {
bool ok=true;
unordered_map mp;
for(auto val:v){
int bill = val-5;
if (bill==0) mp[5]++;
else if (bill == 5){
if (mp[5] == 0) return false;
else {
mp[5]-=1;
mp[10]++;
}
}
else if (bill == 15){
if (mp[10]!=0){
mp[10]-=1;
if (mp[5]==0) return false;
else mp[5]-=1;
}
else {
if (mp[5]<3) return false;
else mp[5]-=3;
}
}
}
return true;
}
};
class Solution {
public:
int sumOfNaturals(int n) {
long long m=1000000007;
long long sum=(n*((n+1)%m)/2)%m;
return sum;
}
};
class Solution {
public:
int matchGame(long long n) {
return n%5?n%5:-1;
}
};
class Solution{
public:
// arr: input array
// n: size of array
//Function to rearrange an array so that arr[i] becomes arr[arr[i]]
//with O(1) extra space.
void arrange(long long arr[], int n) {
// Your code here
int i;
long long mx=n;
for(i=0;i
