Instead of using O(2N). I have solved it in O(N), here is my approach class Solution { public: int pairsum(vector<int> &arr) { // code here int greater1=arr[0]; int greater2=arr[1]; int n=arr.size(); for(int i=2;i<n;i++){ int smaller=(greater1<greater2)?greater1:greater2; int other=greater1+greater2-smaller; if(arr[i]>smaller){ smaller=arr[i]; } if(greater1<greater2){ greater1=smaller; } else if(greater1>greater2){ greater2=smaller; } } return greater1+greater2; } }; BTW: thanks for making consistent videos, it really helps me keep going bro 😀
@codetips-byRochak
Күн бұрын
Yes, so instead of using two for loops, you have written multiple if else condition inside one for loop. So yeah that's great, and thanks for your kind words.😀
@codetips-byRochak
Күн бұрын
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code :- class Solution { public: int pairsum(vector<int> &arr) { int n=arr.size(); int maxi=INT_MIN; for(int i=0;i<n;i++){ if(arr[i]>maxi){ maxi=arr[i]; } } int secmaxi=INT_MIN; for(int i=0;i<n;i++){ if(arr[i]>secmaxi&&arr[i]<maxi){ secmaxi=arr[i]; } } return maxi+secmaxi; } };
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code :- class Solution { public: void dfs(vector<vector<char>>&grid,vector<vector<int>>&vis,int i,int j,int n, int m){ if(i>=n||j>=m||i<0||j<0||grid[i][j]=='0'||vis[i][j]==1){ return; } vis[i][j]=1; dfs(grid,vis,i+1,j,n,m); dfs(grid,vis,i-1,j,n,m); dfs(grid,vis,i,j+1,n,m); dfs(grid,vis,i,j-1,n,m); dfs(grid,vis,i+1,j+1,n,m); dfs(grid,vis,i-1,j-1,n,m); dfs(grid,vis,i+1,j-1,n,m); dfs(grid,vis,i-1,j+1,n,m); } int numIslands(vector<vector<char>>& grid) { int n=grid.size(); int m=grid[0].size(); int ans=0; vector<vector<int>>vis(n,vector<int>(m,0)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(grid[i][j]=='1'&&!vis[i][j]){ dfs(grid,vis,i,j,n,m); ans++; } } } return ans; } };
@sudhanshukumarjha8534
4 күн бұрын
but how 1,3 is a subset. the concept of subset is that , all the elements need to be consecutive
@codetips-byRochak
4 күн бұрын
Thing which you are talking about is known as subarray.
@codetips-byRochak
4 күн бұрын
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code :- class Solution { public: long long findSmallest(vector<int> &arr) { long long ans=1; int n=arr.size(); for(int i=0;i<n;i++){ if(arr[i]>ans){ return ans; } else{ ans=ans+arr[i]; } } return ans; } };
@rashidixit1168
4 күн бұрын
long long findSmallest(vector<int> &arr) { long long res=1; for(int i=0;i<arr.size();i++){ if(res<arr[i]) break; res=res+arr[i]; } return res; }
@codetips-byRochak
5 күн бұрын
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code : class Solution { public: // Function to reverse a circular linked list Node* reverse(Node* head) { if(head==NULL||head->next==head){ return head; } Node* prev=NULL; Node* curr=head; Node* next; do{ next=curr->next; curr->next=prev; prev=curr; curr=next; }while(curr!=head); head->next=prev; head=prev; return head; } // Function to delete a node from the circular linked list Node* deleteNode(Node* head, int key) { if(head==NULL){ return NULL; } if(head->data==key){ if(head->next==head){ delete head; return NULL; } Node* last=head; while(last->next!=head){ last=last->next; } last->next=head->next; Node* temp=head; head=head->next; delete temp; return head; } Node* prev; Node* curr=head; while(curr->next!=head){ if(curr->data==key){ prev->next=curr->next; delete curr; return head; } prev=curr; curr=curr->next; } return head; } };
@codetips-byRochak
6 күн бұрын
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code : class Solution { public: // Function to find the majority elements in the array vector<int> findMajority(vector<int>& nums) { int candidate1=nums[0]; int candidate2=nums[1]; int vote1=0; int vote2=0; int n=nums.size(); vector<int>ans; for(int i=0;i<n;i++){ if(nums[i]==candidate1){ vote1++; } else if(nums[i]==candidate2){ vote2++; } else if(vote1==0){ candidate1=nums[i]; } else if(vote2==0){ candidate2=nums[i]; } else{ vote1--; vote2--; } } vote1=0; vote2=0; int flag=0; for(int i=0;i<n;i++){ if(nums[i]==candidate1){ vote1++; } else if(nums[i]==candidate2){ vote2++; } } if(vote1>n/3){ ans.push_back(candidate1); flag=1; } if(vote2>n/3){ ans.push_back(candidate2); flag=1; } if(!flag){ return {-1}; } return ans; } };
@codetips-byRochak
Ай бұрын
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code : class Solution { public: long long maxSubarraySum(vector<int> &arr) { int n=arr.size(); long long sum=0; long long maxi=INT_MIN; for(int i=0;i<n;i++){ sum+=arr[i]; maxi=max(maxi,sum); if(sum<0){ sum=0; } } return maxi; } };
@jayrathod9271
Ай бұрын
return int(n/2) +1
@codetips-byRochak
Ай бұрын
Yeah this can also work
@vijayanks1714
Ай бұрын
your solution ok but for longer run learn the dp variation that is the take from the problem
@codetips-byRochak
Ай бұрын
Let's Connect 🤝 : LinkedIn : www.linkedin.com/in/rochak-vyas17/ Code : class Solution{ public: int nthStair(int n){ int dp[n+1]; dp[1]=1; dp[2]=2; for(int i=3;i<=n;i++){ dp[i]=dp[i-2]+1; } return dp[n]; } };
Bhai solution github pe dalke uska link share kr diya kr,baaki sb thik hai. sahi ja raha hai tu👍
@AmarjeetKumar-to9ub
Ай бұрын
TY :)
@codetips-byRochak
Ай бұрын
Glad you liked it
@newglobal7271
Ай бұрын
Great Explanation Bhaiya , Love You 🧡🧡🧡🧡 Bhaiya i think there will be no need of visited vector as we are using priority_queue ...
@codetips-byRochak
Ай бұрын
Glad that you liked it. And yeah we do need visited array because when we are going to our neighboring elements it can happen that we come across same element as neighbor with some other element.
@newglobal7271
Ай бұрын
@@codetips-byRochak see this once Bhaiya , I don't use vis , and it works i think it will work bcz until we are not finding the less value of dist array then we will not update pq , and if we are not updating pq then it will empty when all element are visited.. pls conform this 🙏🙏... class Solution{ public: int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1}; int minimumCostPath(vector<vector<int>>& grid){ int n=grid.size(); priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq; vector<vector<int>>dist(n,vector<int>(n,1e9)); dist[0][0]=grid[0][0]; pq.push({grid[0][0],{0,0}}); while(!pq.empty()){ int cost=pq.top().first; int xCo=pq.top().second.first; int yCo=pq.top().second.second; pq.pop(); for(int i=0;i<4;i++){ int newX=xCo+dx[i]; int newY=yCo+dy[i]; if(newX>=0&&newX<n&&newY>=0&&newY<n){ if(dist[newX][newY]>grid[newX][newY]+cost){ dist[newX][newY]=grid[newX][newY]+cost; pq.push({dist[newX][newY],{newX,newY}}); } } } } return dist[n-1][n-1]; } };
@codetips-byRochak
Ай бұрын
Yes bro it does work, but what I mean to say is it's safer to use visited array (although it does not require in this question), just as a template to solve these kind of question. It doesn't require in this question but it might require in some other variation for these kind of problems.
@newglobal7271
Ай бұрын
@@codetips-byRochak got it 🧡🧡🧡🧡☺☺☺☺
@newglobal7271
Ай бұрын
@@codetips-byRochak ook Got It
@mathy642
Ай бұрын
why not DP?
@codetips-byRochak
Ай бұрын
Dp will increase the complexity by reaching out to each and every state( because just imagine, if we are at cell x,y and we go to top, and again through some other path we again come across x,y with some lower path sum, so we are going to same node again and again, so here we are not restricted with one forward direction, we are also considering backward direction, so it can keep on going to find minimum sum, by going forward direction, backward direction,forward direction again, and so on....), but Dijkstra will stop when we encounter bottom right cell, because that will be minimum sum because we are keeping min heap. Hope you understood.
I have pinned the code in comment. You can check it out
@ABHISHEKKUMAR-wo2sg
Ай бұрын
Code is giving tle now
@codetips-byRochak
Ай бұрын
No bro, it is still getting all 1115 test cases passed. I think you might not have popped the element out of the queue. That might be one reason for tle.
@codetips-byRochak
Ай бұрын
I have pinned the code in comment. You can check it out. It is getting accepted.
@ABHISHEKKUMAR-wo2sg
Ай бұрын
@@codetips-byRochakSorry, nothing was wrong in the code, the same code now getting accepted
@codetips-byRochak
Ай бұрын
There might be something wrong, otherwise it can't happen. But yeah, happy to hear that it got accepted
Пікірлер