diff --git a/Famous-Coding-Interview-Problems/MergeKSortedList.cpp b/Famous-Coding-Interview-Problems/MergeKSortedList.cpp new file mode 100644 index 0000000..d7e349b --- /dev/null +++ b/Famous-Coding-Interview-Problems/MergeKSortedList.cpp @@ -0,0 +1,87 @@ + +// 4. link : [ https://leetcode.com/problems/merge-k-sorted-lists ] + + +/** + * 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* merge(ListNode* l1, ListNode* l2) { + if(l1 == NULL && l2 == NULL) + return NULL; + if(l1==NULL) + return l2; + if(l2==NULL) + return l1; + + ListNode* temp1 = l1; + ListNode* temp2 = l2; + + ListNode* tail = new ListNode(0); + ListNode* head = tail; + + + while(temp1 && temp2){ + if(temp1->val <= temp2->val){ + ListNode* newNode = new ListNode(temp1->val); + tail->next = newNode; + tail = newNode; + temp1 = temp1->next; + }else{ + ListNode* newNode = new ListNode(temp2->val); + tail->next = newNode; + tail = newNode; + temp2 = temp2->next; + } + } + + while(temp1){ + ListNode* newNode = new ListNode(temp1->val); + tail->next = newNode; + tail = newNode; + temp1 = temp1->next; + } + + while(temp2){ + ListNode* newNode = new ListNode(temp2->val); + tail->next = newNode; + tail = newNode; + temp2 = temp2->next; + } + + return (head->next); +} + + ListNode* helper(vector& lists, int l, int r){ + if(l == r) + return lists[l]; + + int mid = l+(r-l)/2; + + ListNode* h1 = helper(lists,l,mid); + ListNode* h2 = helper(lists,mid+1,r); + + return merge(h1,h2); + + } + + ListNode* mergeKLists(vector& lists) { + if(lists.size() == 0) + return NULL; + + int l = 0; + int r = lists.size()-1; + + return helper(lists,l,r); + } +}; \ No newline at end of file diff --git a/Famous-Coding-Interview-Problems/firstAndLastPos.cpp b/Famous-Coding-Interview-Problems/firstAndLastPos.cpp new file mode 100644 index 0000000..cb1c0bb --- /dev/null +++ b/Famous-Coding-Interview-Problems/firstAndLastPos.cpp @@ -0,0 +1,77 @@ + +// 7 . link : [ https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array ] + +#include +using namespace std; + + int binarySearch(vector& nums, int l,int r, int target){ + if(l>r){ + return -1; + } + 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); + } + + vector searchRange(vector& nums, int target) { + int pos = binarySearch(nums,0,nums.size()-1, target); + + if(pos < 0) + return {-1,-1}; + int left = pos; + int right = pos; + + while(left > 0){ + if(nums[left-1] != target) + break; + left--; + } + while(right < nums.size()-1){ + if(nums[right+1] != target) + break; + right++; + } + + return {left,right}; + } + +/* +1 +9 +5 7 7 8 8 8 9 10 11 +8 + + +3 5 +*/ + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int test; + cin >> test; + + while(test--){ + int n; + cin >> n; + vector arr(n); + for(int i=0; i> arr[i]; + + int target ; cin >> target; + + vector index = searchRange(arr , target); + cout << index[0] << " " << index[1] << endl;; + } + + + return 0; +} \ No newline at end of file diff --git a/Famous-Coding-Interview-Problems/longestWord.cpp b/Famous-Coding-Interview-Problems/longestWord.cpp new file mode 100644 index 0000000..e2b92a5 --- /dev/null +++ b/Famous-Coding-Interview-Problems/longestWord.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + +string LongestWord(string sen) { + + // code goes here + string ans=""; + string currStr=""; + + + for(int i=0; i= 'a' && sen[i] <= 'z'){ + currStr += sen[i]; + }else{ + if(currStr.size() > ans.size()){ + ans = currStr; + currStr = ""; + } + } + } + + if(currStr.size() > ans.size()) + ans = currStr; + + return ans; + +} + + + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + cout << LongestWord("fun&!! time"); + + string s = "fun&!! time"; + + + return 0; + +} \ No newline at end of file diff --git a/Famous-Coding-Interview-Problems/mergeTwoSortedList.cpp b/Famous-Coding-Interview-Problems/mergeTwoSortedList.cpp new file mode 100644 index 0000000..957d3dc --- /dev/null +++ b/Famous-Coding-Interview-Problems/mergeTwoSortedList.cpp @@ -0,0 +1,123 @@ + +// 3. link : [ https://leetcode.com/problems/merge-two-sorted-lists ] + +#include +using namespace std; + +// node class +class ListNode{ + public: + int val; + ListNode* next; + + ListNode(int data){ + this->val = data; + next = NULL; + } + +}; + + + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode* temp1 = l1; + ListNode* temp2 = l2; + + ListNode* tail = new ListNode(0); + ListNode* head = tail; + + + while(temp1 && temp2){ + if(temp1->val <= temp2->val){ + ListNode* newNode = new ListNode(temp1->val); + tail->next = newNode; + tail = newNode; + temp1 = temp1->next; + }else{ + ListNode* newNode = new ListNode(temp2->val); + tail->next = newNode; + tail = newNode; + temp2 = temp2->next; + } + } + + while(temp1){ + ListNode* newNode = new ListNode(temp1->val); + tail->next = newNode; + tail = newNode; + temp1 = temp1->next; + } + + while(temp2){ + ListNode* newNode = new ListNode(temp2->val); + tail->next = newNode; + tail = newNode; + temp2 = temp2->next; + } + + return head->next; +} + +ListNode* takeInput(){ + ListNode* head = NULL; + ListNode* tail = NULL; + + int data; + while(cin >> data && data != -1){ + ListNode* node = new ListNode(data); + if(head == NULL){ + head = node; + tail = node; + }else{ + tail->next = node; + tail = node; + } + } + return head; +} + +void print(ListNode* head){ + if(head == NULL) + return; + + while(head){ + cout << head->val << " "; + head = head->next; + } + cout << endl; +} + + + +/* +1 +1 +1 3 4 5 6 -1 +-2 -4 3 7 9 -1 + +-2 -4 1 3 3 4 5 6 7 9 + +*/ + +int main(){ + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int test; + cin >> test; + + while(test--){ + + ListNode* l1 = takeInput(); + ListNode* l2 = takeInput(); + + ListNode* head = mergeTwoLists(l1, l2); + print(head); + + + } + + + return 0; +} \ No newline at end of file diff --git a/Famous-Coding-Interview-Problems/rainWaterTrapping.cpp b/Famous-Coding-Interview-Problems/rainWaterTrapping.cpp new file mode 100644 index 0000000..a223247 --- /dev/null +++ b/Famous-Coding-Interview-Problems/rainWaterTrapping.cpp @@ -0,0 +1,61 @@ + +// 5. link : [ https://leetcode.com/problems/trapping-rain-water ] + +#include +using namespace std; + + +int trap(vector& height) { + int n = height.size(); + if(n<=2) + return 0; + + int left[n]; + int right[n]; + + left[0] = height[0]; + for(int i=1; i=0; i--) + right[i] = max(right[i+1],height[i]); + + int water=0; + for(int i=0; i> test; + + while(test--){ + int n; + cin >> n; + vector arr(n); + for(int i=0; i> arr[i]; + + cout << trap(arr) << endl;; + } + + + return 0; +} \ No newline at end of file diff --git a/Famous-Coding-Interview-Problems/threeSum.cpp b/Famous-Coding-Interview-Problems/threeSum.cpp new file mode 100644 index 0000000..279a3ad --- /dev/null +++ b/Famous-Coding-Interview-Problems/threeSum.cpp @@ -0,0 +1,76 @@ + +// 2. link : [ https://leetcode.com/problems/3sum ] + +#include +using namespace std; + +vector> threeSum(vector& nums){ + int n = nums.size(); + set > s; + vector> res; + + sort(nums.begin(), nums.end()); + + for(int i=0; i> test; + + while(test--){ + int n; + cin >> n; + vector arr(n); + for(int i=0; i> arr[i]; + + vector> res = threeSum(arr); + for(int i=0; i