starting out: 88-merge-sorted-array

This commit is contained in:
Kaushik Narayan R 2023-09-26 23:12:56 -07:00
commit a9d7373732
3 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,39 @@
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using std::vector;
class Solution
{
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++)
{
// 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++];
}
}
nums1 = result;
}
};

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# leetcode-gulag: self-coerced doses of daily leetcode
- My work on various Leetcode problems
- Shoutout to [leetcode-torture](https://github.com/The-CodingSloth/haha-funny-leetcode-extension) for making me commit to this

5
thoughts.md Normal file
View File

@ -0,0 +1,5 @@
# misc. thoughts and maybe tips
- libraries:
- essentials: bits/stdc++.h, iostream
- data structures: vector