Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added More Solutions #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Binary-Search/Books Allocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#define ll long long
#include<bits/stdc++.h>
using namespace std;

// Book allocation problems
// Why binary search? Dont just use it !
// Brute force solution will be to check for each book
// note that books are to be allocated in continuous form else it would have been an n subset problem with minimum sum any approaches?
// Interviewer might can gove you a different problem which seems to be solved this way but actually could have been different
// STRATEGY->
// Naive solution
// Notice the function graph
// boom! binary search cna be a solution

bool is(ll x, vector<int> & book,int m){
ll cnt=1,curr=0;
for(int i=0;i<book.size();i++)
if(curr+book[i]<=x) curr+=book[i]; // note curr does not counts the number of books in a set!!
else cnt++,curr=book[i];
if(cnt<=m) return 1;
return 0;
}

int books(vector<int> &book, int m) {
int n=book.size();
if((m==0&&n!=0) || m>n) return -1;

ll lo=0,hi=0,res=0;
for(int i=0;i<n;i++){
lo=max(lo,1LL*book[i]);
hi+=book[i];
}

// using binary search
while(lo<=hi){
ll mid = (hi+lo)>>1;
if(is(mid,book,m)) res=mid,hi=mid-1;
else lo=mid+1;
}

return res;
}
43 changes: 43 additions & 0 deletions Binary-Search/Good Base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#define ll long long
#include<bits/stdc++.h>
using namespace std;
// still we not know when to use binary search!
string solve(string A) {
ll n = stoll(A);
ll res=n-1,temp,mul;
temp=(sqrt(4*n-3)-1)/2;

if(n!=3 && temp*temp+temp+1==n) res=temp;
temp=0;

for(int i=3;i<64;i++){ // at most 1e6+1e4+1e2...
ll j=1; temp=0;
// we can use binary search for this !!!!
while(temp<n){
j++;
temp=0; mul=1;
for(int k=0;k<=i;k++) temp+=mul,mul*=j;
}
if(temp==n) res=j;
}
return to_string(res);
}

/*
string Solution::solve(string n) {
long long num = stol(n);
for (int i = log(num ) / log(2); i >= 2; --i) {
long long left = 2, right = pow(num, 1.0 / (i-1)) + 1;
while (left < right) {
long long mid = left + (right - left) / 2, sum = 0;
for (int j = 0; j < i; ++j) {
sum = sum * mid + 1;
}
if (sum == num) return to_string(mid);
else if (sum < num) left = mid + 1;
else right = mid;
}
}
return to_string(num - 1);
}
*/
28 changes: 28 additions & 0 deletions Binary-Search/Matrix Median.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Given a rowWise sorted array find matrix median of the array
// Naive -> (N*M) log(N*M) Aux->space O(N*M)
// MEGRING SORTED ARRAYS-> (N*M) LOG(M) Aux->space O(N*M)
// BSearch-> (32*N*LOG(M)) Aux->space O(32)/ Dont say O(32) say constant
// in bsearch logn steps are there but each step is having O(1) space so even after the search extra space will be used only once
// but in Merging Sorted arrays recursive func is there so at any time space might be o(1)*logM steps

#include<bits/stdc++.h>
using namespace std;

int cnt(int x, vector<vector<int>> &A){
int cnt=0;
for(auto v:A) cnt+=(lower_bound(v.begin(),v.end(),x)-v.begin());
return cnt;
}

int findMedian(vector<vector<int> > &A) {
int hi=INT_MIN,lo=INT_MAX,res=INT_MIN,r=A.size(),c=A[0].size();

for(auto v:A) for(auto x:v) hi=max(hi,x), lo=min(lo,x);
while(lo<=hi){
int mid= (lo+hi)>>1;
if(cnt(mid,A)*2<r*c) res=mid,lo=mid+1;
else hi=mid-1;
}
if(res==INT_MIN) return A[0][0];
return res;
}
71 changes: 71 additions & 0 deletions Binary-Search/Simple Queries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define re cin>>
#define pr cout<<
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)


const int N=2e6+5;
const int mod=1e9+7;

#define ll unsigned long long
const int mod = 1e9+7;

ll count(int x){
ll res=1;
for(int i=1;i*i<=x;i++){
if(x%i==0){
res=(res*(i))%mod;
if(x==i*i) continue;
res=(res*(x/i))%mod;
}
}
return res%mod;
}

