mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
day 2: 69-sqrt-x
also refactored, added drivers
This commit is contained in:
14
88-merge-sorted-array/driver.cpp
Normal file
14
88-merge-sorted-array/driver.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "soln.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
vector<int> nums1 = vector<int>{1, 2, 3, 0, 0, 0};
|
||||
vector<int> nums2 = vector<int>{2, 5, 6};
|
||||
vector<int> answer = vector<int>{1, 2, 2, 3, 5, 6};
|
||||
Solution soln;
|
||||
cout << "Merged correctly? " << soln.test(nums1, 3, nums2, 3, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,39 +1,41 @@
|
||||
#include <bits/stdc++.h>
|
||||
#include <iostream>
|
||||
#include "soln.hpp"
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
|
||||
class Solution
|
||||
void Solution::merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
|
||||
{
|
||||
public:
|
||||
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
|
||||
int arr1_p = 0, arr2_p = 0;
|
||||
vector<int> result = vector<int>(m + n);
|
||||
for (int i = 0; i < m + n; i++)
|
||||
{
|
||||
int arr1_p = 0, arr2_p = 0;
|
||||
vector<int> result = vector<int>(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;
|
||||
}
|
||||
};
|
||||
nums1 = result;
|
||||
};
|
||||
|
||||
bool Solution::test(vector<int> &nums1, int m, vector<int> &nums2, int n, vector<int> &answer)
|
||||
{
|
||||
vector<int> nums1_copy(nums1);
|
||||
merge(nums1_copy, m, nums2, n);
|
||||
return answer == nums1_copy;
|
||||
}
|
||||
8
88-merge-sorted-array/soln.hpp
Normal file
8
88-merge-sorted-array/soln.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n);
|
||||
bool test(vector<int> &nums1, int m, vector<int> &nums2, int n, vector<int> &answer);
|
||||
};
|
||||
Reference in New Issue
Block a user