From a3e9dffbb34fbedaa7801792302dbaff9cb3ca23 Mon Sep 17 00:00:00 2001 From: Akash Gautam <76935019+geekblower@users.noreply.github.com> Date: Sun, 9 Oct 2022 23:01:51 +0530 Subject: [PATCH 1/2] Add files via upload --- Easy/1-two-sum.cpp | 17 ++++ Easy/2-add-two-numbers.cpp | 82 +++++++++++++++++++ ...26-remove-duplicates-from-sorted-array.cpp | 13 +++ Easy/29-divide-two-integers.cpp | 11 +++ Easy/50-powx-n.cpp | 29 +++++++ 5 files changed, 152 insertions(+) create mode 100644 Easy/1-two-sum.cpp create mode 100644 Easy/2-add-two-numbers.cpp create mode 100644 Easy/26-remove-duplicates-from-sorted-array.cpp create mode 100644 Easy/29-divide-two-integers.cpp create mode 100644 Easy/50-powx-n.cpp diff --git a/Easy/1-two-sum.cpp b/Easy/1-two-sum.cpp new file mode 100644 index 0000000..caea94a --- /dev/null +++ b/Easy/1-two-sum.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector ans; + + for(int i=0; ival == 0 && l1->next == nullptr){ + answer = l2; + } + else if(l2->val == 0 && l2->next == nullptr){ + answer = l1; + } + else{ + while(true){ + + sum = l1->val + l2->val + carry; + carry = sum/10; + addElementToLinkedList(answer,sum%10,size); + + if(l1->next == nullptr && l2->next == nullptr){ + if(carry>0){ + addElementToLinkedList(answer,carry,100); + } + break; + } + + if(l1->next != nullptr){ + l1 = l1->next; + } + else{ + l1->val = 0; + } + + if(l2->next != nullptr){ + l2 = l2->next; + } + else{ + l2->val = 0; + } + size++; + + } + } + + return answer; + + } + + void addElementToLinkedList(ListNode* head,int value,int size){ + + ListNode* cur = head; + + if(size==0){ + head->val = value; + return; + } + + while(cur->next != nullptr){ + cur = cur->next; + } + ListNode* newNode = new ListNode(value); + cur->next = newNode; + return; + } + + +}; \ No newline at end of file diff --git a/Easy/26-remove-duplicates-from-sorted-array.cpp b/Easy/26-remove-duplicates-from-sorted-array.cpp new file mode 100644 index 0000000..9f9f96c --- /dev/null +++ b/Easy/26-remove-duplicates-from-sorted-array.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int removeDuplicates(vector& nums) { + for(int i=0; i0) + if(n>0) { + if(n==1) + return x; + double ans = myPow(x, n/2); // Recursive call + if(n&1) + return x*ans*ans; + else + return ans*ans; + } + + // For zero power (n==0) + return 1; + } +}; \ No newline at end of file From 10619cbfe80c3205c94854c8947832a2bb41bad2 Mon Sep 17 00:00:00 2001 From: Akash Gautam <76935019+geekblower@users.noreply.github.com> Date: Sun, 9 Oct 2022 23:07:43 +0530 Subject: [PATCH 2/2] Add files via upload --- .../160-intersection-of-two-linked-lists.cpp | 38 ++++++++++++ Medium/240-search-a-2d-matrix-ii.cpp | 27 +++++++++ ...it-array-into-consecutive-subsequences.cpp | 33 +++++++++++ Medium/858-mirror-reflection.cpp | 13 +++++ Medium/994-rotting-oranges.cpp | 58 +++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 Medium/160-intersection-of-two-linked-lists.cpp create mode 100644 Medium/240-search-a-2d-matrix-ii.cpp create mode 100644 Medium/659-split-array-into-consecutive-subsequences.cpp create mode 100644 Medium/858-mirror-reflection.cpp create mode 100644 Medium/994-rotting-oranges.cpp diff --git a/Medium/160-intersection-of-two-linked-lists.cpp b/Medium/160-intersection-of-two-linked-lists.cpp new file mode 100644 index 0000000..2f4491c --- /dev/null +++ b/Medium/160-intersection-of-two-linked-lists.cpp @@ -0,0 +1,38 @@ +/** + * 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) { + ListNode *a=headA, *b=headB; + int count1 = 0, count2 = 0; + + for(ListNode *curr=headA; curr!=NULL; curr=curr->next) + count1++; + + for(ListNode *curr=headB; curr!=NULL; curr=curr->next) + count2++; + + while(count1 > count2) { + count1--; + a = a->next; + } + + while(count2 > count1) { + count2--; + b = b->next; + } + + while(a != b) { + a = a->next; + b = b->next; + } + + return a; + } +}; \ No newline at end of file diff --git a/Medium/240-search-a-2d-matrix-ii.cpp b/Medium/240-search-a-2d-matrix-ii.cpp new file mode 100644 index 0000000..9bda1d6 --- /dev/null +++ b/Medium/240-search-a-2d-matrix-ii.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + int row = matrix.size(); + int col = matrix[0].size(); + int rowCount = 0; + int colCount = col - 1; + + while((rowCount < row) && (colCount >= 0)) { + int element = matrix[rowCount][colCount]; + + if(element == target) { + return true; + } + + if(element > target) { + colCount--; + } + + if(element < target) { + rowCount++; + } + } + + return false; + } +}; \ No newline at end of file diff --git a/Medium/659-split-array-into-consecutive-subsequences.cpp b/Medium/659-split-array-into-consecutive-subsequences.cpp new file mode 100644 index 0000000..81f7c9d --- /dev/null +++ b/Medium/659-split-array-into-consecutive-subsequences.cpp @@ -0,0 +1,33 @@ +class Solution { + public: + + bool isPossible(vector& nums) { + unordered_map freq; + + for(int x:nums) { + freq[x]++; + } + + unordered_map need; + + for(int n:nums){ + if(freq[n]==0) { + continue; + } else if(need[n]>0){ + need[n]--; + freq[n]--; + need[n+1]++; + } else if(freq[n]>0 && freq[n+1]>0 && freq[n+2]>0) { + freq[n]--; + freq[n+1]--; + freq[n+2]--; + + need[n+3]++; + } else { + return false; + } + } + + return true; + } +}; \ No newline at end of file diff --git a/Medium/858-mirror-reflection.cpp b/Medium/858-mirror-reflection.cpp new file mode 100644 index 0000000..84f7086 --- /dev/null +++ b/Medium/858-mirror-reflection.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int mirrorReflection(int p, int q) { + while(p%2==0 && q%2==0) { + p/=2; + q/=2; + } + + int ans = 1 - (p%2) + (q%2); + + return ans; + } +}; \ No newline at end of file diff --git a/Medium/994-rotting-oranges.cpp b/Medium/994-rotting-oranges.cpp new file mode 100644 index 0000000..fb6d36d --- /dev/null +++ b/Medium/994-rotting-oranges.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + int orangesRotting(vector>& grid) { + queue > q; + int time = 0; + int count = 0; + int orange = 0; + + int m = grid.size(); + int n = grid[0].size(); + + for(int i=0; i curr = q.front(); + q.pop(); + int x = curr[0]; + int y = curr[1]; + + for(int i=0; i<4; i++) { + int nx = x+dx[i]; + int ny = y+dy[i]; + + if(nx<0 || nx>=m || ny<0 || ny>=n || grid[nx][ny] != 1) + continue; + + grid[nx][ny] = 2; + q.push({nx,ny}); + } + } + if(!q.empty()) + time++; + } + + return count==orange ? time : -1; + } +}; \ No newline at end of file