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 countBits(int n) {
vector ans(n+1, 0);
for(int i=1; i<=n; i++){
ans[i]=ans[i>>1]+(i&1);
}
return ans;
}
};
void printCorner(Node *root)
{
queue q;
q.push(root);
int first, second;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
Node *node = q.front();
q.pop();
Node *left = node->left;
if (left) {
q.push(left);
}
Node *right = node->right;
if (right) {
q.push(right);
}
if (i == 0) {
first = node->data;
}
if (i == size - 1) {
second = node->data;
}
}
if (size == 1) {
cout << first << " ";
} else {
cout << first << " " << second << " ";
}
}
}
class Solution {
public:
int minTaps(int n, vector& ranges) {
vector jumps(n+1, 0);
for (int i=0; icurFarthest)
return -1;
curFarthest = max(curFarthest, i + jumps[i]);
if (i == curEnd) {
count++;
curEnd = curFarthest;
}
}
return curFarthest >= n ? count : -1;
}
};
int height(Node *root){
if(!root)
return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight>rightHeight?leftHeight:rightHeight)+1;
}
int bf(Node *root){
if(!root)
return 0;
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return rightHeight-leftHeight;
}
Node *leftRotation(Node *x){
Node *y = x->right;
Node *T = y->left;
x->right = T;
y->left = x;
return y;
}
Node *rightRotation(Node *x){
Node *y = x->left;
Node *T = y->right;
x->left = T;
y->right = x;
return y;
}
int findMax(Node *head){
if(!head)
return -1;
while(head->left){
head = head->left;
}
return head->data;
}
Node* deleteNode(Node* root, int data)
{
//add code here,
if(!root)
return root;
if(root->dataright = deleteNode(root->right,data);
else if(root->data>data)
root->left = deleteNode(root->left,data);
else{
if(!root->left and !root->right){
Node *temp = root;
root = NULL;
delete(temp);
}else if(!root->right){
Node *temp = root;
root = root->left;
delete(temp);
}else if(!root->left){
Node *temp = root;
root = root->right;
delete(temp);
}else{
int maximum = findMax(root->right);
root->data = maximum;
root->right = deleteNode(root->right,maximum);
}
}
if(!root)
return root;
int bff = bf(root);
if(bff>1 and bf(root->right)>=0)
return leftRotation(root);
else if(bff<-1 and bf(root->left)<=0)
return rightRotation(root);
else if(bff>1 and bf(root->right)<0){
root->right = rightRotation(root->right);
return leftRotation(root);
}
else if(bff<-1 and bf(root->left)>0){
root->left = leftRotation(root->left);
return rightRotation(root);
}
return root;
}
class Solution {
public:
long long minimumReplacement(vector<int>& nums) {
long long p=0;
long long o =0;
for(int i = nums.size()-1 ; i>0 ; i--){
if(nums[i-1] > nums[i] ){
p = nums[i-1]/nums[i];
if(nums[i-1] % nums[i] !=0) p++;
nums[i-1] /= p;
o += (p-1);
}
}
return o;
}
};
Node* deleteNode(Node *head,int x)
{
if(head==NULL)
{
return head;
}
if(x==1)
{
return head->next;
}
Node* curr=head;
for(int i=1;inext;
}
curr->next=curr->next->next;
return head;
}
class Solution {
public:
int bestClosingTime(string s) {
int n=s.size();
int ind=-1,mxscore=0,score=0;
for(int i=0;imxscore){
mxscore=score;
ind=i;
}
}
return ind+1;
}
};
class Solution
{
public:
Node* reverse(Node* head){
Node* pre=NULL,*curr=head,*nxt=head->next;
while(nxt!=NULL){
curr->next=pre;
pre=curr;
curr=nxt;
nxt=nxt->next;
}
curr->next=pre;
return curr;
}
Node *compute(Node *head)
{
if(head==NULL||head->next==NULL) return head;
Node* curr=reverse(head);
int cnt=curr->data;
head=curr;
while(curr!=NULL){
Node* p1=curr->next;
while(p1!=NULL &&cnt>p1->data){
p1=p1->next;
}
curr->next=p1;
curr=p1;
if(p1!=NULL) cnt=p1->data;
}
head=reverse(head);
return head;
}
};
class MyStack {
public:
queue q;
MyStack()
{
}
void push(int x)
{
q.push(x);
for(int i=0;i
Node *removeDuplicates(Node *head)
{
if(head== NULL)
{
return NULL;
}
Node* curr=head;
while(curr->next!= NULL)
{
if(curr->data==curr->next->data)
{
curr->next=curr->next->next;
}else{
curr=curr->next;
}
}
return head;
}
