less than 1 minute read

[no-alignment]

구현 방식

  • 탐색해야하는 M개의 정수 중 하나씩 선택해서
  • N개의 숫자 카드 중에 있는지 이분 탐색 실시
void solution(int N, int M, vector<int>& cards, vector<int>& numbers)
{
    sort(cards.begin(), cards.end());
    for (auto number : numbers)
    {
        if (isInCards(number, N, cards)) cout << 1 << " ";
        else cout << 0 << " ";
    }
}

bool isInCards(int number, int N, vector<int>& cards)
{
    int minNumberIdx = 0;
    int maxNumberIdx = N - 1;
    int midNumberIdx = (minNumberIdx + maxNumberIdx) / 2;

    while (minNumberIdx < maxNumberIdx)
    {
        if (cards[midNumberIdx] == number)
            return true;
        else if (cards[midNumberIdx] < number)
            minNumberIdx = midNumberIdx + 1;
        else
            maxNumberIdx = midNumberIdx - 1;
        midNumberIdx = (minNumberIdx + maxNumberIdx) / 2;
    }
    if (cards[midNumberIdx] == number)
        return true;
    else
        return false;
}