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 Longest Common Subsequence.cpp #28

Open
wants to merge 6 commits 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
49 changes: 49 additions & 0 deletions Codechef/SEPT2020-LUNCHTIME/GCD operations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc,n,flag,v;
cin>>tc;
while(tc--)
{
flag=1;
cin>>n;
vector<int>a(n);
vector<int>b(n);
for(int i=0;i<n;i++)
{
a[i]=i+1;
cin>>b[i];
}
for(int i=0;i<n;i++)
{
if(a[i]!=b[i] && b[i]!=1)
{
v=gcd(a[i],b[i]);
if(v!=b[i])
{
flag=0;
break;
}
}
}
if(flag==0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}
61 changes: 61 additions & 0 deletions Codechef/SEPT2020-LUNCHTIME/Root the Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
void dfs(vector<int>a[], int n, bool visited[], int &ans, bool root[])
{
visited[n]=true;
for(int i=0;i<a[n].size();i++)
{
if(!visited[a[n][i]])
{
dfs(a,a[n][i],visited,ans,root);
}
else
{
if(root[ a[n][i] ]==true)
{
root[a[n][i]]=false;
}
else
{
ans++;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc,n,x,y,ans;
cin>>tc;
while(tc--)
{
ans=0;
cin>>n;
vector<int>a[n+1];
bool visited[n+1];
bool root[n+1];
for(int i=1;i<n;i++)
{
visited[i]=false;
cin>>x>>y;
a[x].push_back(y);
}
for(int i=0;i<=n;i++)
{
visited[i]=false;
root[i]=false;
}
for(int i=1;i<=n;i++)
{
if(visited[i]==false)
{
root[i]=true;
dfs(a,i,visited,ans,root);
}
}
cout<<ans<<endl;
}
return 0;
}
30 changes: 30 additions & 0 deletions Codechef/SEPT2020-LUNCHTIME/Watermelon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc,n,sum,x;
cin>>tc;
while(tc--)
{
sum=0;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
if(sum>=0)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Node *lca(Node *root, int v1,int v2)
{
Node *temp=root;
while(1)
{
if(v1==temp->data || v2==temp->data)
{
return temp;
}
else if(v1<temp->data && v2<temp->data)
{
temp=temp->left;
}
else if(v1>temp->data && v2>temp->data)
{
temp=temp->right;
}
else
{
return temp;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)
{
SinglyLinkedListNode* tmp1=head1;
SinglyLinkedListNode* tmp2=head2;
while(tmp1!=tmp2)
{
tmp1=tmp1->next;
tmp2=tmp2->next;
if(tmp1==NULL)
{
tmp1=head2;
}
if(tmp2==NULL)
{
tmp2=head1;
}
}
return tmp1->data;
}
24 changes: 24 additions & 0 deletions Leetcode/Array/Teemo Attacking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration)
{
int ans=0;
if(timeSeries.size()==0)
{
return ans;
}
ans+=duration;
for(int i=1;i<timeSeries.size();i++)
{
if(timeSeries[i]-timeSeries[i-1]>=duration)
{
ans+=duration;
}
else
{
ans+=timeSeries[i]-timeSeries[i-1];
}
}
return ans;
}
};
55 changes: 55 additions & 0 deletions Leetcode/Array/Valid Sudoku.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Solution {
public:
int start(int n) //return starting of sub-boxes
{
if(n>=0 && n<=2)
return 0;
else if(n>=3 && n<=5)
return 3;
else
return 6;
}
bool check(vector<vector<char>>& board, int x, int y) //check whether board[x][y] is valid or not
{
for(int i=0;i<9;i++) //Check whether column contain repetition or not
{
if(i!=x && board[i][y]==board[x][y])
{
return false;
}
}
for(int j=0;j<9;j++) //Check whether row contain repetition or not
{
if(j!=y && board[x][j]==board[x][y])
{
return false;
}
}
int x1=start(x),y1=start(y);
for(int i=x1;i<x1+3;i++) //Check whether subbox contain repetition or not
{
for(int j=y1;j<y1+3;j++)
{
if(i!=x && j!=y && board[i][j]==board[x][y])
{
return false;
}
}
}
return true;
}
bool isValidSudoku(vector<vector<char>>& board)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j]!='.' && check(board,i,j)==false)
{
return false;
}
}
}
return true;
}
};
24 changes: 24 additions & 0 deletions Leetcode/DP/Longest Common Subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int n1 = text1.size();
int n2 = text2.size();
int dp[n1+1][n2+1];
for(int i = 0; i <= n1; i++){
for(int j = 0; j <= n2; j++){
if(i == 0 || j == 0){
dp[i][j] = 0;
}
else if(text1[i-1] == text2[j-1]){
dp[i][j] = 1 + dp[i-1][j-1];
}
else{
dp[i][j] = max({ dp[i-1][j-1], dp[i][j-1], dp[i-1][j] });
}
}
}
return dp[n1][n2];
}
};

// https://leetcode.com/problems/longest-common-subsequence/
32 changes: 32 additions & 0 deletions Leetcode/Monthly Challenge/October 2020/Combination Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
void find( vector<int>& candidates, int target, vector<vector<int>>&ans, vector<int>v ,int start )
{
if(target==0)
{
ans.push_back(v);
return;
}
for(int i=start;i<candidates.size();i++)
{
if(target>=candidates[i])
{
v.push_back(candidates[i]);
find(candidates,target-candidates[i],ans,v,i);
v.pop_back();
}
else
{
break;
}
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
vector<vector<int>>ans;
sort(candidates.begin(),candidates.end());
vector<int>v;
find(candidates,target,ans,v,0);
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
int findPairs(vector<int>& nums, int k)
{
int ans=0;
unordered_map<int,int>m;
for(int i=0;i<nums.size();i++)
{
if(m[nums[i]]) //if nums[i] is present
{
if(k==0)
{
if(m[nums[i]]==1)
{
ans++; //consider a=b when k=0
m[nums[i]]++;
}
}
continue;
}
if(m[k+nums[i]]) //if we consider nums[i]=a
{
ans++;
}
if(m[nums[i]-k]) //if we consider nums[i]=b
{
ans++;
}
m[nums[i]]=1;
}
return ans;
}
};
Loading