From ef0ad91b89a73ad7d69f35fbbfcd66c3169e877c Mon Sep 17 00:00:00 2001 From: skrishan07 Date: Fri, 2 Oct 2020 19:20:54 +0530 Subject: [PATCH 1/6] Adding Leetcode monthly Challenge --- .../October 2020/Combination Sum.cpp | 32 ++++++++++++++ .../October 2020/Number of Recent Calls.cpp | 44 +++++++++++++++++++ .../September 2020/Find the Difference.cpp | 24 ++++++++++ .../September 2020/Largest Number.cpp | 27 ++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 Leetcode/Monthly Challenge/October 2020/Combination Sum.cpp create mode 100644 Leetcode/Monthly Challenge/October 2020/Number of Recent Calls.cpp create mode 100644 Leetcode/Monthly Challenge/September 2020/Find the Difference.cpp create mode 100644 Leetcode/Monthly Challenge/September 2020/Largest Number.cpp diff --git a/Leetcode/Monthly Challenge/October 2020/Combination Sum.cpp b/Leetcode/Monthly Challenge/October 2020/Combination Sum.cpp new file mode 100644 index 0000000..c5bea2b --- /dev/null +++ b/Leetcode/Monthly Challenge/October 2020/Combination Sum.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + void find( vector& candidates, int target, vector>&ans, vectorv ,int start ) + { + if(target==0) + { + ans.push_back(v); + return; + } + for(int i=start;i=candidates[i]) + { + v.push_back(candidates[i]); + find(candidates,target-candidates[i],ans,v,i); + v.pop_back(); + } + else + { + break; + } + } + } + vector> combinationSum(vector& candidates, int target) + { + vector>ans; + sort(candidates.begin(),candidates.end()); + vectorv; + find(candidates,target,ans,v,0); + return ans; + } +}; \ No newline at end of file diff --git a/Leetcode/Monthly Challenge/October 2020/Number of Recent Calls.cpp b/Leetcode/Monthly Challenge/October 2020/Number of Recent Calls.cpp new file mode 100644 index 0000000..af58302 --- /dev/null +++ b/Leetcode/Monthly Challenge/October 2020/Number of Recent Calls.cpp @@ -0,0 +1,44 @@ +class RecentCounter { +public: + vectorv; + int c; + RecentCounter() { + c=0; + } + int find(vectorv, int t) + { + int mid=0,x=1,start=0,end=v.size()-1; + if(t-3000>=1) + { + x=t-3000; + } + while(start<=end) + { + mid=(start+end)/2; + if(v[mid]==x) + { + return mid; + } + else if(v[mid]>x) + { + end=mid-1; + if(end>=0 && v[end]b+a) + return true; + return false; + } + string largestNumber(vector& nums) + { + string x,ans=""; + int n=nums.size(); + if(n==0) + return ans; + sort(nums.begin(),nums.begin()+n,compare); + if(nums[0]==0) + return "0"; + for(int i=0;i Date: Fri, 2 Oct 2020 19:39:04 +0530 Subject: [PATCH 2/6] Adding Codechef September2020 Lunchtime --- .../SEPT2020-LUNCHTIME/GCD operations.cpp | 49 +++++++++++++++ Codechef/SEPT2020-LUNCHTIME/Root the Tree.cpp | 61 +++++++++++++++++++ Codechef/SEPT2020-LUNCHTIME/Watermelon.cpp | 30 +++++++++ 3 files changed, 140 insertions(+) create mode 100644 Codechef/SEPT2020-LUNCHTIME/GCD operations.cpp create mode 100644 Codechef/SEPT2020-LUNCHTIME/Root the Tree.cpp create mode 100644 Codechef/SEPT2020-LUNCHTIME/Watermelon.cpp diff --git a/Codechef/SEPT2020-LUNCHTIME/GCD operations.cpp b/Codechef/SEPT2020-LUNCHTIME/GCD operations.cpp new file mode 100644 index 0000000..f835149 --- /dev/null +++ b/Codechef/SEPT2020-LUNCHTIME/GCD operations.cpp @@ -0,0 +1,49 @@ +#include +#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; + vectora(n); + vectorb(n); + for(int i=0;i>b[i]; + } + for(int i=0;i +#define ll long long int +using namespace std; +void dfs(vectora[], int n, bool visited[], int &ans, bool root[]) +{ + visited[n]=true; + for(int i=0;i>tc; + while(tc--) + { + ans=0; + cin>>n; + vectora[n+1]; + bool visited[n+1]; + bool root[n+1]; + for(int i=1;i>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< +#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; + vectora(n); + for(int i=0;i>a[i]; + sum+=a[i]; + } + if(sum>=0) + { + cout<<"YES"< Date: Fri, 2 Oct 2020 20:08:53 +0530 Subject: [PATCH 3/6] Adding Leetcode Array Questions --- Leetcode/Array/Teemo Attacking.cpp | 24 +++++++++++++ Leetcode/Array/Valid Sudoku.cpp | 55 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Leetcode/Array/Teemo Attacking.cpp create mode 100644 Leetcode/Array/Valid Sudoku.cpp diff --git a/Leetcode/Array/Teemo Attacking.cpp b/Leetcode/Array/Teemo Attacking.cpp new file mode 100644 index 0000000..4f192b6 --- /dev/null +++ b/Leetcode/Array/Teemo Attacking.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int findPoisonedDuration(vector& timeSeries, int duration) + { + int ans=0; + if(timeSeries.size()==0) + { + return ans; + } + ans+=duration; + for(int i=1;i=duration) + { + ans+=duration; + } + else + { + ans+=timeSeries[i]-timeSeries[i-1]; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/Leetcode/Array/Valid Sudoku.cpp b/Leetcode/Array/Valid Sudoku.cpp new file mode 100644 index 0000000..c6910c9 --- /dev/null +++ b/Leetcode/Array/Valid Sudoku.cpp @@ -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>& 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>& 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; + } +}; \ No newline at end of file From be207c603a4daea5b16d1e60961a4d1bc926cfcf Mon Sep 17 00:00:00 2001 From: skrishan07 Date: Sat, 3 Oct 2020 01:05:47 +0530 Subject: [PATCH 4/6] Adding Hackerrank Questions --- .../BST Lowest Common Ancestor.cpp | 23 +++++++++++++++++++ .../Find Merge Point of Two Lists.cpp | 19 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Hackerrank + Geeks for Geeks + DS Courseware/BST Lowest Common Ancestor.cpp create mode 100644 Hackerrank + Geeks for Geeks + DS Courseware/Find Merge Point of Two Lists.cpp diff --git a/Hackerrank + Geeks for Geeks + DS Courseware/BST Lowest Common Ancestor.cpp b/Hackerrank + Geeks for Geeks + DS Courseware/BST Lowest Common Ancestor.cpp new file mode 100644 index 0000000..143361a --- /dev/null +++ b/Hackerrank + Geeks for Geeks + DS Courseware/BST Lowest Common Ancestor.cpp @@ -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(v1data && v2data) + { + temp=temp->left; + } + else if(v1>temp->data && v2>temp->data) + { + temp=temp->right; + } + else + { + return temp; + } + } +} \ No newline at end of file diff --git a/Hackerrank + Geeks for Geeks + DS Courseware/Find Merge Point of Two Lists.cpp b/Hackerrank + Geeks for Geeks + DS Courseware/Find Merge Point of Two Lists.cpp new file mode 100644 index 0000000..9f7746d --- /dev/null +++ b/Hackerrank + Geeks for Geeks + DS Courseware/Find Merge Point of Two Lists.cpp @@ -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; +} \ No newline at end of file From 0eab1c710e2f0c8d69ef4aabd356a2f8d0d42d12 Mon Sep 17 00:00:00 2001 From: skrishan07 Date: Sat, 3 Oct 2020 19:31:33 +0530 Subject: [PATCH 5/6] Added Leetcode Day 3 October Challenge --- .../October 2020/K-diff Pairs in an Array.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Leetcode/Monthly Challenge/October 2020/K-diff Pairs in an Array.cpp diff --git a/Leetcode/Monthly Challenge/October 2020/K-diff Pairs in an Array.cpp b/Leetcode/Monthly Challenge/October 2020/K-diff Pairs in an Array.cpp new file mode 100644 index 0000000..9d2f67c --- /dev/null +++ b/Leetcode/Monthly Challenge/October 2020/K-diff Pairs in an Array.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int findPairs(vector& nums, int k) + { + int ans=0; + unordered_mapm; + for(int i=0;i Date: Fri, 1 Oct 2021 23:31:08 +0530 Subject: [PATCH 6/6] Added Longest Common Subsequence.cpp --- Leetcode/DP/Longest Common Subsequence.cpp | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Leetcode/DP/Longest Common Subsequence.cpp diff --git a/Leetcode/DP/Longest Common Subsequence.cpp b/Leetcode/DP/Longest Common Subsequence.cpp new file mode 100644 index 0000000..3029af2 --- /dev/null +++ b/Leetcode/DP/Longest Common Subsequence.cpp @@ -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/ \ No newline at end of file