vector<int> solve(vector<int> &A, vector<int> &B) {
int n=A.size();
vector<ll> times(n+1),order(n);
stack<int> st;
for(int i=0;i<A.size();i++){
while(!st.empty() && A[st.top()]<A[i]){
int p=st.top(); st.pop();
int l=st.size()?st.top()+1:0;
int r=i-1;
times[p]=(p-l+1LL)*(r-p+1);
}
st.push(i);
}
while(!st.empty()){
int p=st.top(); st.pop();
int l=st.size()?st.top()+1:0;
times[p]=(p-l+1LL)*(n-1-p+1);
}

vector<pair<int,int>> pp;
for(int i=0;i<n;i++) pp.push_back(make_pair(count(A[i]),times[i]));
sort(pp.begin(),pp.end(),greater<pair<int,int>>());

ll sum=0;
for(int i=0;i<n;i++) order[i]=(sum+=pp[i].second);
for(int i=0;i<B.size();i++)
B[i] =pp[lower_bound(order.begin(),order.end(),B[i])-order.begin()].first;
return B;
}

int main(){

cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);


return 0;
}

23 changes: 23 additions & 0 deletions DynamicProgramming/Catalan Sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const int mod =1e9+7;
void EE(int aa, int bb, int& xx, int& yy){if(aa%bb == 0){xx=0,yy=1;return;}
EE(bb,aa%bb,xx,yy);int t = xx;xx = yy;yy = t - yy*(aa/bb);}
int mmi(int aa){int xx,yy;EE(aa,mod,xx,yy);xx = (xx+mod)%mod;return xx;}

int getCatalan(int A){
int res=1;
for(int i=2;i<=A;i++) res= (1LL*res*(A+i))%mod;
for(int i=2;i<=A;i++) res= (1LL*res*mmi(i))%mod;
return res;
}

int dpSol(int A){
vector<int>dp(A+1,0);
dp[1]=dp[0]=1;
for(int i=2;i<=A;i++) for(int j=0;j<i;j++) dp[i]=(dp[i]+1LL*dp[j]*dp[i-1-j])%mod;
return dp[A];
}
int chordCnt(int A) {
if(A==0) return 0;
return dpSol(A);
return getCatalan(A);
}
44 changes: 44 additions & 0 deletions DynamicProgramming/Count Ways in 3Nboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
#define ll int64_t
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define fastio cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);

using namespace std;
const ll mod =1e9+7;
const int N =2e5+1;


// for counting->
int main() {
int d[3]={1,2,3};
int s[3]={1,2,1};

cout<<"For diff->";
int same=0,diff=0;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
for(int k=1;k<=4;k++){
if(d[0]==i|| d[1]==j || d[2]==k) continue;
if(i==j || j==k) continue;
if(i==k) same++;
else diff++;
}
cout<<same<<" "<<diff;

cout<<"For Same->";
same=0,diff=0;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
for(int k=1;k<=4;k++){
if(s[0]==i || s[1]==j|| s[2]==k) continue;
if(i==j || j==k) continue;
if(i==k) same++;
else diff++;
}

cout<<same<<" "<<diff;
return 0;
}
31 changes: 31 additions & 0 deletions DynamicProgramming/Palindrome Partition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
vector<vector<bool>> isPad;
vector<int> dp;

void cr(string &A){
isPad=vector<vector<bool>>(A.size()+1,vector<bool>(A.size()+1));
dp=vector<int>(A.size()+1,-1);
}

void generatePads(string &A){
int n=A.size();
for(int i=0;i<n;i++) isPad[i][i]=1;
for(int i=0;i<n-1;i++) isPad[i][i+1]=A[i]==A[i+1];
for(int l=3;l<=n;l++) for(int i=0,j=l-1;j<n;i++,j++)
isPad[i][j]=(A[i]==A[j] && isPad[i+1][j-1]);
}

int solve(int i, string &s){
if(i==s.size()) return dp[i]=0;
if(dp[i]!=-1) return dp[i];
dp[i]=1e6;
for(int k=i;k<s.size();k++)
if(isPad[i][k]) dp[i]=min(dp[i],solve(k+1,s)+1);
return dp[i];
}

int Solution::minCut(string A) {
if(A.size()==0) return 0;
cr(A);
generatePads(A);
return solve(0,A)-1;
}
Loading