From cd5dc9971da025efb85ee9100b0850a8e9ba7d09 Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:26:49 +0530 Subject: [PATCH 1/7] Create merge-sort.cpp I have added merge sort algorithm in C++. Explained all the steps in the comments as well. --- c++/merge-sort.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 c++/merge-sort.cpp diff --git a/c++/merge-sort.cpp b/c++/merge-sort.cpp new file mode 100644 index 0000000..6f543bf --- /dev/null +++ b/c++/merge-sort.cpp @@ -0,0 +1,104 @@ +// C++ program for Merge Sort +#include +using namespace std; + +// Merges two subarrays of array[]. +// First subarray is arr[begin..mid] +// Second subarray is arr[mid+1..end] +void merge(int array[], int const left, int const mid, + int const right) +{ + int const subArrayOne = mid - left + 1; + int const subArrayTwo = right - mid; + + // Create temp arrays + auto *leftArray = new int[subArrayOne], + *rightArray = new int[subArrayTwo]; + + // Copy data to temp arrays leftArray[] and rightArray[] + for (auto i = 0; i < subArrayOne; i++) + leftArray[i] = array[left + i]; + for (auto j = 0; j < subArrayTwo; j++) + rightArray[j] = array[mid + 1 + j]; + + auto indexOfSubArrayOne = 0, indexOfSubArrayTwo = 0; + int indexOfMergedArray = left; + + // Merge the temp arrays back into array[left..right] + while (indexOfSubArrayOne < subArrayOne + && indexOfSubArrayTwo < subArrayTwo) { + if (leftArray[indexOfSubArrayOne] + <= rightArray[indexOfSubArrayTwo]) { + array[indexOfMergedArray] + = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + } + else { + array[indexOfMergedArray] + = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + } + indexOfMergedArray++; + } + + // Copy the remaining elements of + // left[], if there are any + while (indexOfSubArrayOne < subArrayOne) { + array[indexOfMergedArray] + = leftArray[indexOfSubArrayOne]; + indexOfSubArrayOne++; + indexOfMergedArray++; + } + + // Copy the remaining elements of + // right[], if there are any + while (indexOfSubArrayTwo < subArrayTwo) { + array[indexOfMergedArray] + = rightArray[indexOfSubArrayTwo]; + indexOfSubArrayTwo++; + indexOfMergedArray++; + } + delete[] leftArray; + delete[] rightArray; +} + +// begin is for left index and end is right index +// of the sub-array of arr to be sorted +void mergeSort(int array[], int const begin, int const end) +{ + if (begin >= end) + return; + + int mid = begin + (end - begin) / 2; + mergeSort(array, begin, mid); + mergeSort(array, mid + 1, end); + merge(array, begin, mid, end); +} + +// UTILITY FUNCTIONS +// Function to print an array +void printArray(int A[], int size) +{ + for (int i = 0; i < size; i++) + cout << A[i] << " "; + cout << endl; +} + +// Driver code +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + int arr_size = sizeof(arr) / sizeof(arr[0]); + + cout << "Given array is \n"; + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + cout << "\nSorted array is \n"; + printArray(arr, arr_size); + return 0; +} + +// This code is contributed by Mayank Tyagi +// This code was revised by Joshua Estes From 92d4229613cca34eac68bcc7703040bfc2bcf8da Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:59:11 +0530 Subject: [PATCH 2/7] Create longest-palindromic-subseq.cpp Added C++ code for finding the length of the Longest Palindromic Subsequence. --- c++/longest-palindromic-subseq.cpp | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 c++/longest-palindromic-subseq.cpp diff --git a/c++/longest-palindromic-subseq.cpp b/c++/longest-palindromic-subseq.cpp new file mode 100644 index 0000000..ade1a2f --- /dev/null +++ b/c++/longest-palindromic-subseq.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + +int tabulation(string t1, string t2) +{ + vectorcurr(t2.length()+19,0); + vectornext(t2.length()+19,0); + + for(int i = t1.length()-1;i>=0;i--) + { + for(int j =t2.length()-1;j>=0;j--) + { + int ans =0; + if(t1[i]==t2[j]) + { + ans=1+next[j+1]; + } + else + { + ans=max(next[j],curr[j+1]); + } + curr[j]=ans; + } + next=curr; + } + return curr[0]; +} + + int longestPalindromeSubseq(string s) { + string str=s; + reverse(s.begin(),s.end()); + return tabulation(s,str); + } +}; From 971bb7aa9eab2c4f7367b146912595b248abcfb4 Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:01:21 +0530 Subject: [PATCH 3/7] Create Regex-matching.cpp Added detailed C++ code for regular expression matching with support for '.' and '*'. --- c++/Regex-matching.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 c++/Regex-matching.cpp diff --git a/c++/Regex-matching.cpp b/c++/Regex-matching.cpp new file mode 100644 index 0000000..17c899d --- /dev/null +++ b/c++/Regex-matching.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + bool isMatch(string s, string p) { + int n = s.length(), m = p.length(); + bool dp[n+1][m+1]; + memset(dp, false, sizeof(dp)); + dp[0][0] = true; + + for(int i=0; i<=n; i++){ + for(int j=1; j<=m; j++){ + if(p[j-1] == '*'){ + dp[i][j] = dp[i][j-2] || (i > 0 && (s[i-1] == p[j-2] || p[j-2] == '.') && dp[i-1][j]); + } + else{ + dp[i][j] = i > 0 && dp[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.'); + } + } + } + + return dp[n][m]; + } +}; From fe2642823970fc4f6ea7e9a25caea6e3d44dd9ac Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:03:41 +0530 Subject: [PATCH 4/7] Create findMedianSortedArrays.cpp Added detailed C++ solution to return the median of the two sorted arrays in O(log (m+n)). --- c++/findMedianSortedArrays.cpp | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 c++/findMedianSortedArrays.cpp diff --git a/c++/findMedianSortedArrays.cpp b/c++/findMedianSortedArrays.cpp new file mode 100644 index 0000000..c713d9e --- /dev/null +++ b/c++/findMedianSortedArrays.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + if (nums1.size() > nums2.size()) { + return findMedianSortedArrays(nums2, nums1); + } + + int m = nums1.size(), n = nums2.size(); + int left = 0, right = m; + + while (left <= right) { + int partitionA = (left + right) / 2; + int partitionB = (m + n + 1) / 2 - partitionA; + + int maxLeftA = (partitionA == 0) ? INT_MIN : nums1[partitionA - 1]; + int minRightA = (partitionA == m) ? INT_MAX : nums1[partitionA]; + int maxLeftB = (partitionB == 0) ? INT_MIN : nums2[partitionB - 1]; + int minRightB = (partitionB == n) ? INT_MAX : nums2[partitionB]; + + if (maxLeftA <= minRightB && maxLeftB <= minRightA) { + if ((m + n) % 2 == 0) { + return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2.0; + } else { + return max(maxLeftA, maxLeftB); + } + } else if (maxLeftA > minRightB) { + right = partitionA - 1; + } else { + left = partitionA + 1; + } + } + + return 0.0; + } +}; From b53cacbc4abb292c3bb70ad511abee8edf82ca15 Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:19:51 +0530 Subject: [PATCH 5/7] Delete c++/findMedianSortedArrays.cpp --- c++/findMedianSortedArrays.cpp | 35 ---------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 c++/findMedianSortedArrays.cpp diff --git a/c++/findMedianSortedArrays.cpp b/c++/findMedianSortedArrays.cpp deleted file mode 100644 index c713d9e..0000000 --- a/c++/findMedianSortedArrays.cpp +++ /dev/null @@ -1,35 +0,0 @@ -class Solution { -public: - double findMedianSortedArrays(vector& nums1, vector& nums2) { - if (nums1.size() > nums2.size()) { - return findMedianSortedArrays(nums2, nums1); - } - - int m = nums1.size(), n = nums2.size(); - int left = 0, right = m; - - while (left <= right) { - int partitionA = (left + right) / 2; - int partitionB = (m + n + 1) / 2 - partitionA; - - int maxLeftA = (partitionA == 0) ? INT_MIN : nums1[partitionA - 1]; - int minRightA = (partitionA == m) ? INT_MAX : nums1[partitionA]; - int maxLeftB = (partitionB == 0) ? INT_MIN : nums2[partitionB - 1]; - int minRightB = (partitionB == n) ? INT_MAX : nums2[partitionB]; - - if (maxLeftA <= minRightB && maxLeftB <= minRightA) { - if ((m + n) % 2 == 0) { - return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2.0; - } else { - return max(maxLeftA, maxLeftB); - } - } else if (maxLeftA > minRightB) { - right = partitionA - 1; - } else { - left = partitionA + 1; - } - } - - return 0.0; - } -}; From 82efd8dc3485e63194b6ee7ab3c86bb3b64c37ee Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Thu, 26 Oct 2023 12:20:04 +0530 Subject: [PATCH 6/7] Delete c++/Regex-matching.cpp --- c++/Regex-matching.cpp | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 c++/Regex-matching.cpp diff --git a/c++/Regex-matching.cpp b/c++/Regex-matching.cpp deleted file mode 100644 index 17c899d..0000000 --- a/c++/Regex-matching.cpp +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -public: - bool isMatch(string s, string p) { - int n = s.length(), m = p.length(); - bool dp[n+1][m+1]; - memset(dp, false, sizeof(dp)); - dp[0][0] = true; - - for(int i=0; i<=n; i++){ - for(int j=1; j<=m; j++){ - if(p[j-1] == '*'){ - dp[i][j] = dp[i][j-2] || (i > 0 && (s[i-1] == p[j-2] || p[j-2] == '.') && dp[i-1][j]); - } - else{ - dp[i][j] = i > 0 && dp[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.'); - } - } - } - - return dp[n][m]; - } -}; From 0181a06e0d9a94f056871d73e9183e3b625bd7d5 Mon Sep 17 00:00:00 2001 From: Patil Shrivardhan Chandrakant <90125299+shrivardha@users.noreply.github.com> Date: Thu, 26 Oct 2023 14:00:19 +0530 Subject: [PATCH 7/7] Create sudokusolver.cpp Added C++ implementation for a sudoku solver --- c++/sudokusolver.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 c++/sudokusolver.cpp diff --git a/c++/sudokusolver.cpp b/c++/sudokusolver.cpp new file mode 100644 index 0000000..e3b7665 --- /dev/null +++ b/c++/sudokusolver.cpp @@ -0,0 +1,53 @@ +class Solution { +private: + void ans(vector>& board, vector>& curr, int i, int j, vector>& col, vector>& row, vector>& mat) { + while(i < 9 && board[i][j] != '.') { + if (j < 8) j++; + else { + j = 0; + i++; + } + } + if(i == 9) { + curr = board; + return; + } + + for(int k = 1; k <= 9; k++) { + if(curr.size() > 0) break; + if(!col[j][k] && !row[i][k] && !mat[3*(i/3) + (j/3)][k]) { + col[j][k] = true; + row[i][k] = true; + mat[3*(i/3) + (j/3)][k] = true; + board[i][j] = (char)('0'+k); + ans(board, curr, i, j, col, row, mat); + col[j][k] = false; + row[i][k] = false; + mat[3*(i/3) + (j/3)][k] = false; + board[i][j] = '.'; + } + } + } +public: + void solveSudoku(vector>& board) { + vector> col(9, vector(10, false)); + vector> row(9, vector(10, false)); + vector> mat(9, vector(10, false)); + vector> curr; + + for(int i = 0; i < 9; i++) { + for(int j = 0; j < 9; j++) { + if(board[i][j] != '.') { + int x = board[i][j] - '0'; + row[i][x] = true; + col[j][x] = true; + mat[3*(i/3) + (j/3)][x] = true; + } + } + } + + ans(board, curr, 0, 0, col, row, mat); + board = curr; + return; + } +};