From 9da326288a0ae975ccc25940c5b894419e8757c4 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Wed, 27 Sep 2023 11:22:43 -0700 Subject: [PATCH] day 2: 69-sqrt-x also refactored, added drivers --- .gitignore | 2 ++ 69-sqrt-x/driver.cpp | 12 +++++++ 69-sqrt-x/soln.cpp | 32 +++++++++++++++++ 69-sqrt-x/soln.hpp | 6 ++++ 88-merge-sorted-array/driver.cpp | 14 ++++++++ 88-merge-sorted-array/soln.cpp | 62 ++++++++++++++++---------------- 88-merge-sorted-array/soln.hpp | 8 +++++ 7 files changed, 106 insertions(+), 30 deletions(-) create mode 100644 .gitignore create mode 100644 69-sqrt-x/driver.cpp create mode 100644 69-sqrt-x/soln.cpp create mode 100644 69-sqrt-x/soln.hpp create mode 100644 88-merge-sorted-array/driver.cpp create mode 100644 88-merge-sorted-array/soln.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..decaba0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +*.exe diff --git a/69-sqrt-x/driver.cpp b/69-sqrt-x/driver.cpp new file mode 100644 index 0000000..a06b099 --- /dev/null +++ b/69-sqrt-x/driver.cpp @@ -0,0 +1,12 @@ +#include "soln.hpp" +#include + +using namespace std; + +int main(int argc, char *argv[]) +{ + int x = 69, answer = 8; + Solution soln; + cout << "Square root correct? " << soln.test(x, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/69-sqrt-x/soln.cpp b/69-sqrt-x/soln.cpp new file mode 100644 index 0000000..fac8540 --- /dev/null +++ b/69-sqrt-x/soln.cpp @@ -0,0 +1,32 @@ +#include "soln.hpp" +#include + +int Solution::mySqrt(int x) +{ + // base case + if (x == 0 || x == 1) + { + return x; + } + // effectively only till ~sqrt(x) + for (int i = 2; i <= x; i++) + { + long i_square = long(i) * i; + // x is a perfect square + if (i_square == x) + { + return int(i); + } + // x is not a perfect square + else if (i_square > x) + { + return int(i - 1); + } + } + return -1; // compiler fuss +} + +bool Solution::test(int x, int answer) +{ + return answer == mySqrt(x); +} \ No newline at end of file diff --git a/69-sqrt-x/soln.hpp b/69-sqrt-x/soln.hpp new file mode 100644 index 0000000..76e5c84 --- /dev/null +++ b/69-sqrt-x/soln.hpp @@ -0,0 +1,6 @@ +class Solution +{ +public: + int mySqrt(int x); + bool test(int x, int answer); +}; \ No newline at end of file diff --git a/88-merge-sorted-array/driver.cpp b/88-merge-sorted-array/driver.cpp new file mode 100644 index 0000000..dca9d79 --- /dev/null +++ b/88-merge-sorted-array/driver.cpp @@ -0,0 +1,14 @@ +#include "soln.hpp" +#include + +using namespace std; + +int main(int argc, char *argv[]) +{ + vector nums1 = vector{1, 2, 3, 0, 0, 0}; + vector nums2 = vector{2, 5, 6}; + vector answer = vector{1, 2, 2, 3, 5, 6}; + Solution soln; + cout << "Merged correctly? " << soln.test(nums1, 3, nums2, 3, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/88-merge-sorted-array/soln.cpp b/88-merge-sorted-array/soln.cpp index 272cd13..20dc28f 100644 --- a/88-merge-sorted-array/soln.cpp +++ b/88-merge-sorted-array/soln.cpp @@ -1,39 +1,41 @@ -#include -#include +#include "soln.hpp" #include using std::vector; -class Solution +void Solution::merge(vector &nums1, int m, vector &nums2, int n) { -public: - void merge(vector &nums1, int m, vector &nums2, int n) + int arr1_p = 0, arr2_p = 0; + vector result = vector(m + n); + for (int i = 0; i < m + n; i++) { - int arr1_p = 0, arr2_p = 0; - vector result = vector(m + n); - for (int i = 0; i < m + n; i++) + // second array depleted + if (arr2_p >= n) { - // second array depleted - if (arr2_p >= n) - { - result[i] = nums1[arr1_p++]; - } - // first array depleted - else if (arr1_p >= m) - { - result[i] = nums2[arr2_p++]; - } - // nums2 elem greater, take from nums1 - else if (nums2[arr2_p] > nums1[arr1_p]) - { - result[i] = nums1[arr1_p++]; - } - // nums1 elem greater, take from nums2 - else if (nums2[arr2_p] <= nums1[arr1_p]) - { - result[i] = nums2[arr2_p++]; - } + result[i] = nums1[arr1_p++]; + } + // first array depleted + else if (arr1_p >= m) + { + result[i] = nums2[arr2_p++]; + } + // nums2 elem greater, take from nums1 + else if (nums2[arr2_p] > nums1[arr1_p]) + { + result[i] = nums1[arr1_p++]; + } + // nums1 elem greater, take from nums2 + else if (nums2[arr2_p] <= nums1[arr1_p]) + { + result[i] = nums2[arr2_p++]; } - nums1 = result; } -}; \ No newline at end of file + nums1 = result; +}; + +bool Solution::test(vector &nums1, int m, vector &nums2, int n, vector &answer) +{ + vector nums1_copy(nums1); + merge(nums1_copy, m, nums2, n); + return answer == nums1_copy; +} \ No newline at end of file diff --git a/88-merge-sorted-array/soln.hpp b/88-merge-sorted-array/soln.hpp new file mode 100644 index 0000000..14a48d4 --- /dev/null +++ b/88-merge-sorted-array/soln.hpp @@ -0,0 +1,8 @@ +#include +using std::vector; +class Solution +{ +public: + void merge(vector &nums1, int m, vector &nums2, int n); + bool test(vector &nums1, int m, vector &nums2, int n, vector &answer); +}; \ No newline at end of file