diff --git a/Miscellaneous/Binary_Search/Anjali_Singh.cpp b/Miscellaneous/Binary_Search/Anjali_Singh.cpp new file mode 100644 index 0000000..860743e --- /dev/null +++ b/Miscellaneous/Binary_Search/Anjali_Singh.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int BinarySearch(vectornums, int l, int r, int target){ + if(r>=l){ + int mid = l + (r-l)/2; + if(nums[mid]==target) return mid; + else if(nums[mid]>target) return BinarySearch(nums, l, mid-1, target); + else return BinarySearch(nums, mid+1, r, target); + } + return -1; + } + int search(vector& nums, int target) { + int x = BinarySearch(nums, 0, nums.size()-1, target); + return x; + } +}; diff --git a/Miscellaneous/Duplicate_Number/Anjali_Singh.cpp b/Miscellaneous/Duplicate_Number/Anjali_Singh.cpp new file mode 100644 index 0000000..f5609dd --- /dev/null +++ b/Miscellaneous/Duplicate_Number/Anjali_Singh.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int findDuplicate(vector& nums) { + sort(nums.begin(), nums.end()); + for(int i=0; inums, int l, int r, int target){ + if(r>=l){ + int mid = l + (r-l)/2; + if(nums[mid]==target) return mid; + else if(nums[mid]>target) return BinarySearch(nums, l, mid-1, target); + else return BinarySearch(nums, mid+1, r, target); + } + return -1; + } + int search(vector& nums, int target) { + int x = BinarySearch(nums, 0, nums.size()-1, target); + return x; + } +}; diff --git a/Stacks/BackSpace_String_Compare/Anjali_Singh.cpp b/Stacks/BackSpace_String_Compare/Anjali_Singh.cpp new file mode 100644 index 0000000..980c425 --- /dev/null +++ b/Stacks/BackSpace_String_Compare/Anjali_Singh.cpp @@ -0,0 +1,50 @@ +class Solution { +public: + bool isEqual(stack &stack1, stack &stack2) { + if (stack1.empty() && stack2.empty()) + return true; + else if (stack1.empty() || stack2.empty()) + return false; + while (!stack1.empty() && !stack2.empty()) + { + if (stack1.top() != stack2.top()) + return false; + stack1.pop(); + stack2.pop(); + } + if (stack1.empty() && stack2.empty()) + return true; + else + return false; + } + + bool backspaceCompare(string S, string T) { + stack stack1; + stack stack2; + for(char s:S){ + if(s=='#' && !stack1.empty()){ + // cout< stk; + string ans; + for(char i: s){ + if(!stk.empty() && (i==stk.top()-32||i==stk.top()+32)){ + cout< nextGreaterElement(vector& nums1, vector& nums2) { + + stack st; + unordered_map hashmap; + for(int i=nums2.size()-1;i>=0;i--) + { + while(!st.empty()&&st.top() stk; + string ans; + for(char s: S){ + if(!stk.empty() && s==stk.top()) + { + stk.pop(); + } + else{ + stk.push(s); + } + } + while(!stk.empty()){ + ans.push_back(stk.top()); + cout< st; + string ans; + + + + for(char& c : S) + { + //for opening brackets + // if it is not the outer most bracket then we will take it into ans string and push it to stack for later comparison. + if (c == '(' && st.size()>=1) + { + ans.push_back('('); + st.push('('); + } + // if c is outer most bracket then push it into stack for later comparison + else + { + if (c == '(' && st.size() == 0) + { + st.push('('); + } + } + + + //for closing brackets + + if (c == ')' && st.size()>1) + { + ans.push_back(')'); + st.pop(); + } + else + { + if (c == ')' && st.size() == 1) + { + st.pop(); + } + } + } + + + return ans; + + } +}; diff --git a/Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp b/Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp new file mode 100644 index 0000000..f64a75b --- /dev/null +++ b/Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int evalRPN(vector& tokens) { + unordered_set operands = {"+", "-", "*", "/"}; + stack s; + for (string token : tokens) { + // We have a number + if (operands.find(token) == operands.end()) { + s.push(stoi(token)); + } + else { // We have an operand + int b = s.top(); + s.pop(); + int a = s.top(); + s.pop(); + if (token == "+") { + s.push(a + b); + } + else if (token == "-") { + s.push(a - b); + } + else if (token == "*") { + s.push(a * b); + } + else if (token == "/") { + s.push(a / b); + } + } + } + return s.top(); + } +}; diff --git a/Stacks/Valid_Parentheses/Anjali_Singh.cpp b/Stacks/Valid_Parentheses/Anjali_Singh.cpp new file mode 100644 index 0000000..68f80ee --- /dev/null +++ b/Stacks/Valid_Parentheses/Anjali_Singh.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isValid(string s) { + stack stk; + for (char c : s) { + if (c == '(' || c == '{' || c == '[') { + stk.push(c); + continue; + } + // now we've all closing brackets + if (stk.empty()) return false; + + if (c == ')' && stk.top() != '(') return false; + if (c == '}' && stk.top() != '{') return false; + if (c == ']' && stk.top() != '[') return false; + stk.pop(); + } + return stk.empty(); + } +}; diff --git a/Week1/Count Negative Nos/Anjali_Singh.cpp b/Week1/Count Negative Nos/Anjali_Singh.cpp new file mode 100644 index 0000000..a70ad65 --- /dev/null +++ b/Week1/Count Negative Nos/Anjali_Singh.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int countNegatives(vector>& grid) { + int r=grid.size(), c=grid[0].size(); + int count=0; + for(int i=0; imymap; + for(int i=0; i=left){ + left=mymap[s[i]]+1; + mymap[s[i]]=i; + + } + else{ + mymap[s[i]]=i; + } + } + res=max(res,i-left+1); + } + return res; + } +}; diff --git a/Week1/Max Subarray/Anjali_Singh.cpp b/Week1/Max Subarray/Anjali_Singh.cpp new file mode 100644 index 0000000..12c679b --- /dev/null +++ b/Week1/Max Subarray/Anjali_Singh.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + int sum=0, maxSum=INT_MIN; + for(int i=0; imaxSum){ + maxSum=sum; + } + if(sum<0){ + sum=0; + } + } + return maxSum; + + } +}; diff --git a/Week1/Missing no/Anjali_Singh.cpp b/Week1/Missing no/Anjali_Singh.cpp new file mode 100644 index 0000000..1431e5c --- /dev/null +++ b/Week1/Missing no/Anjali_Singh.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int missingNumber(vector& nums) { + sort(nums.begin(),nums.end()); + cout<& A) { + int len = A.size(); + int count1 = 1; + int count2 = 1; + for(int i = 1; i < len; i++){ + if(A[i] >= A[i-1]){ + count1++; + } + if(A[i] <= A[i-1]){ + count2++; + } + } + if(count1 == len or count2 == len){ + return true; + } + return false; + } +}; diff --git a/Week1/Move Zeroes/Anjali_Singh.cpp b/Week1/Move Zeroes/Anjali_Singh.cpp new file mode 100644 index 0000000..6440564 --- /dev/null +++ b/Week1/Move Zeroes/Anjali_Singh.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + void moveZeroes(vector& nums) { + for(int i=0, j=0; i> generate(int numRows) { + vector>ans; + for(int i=0;irow(i+1,1); + for(int j=1;j& nums) { + int leftSum=0, rightSum=0, sum=0; + for(int i=0; i productExceptSelf(vector& nums) { + vector left(nums.size()), right(nums.size()); + left[0] = 1; + right[nums.size()-1] = 1; + for(int i=1; i=0; i--){ + right[i] = right[i+1]*nums[i+1]; + } + for(int i=0; i& nums) { + int n = nums.size(); + if(n==0||n==1){ + return n; + } + int temp[n]; + int j = 0; + for (int i=0; i& nums, int k) { + // while(k!=0){ + // int j = nums[nums.size()-1]; + // for(int i=nums.size()-1; i>0; i--){ + // nums[i] = nums[i-1]; + // } + // nums[0] = j; + // k--; + // } + int n=nums.size(); + int a[n]; + for(int i=0; i spiralOrder(vector>& matrix) { + int r=matrix.size(), c=matrix[0].size(); + vector ans; + int k=0, l=0; + while(k=l; i--){ + ans.push_back(matrix[r-1][i]); + } + r--; + } + if(l=k; i--){ + ans.push_back(matrix[i][l]); + } + l++; + } + } + + return ans; + } +}; diff --git a/Week1/Target Array/Anjali_Singh.cpp b/Week1/Target Array/Anjali_Singh.cpp new file mode 100644 index 0000000..835ce56 --- /dev/null +++ b/Week1/Target Array/Anjali_Singh.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + vector createTargetArray(vector& nums, vector& index) { + vectorans; + for(int i=0; i x; + int a=start, s; + for(int i=0; ivalval){ + ///Node* nh=hd1; + hd1->next=MergeList(hd1->next,hd2); + return hd1; + } else { + hd2->next=MergeList(hd1,hd2->next); + return hd2; + } + } + + ListNode* MidSplit(ListNode* hd){ + if (hd==NULL||hd->next==NULL) return hd; + ListNode* fp=hd, *sp=hd; + while (fp->next!=NULL&&fp->next->next!=NULL){ + sp=sp->next; + fp=fp->next->next; + } + ListNode* nh=sp->next; + sp->next=NULL; + return nh; + } + ListNode* sortList(ListNode* head) { + if (head==NULL||head->next==NULL) return head; + ListNode* nh=MidSplit(head); + head=sortList(head); + nh=sortList(nh); + return MergeList(head, nh); + + } +}; diff --git a/Week2/Intersection_of_2_LL/Anjali_Singh.cpp b/Week2/Intersection_of_2_LL/Anjali_Singh.cpp new file mode 100644 index 0000000..0fc5934 --- /dev/null +++ b/Week2/Intersection_of_2_LL/Anjali_Singh.cpp @@ -0,0 +1,59 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + if(headA==NULL||headB==NULL) + return NULL; + if(headB==headA) + return headA; + ListNode *tailA = headA; + ListNode *tailB = headB; + ListNode *tailsA = headA; + ListNode *tailsB = headB; + ListNode *x = NULL; + int sizeA=0, sizeB=0; + for(int i = 0; tailsA!=NULL; i++) + { + sizeA = i; + tailsA = tailsA->next; + } + for(int i = 0; tailsB!=NULL; i++) + { + sizeB = i; + tailsB = tailsB->next; + } + + sizeA++; + sizeB++; + while(tailA!=NULL&&tailB!=NULL) + { + if(sizeA>sizeB){ + x = tailA; + while(x!=NULL){ + if(tailB==x) + return x; + x = x->next; + } + } + else{ + x = tailB; + while(x!=NULL){ + if(tailA==x) + return x; + x = x->next; + } + } + tailB = tailB->next; + tailA = tailA->next; + + } + return NULL; + } +}; diff --git a/Week2/Linked_List_Cycle/Anjali_Singh.cpp b/Week2/Linked_List_Cycle/Anjali_Singh.cpp new file mode 100644 index 0000000..7e19674 --- /dev/null +++ b/Week2/Linked_List_Cycle/Anjali_Singh.cpp @@ -0,0 +1,33 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) { + ListNode* sp=head,*fp=head; + if(head==NULL || head->next==NULL){ + return false; + } + while (true){ + if (fp==NULL || fp->next==NULL){ + return false; + } + sp=sp->next; + fp=fp->next->next; + if (sp==fp) break; + } + // sp=head; + // while (true){ + // sp=sp->next; + // fp=fp->next; + // if (sp==fp) break; + // } + return true; + + } +}; diff --git a/Week2/Merge_2_Sorted_List/Anjali_Singh.cpp b/Week2/Merge_2_Sorted_List/Anjali_Singh.cpp new file mode 100644 index 0000000..aaa9030 --- /dev/null +++ b/Week2/Merge_2_Sorted_List/Anjali_Singh.cpp @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + if (l1==NULL && l2==NULL) return NULL; + if (l1==NULL) return l2; + if (l2==NULL) return l1; + if (l1->valval){ + ///Node* nh=hd1; + l1->next=mergeTwoLists(l1->next,l2); + return l1; + } else { + l2->next=mergeTwoLists(l1,l2->next); + return l2; + } + } +}; diff --git a/Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp b/Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp new file mode 100644 index 0000000..fab2ba8 --- /dev/null +++ b/Week2/Middle_of_the_LinkedList/Anjali_Singh.cpp @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + ListNode *fp = head; + ListNode *sp = head; + while(fp!=NULL && fp->next!=NULL){ + sp = sp->next; + fp= fp->next->next; + } + return sp; + } +}; diff --git a/Week2/Odd_Even_LL/Anjali_Singh.cpp b/Week2/Odd_Even_LL/Anjali_Singh.cpp new file mode 100644 index 0000000..9bdec07 --- /dev/null +++ b/Week2/Odd_Even_LL/Anjali_Singh.cpp @@ -0,0 +1,43 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* oddEvenList(ListNode* head) { + if(head==NULL) + return NULL; + ListNode* odd = head; + ListNode* even = head->next; + ListNode *evenFirst = even; + + while(true){ + if (!odd || !even || !(even->next)) + { + odd->next = evenFirst; + break; + } + odd->next = even->next; //even's next position will be obivously odd and thus will be become next position + odd = even->next; // moving odd pointer to next odd position + if(odd->next==NULL){ + even->next = NULL; + odd->next = evenFirst; // connecting odd and even LL together + break; + + } + even->next = odd->next; + even = odd->next; + + } + + return head; + } + + +}; diff --git a/Week2/Palindrome_LinkedList/Anjali_Singh.cpp b/Week2/Palindrome_LinkedList/Anjali_Singh.cpp new file mode 100644 index 0000000..a9537b3 --- /dev/null +++ b/Week2/Palindrome_LinkedList/Anjali_Singh.cpp @@ -0,0 +1,42 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + + bool isPalindrome(ListNode* head) { + if(head==NULL || head->next==NULL) return true; + ListNode *temp = reverseList(head); + ListNode *tempA = head; + while(tempA!=NULL && temp!=NULL){ + if(temp->val != tempA->val){ + return false; + } + cout<next->val<<" "; + cout<next->val<<" "; + temp = temp->next; + tempA = tempA->next; + } + return true; + } + + ListNode* reverseList(ListNode* head) { + if(head==NULL) + return NULL; + if(head->next==NULL) + return head; + ListNode *ans = reverseList(head->next); + head->next->next=head; + head->next=NULL; + return ans; + + } + +}; diff --git a/Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp b/Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp new file mode 100644 index 0000000..c7c92f4 --- /dev/null +++ b/Week2/Remove_Duplicate_from_Sorted_List/Anjali_Singh.cpp @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + ListNode* temp=head; + while(temp!=NULL&&temp->next!=NULL){ + if(temp->val == temp->next->val){ + temp->next = temp->next->next; + } + else{ + temp = temp->next; + } + } + return head; + } +}; diff --git a/Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp b/Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp new file mode 100644 index 0000000..8830995 --- /dev/null +++ b/Week2/Remove_Nth_end_of_List/Anjali_Singh.cpp @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* first = head; + ListNode* second = head; + while(second->next!=NULL){ + if(n>0){ + second = second->next; + n--; + } + else{ + first = first->next; + second = second->next; + } + } + if(n==0){ + first->next = first->next->next; + } + else{ + head=head->next; + } + + return head; + + } +}; diff --git a/Week2/Reverse_Linked_List/Anjali_Singh.cpp b/Week2/Reverse_Linked_List/Anjali_Singh.cpp new file mode 100644 index 0000000..b094ee7 --- /dev/null +++ b/Week2/Reverse_Linked_List/Anjali_Singh.cpp @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + if(head==NULL) + return NULL; + if(head->next==NULL) + return head; + ListNode *ans = reverseList(head->next); + head->next->next=head; + head->next=NULL; + return ans; + + } +}; diff --git a/Week2/Sort_List/Anjali_Singh.cpp b/Week2/Sort_List/Anjali_Singh.cpp new file mode 100644 index 0000000..a435a55 --- /dev/null +++ b/Week2/Sort_List/Anjali_Singh.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* MergeList(ListNode* hd1, ListNode* hd2){ + if (hd1==NULL && hd2==NULL) return NULL; + if (hd1==NULL) return hd2; + if (hd2==NULL) return hd1; + if (hd1->valval){ + ///Node* nh=hd1; + hd1->next=MergeList(hd1->next,hd2); + return hd1; + } else { + hd2->next=MergeList(hd1,hd2->next); + return hd2; + } + } + + ListNode* MidSplit(ListNode* hd){ + if (hd==NULL||hd->next==NULL) return hd; + ListNode* fp=hd, *sp=hd; + while (fp->next!=NULL&&fp->next->next!=NULL){ + sp=sp->next; + fp=fp->next->next; + } + ListNode* nh=sp->next; + sp->next=NULL; + return nh; + } + ListNode* sortList(ListNode* head) { + if (head==NULL||head->next==NULL) return head; + ListNode* nh=MidSplit(head); + head=sortList(head); + nh=sortList(nh); + return MergeList(head, nh); + + } +};