Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Median of Two Sorted Arrays #29

Closed
Tracked by #100
fkdl0048 opened this issue Jul 13, 2024 · 1 comment
Closed
Tracked by #100

Median of Two Sorted Arrays #29

fkdl0048 opened this issue Jul 13, 2024 · 1 comment
Assignees

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Jul 13, 2024

#include <bits/stdc++.h>
#include <cassert>

using namespace std;

// 벡터로 해결하는 방법
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) 
{
    vector<int> merged;

    for(auto n : nums1)
    {
        merged.push_back(n);
    }

    for(auto n : nums2)
    {
        merged.push_back(n);
    }

    sort(merged.begin(), merged.end());

    int size = merged.size();

    if (size % 2 != 0)
        return static_cast<double>(merged[size / 2]);
    else
        return (static_cast<double>(merged[size / 2 - 1]) + static_cast<double>(merged[size / 2])) / 2.0;
}

void testCase()
{
    auto epsilon = 1e-9;  // 작은 허용 오차

    {
        vector<int> nums1{1, 2};
        vector<int> nums2{2};
        auto res = findMedianSortedArrays(nums1, nums2);
        assert(abs(res - 2.0) < epsilon);
    }
    {
        vector<int> nums1{1, 2};
        vector<int> nums2{3, 4};
        auto res = findMedianSortedArrays(nums1, nums2);
        assert(abs(res - 2.5) < epsilon);
    }

    cout << "Passed all test cases" << '\n';
}

int main()
{
    testCase();
    return 0;
}
@fkdl0048 fkdl0048 mentioned this issue Jul 13, 2024
7 tasks
@fkdl0048 fkdl0048 self-assigned this Jul 13, 2024
@fkdl0048 fkdl0048 added this to Todo Jul 13, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Jul 13, 2024
@fkdl0048
Copy link
Owner Author

  • 테스트 코드 형식으로 풀이해봄
  • C++ assert 사용법,
  • 실수끼리의 비교에서 앱실론 사용해야 하는 이유

@github-project-automation github-project-automation bot moved this from Todo to Done in Todo Jul 13, 2024
@fkdl0048 fkdl0048 mentioned this issue Oct 15, 2024
47 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

1 participant