Make Unreal REAL.
article thumbnail
Level 0. 무작위로 K개의 수 뽑기

 

 

배열의 인덱스와 값을 활용해 O(1) 시간에 중복을 확인했다.

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr, int k)
{
    int* p = new int[100'001]();
    vector<int> answer(k, -1);
    int i = 0;

    for (int e : arr)
    {
        if (k <= i)
            break;

        if (p[e] == 0)
        {
            ++p[e];
            answer[i++] = e;
        }
    }
    
    delete [] p;

    return answer;
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 0. 배열 만들기 6  (0) 2023.05.03
Level 0. 그림 확대  (0) 2023.05.02
Level 0. 왼쪽 오른쪽  (0) 2023.05.01
Level 0. 리스트 자르기  (0) 2023.05.01
Level 0. 수열과 구간 쿼리 1  (0) 2023.04.30
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그