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 garbageCollection(vector& garbage, vector& travel) {
int lastg=0, lastm=0, lastp=0, total=0;
for(int i=1; i
class Solution{
public:
int sumK(Node *root,int k)
{
unordered_map f;
f[0] = 1;
long long ans = 0;
const long long mod = 1e9 + 7;
function dfs = [&](Node * node, int sum) {
if(node == nullptr)
return 0;
sum += node -> data;
if(f.find(sum - k) != f.end())
ans = (ans + f[sum - k]) % mod;
++f[sum];
dfs(node -> left, sum);
dfs(node -> right, sum);
--f[sum];
if(f[sum] == 0)
f.erase(sum);
};
dfs(root, 0);
return ans;
}
};
class Solution {
public:
int reductionOperations(vector<int>& nums) {
priority_queue<pair<int,int>> pq;
map<int,int> mp;
int answer = 0;
for(int i = 0; i < nums.size(); i++){
mp[nums[i]]++;
}
for(auto data : mp){
pq.push(make_pair(data.first, data.second));
}
int current = pq.top().second;
while(pq.size() > 1){
pair<int,int> right = pq.top();
pq.pop();
pair<int,int> left = pq.top();
if(right.first != left.first){
answer += current;
current += left.second;
}
}
return answer;
}
};
class Solution
{
public:
Node* findIntersection(Node* head1, Node* head2)
{
Node* temp=new Node(-1);
Node* sum=temp;
while(head1 && head2)
{
if(head1->data==head2->data)
{
sum->next=new Node(head2->data);
sum=sum->next;
head1=head1->next;
head2=head2->next;
}else if(head1->data>head2->data){
head2=head2->next;
}else{
head1=head1->next;
}
}
return temp->next;
}
};
class Solution {
public:
int maxFrequency(vector& nums, int k) {
sort(nums.begin(), nums.end());
int l=nums.size()-1, h = l;
int sum = 0, res = 0;
while(h>=0){
sum += nums[l]-nums[h];
while(sum > k){
int x = nums[l]-nums[l-1];
l--;
sum -= x*(l-h+1);
}
res = max(res, (l-h+1));
h--;
}
return res;
}
};
class Solution
{
public:
Node* reverseDLL(Node * head)
{
if(head == NULL || head->next == NULL)
return head;
Node* ntemp = head->prev;
Node* ptemp = head;
while(head!=NULL){
ptemp = head;
head = head->next;
ptemp->next = ntemp;
ptemp->prev = head;
if(ntemp)
ntemp->prev = ptemp;
ntemp = ptemp;
}
return ptemp;
}
};
class Solution {
public:
int minPairSum(vector& nums) {
sort(nums.begin(), nums.end());
int n = nums.size(), ans=0;
for (int i = 0; i < n / 2; i++) {
if (i < n - i - 1) {
int a = nums[i] + nums[n - i - 1];
ans = max(ans, a);
}
}
return ans;
}
};
class Solution
{
pair solve(Node* root)
{
if(root -> left == NULL and root -> right == NULL)
return {root, root};
if(root -> left == NULL)
{
pair right = solve(root -> right);
root -> right = right.first;
right.first -> left = root;
return {root, right.second};
}
if(root -> right == NULL)
{
pair left = solve(root -> left);
left.second -> right = root;
root -> left = left.second;
return {left.first, root};
}
pair left = solve(root -> left);
pair right = solve(root -> right);
left.second -> right = root;
root -> left = left.second;
root -> right = right.first;
right.first -> left = root;
return {left.first, right.second};
}
public:
//Function to convert binary tree into circular doubly linked list.
Node *bTreeToCList(Node *root)
{
//add code here.
if(root == NULL)
return NULL;
pair res = solve(root);
res.first -> left = res.second;
res.second -> right = res.first;
return res.first;
}
};
class Solution {
public:
string findDifferentBinaryString(vector& nums) {
string ans="";
int n=nums[0].size();
int p=0;
for(auto i:nums)
{
if(i[p]=='0')
ans+="1";
else
ans+="0";
p++;
}
return ans;
}
};
class Solution
{
public:
void dfs(int k , string prev , unordered_set &seen , vector &edges){
for(int i=0 ; i seen;
string startingNode = string(n-1,'0');
vector edges;
dfs(k,startingNode,seen,edges);
string ret;
int l = pow(k,n);
for(int i=0 ; i< l ; i++){
ret+=(edges[i]+'0');
}
ret+=startingNode;
return ret;
}
};
