프로그래머스 레벨 1 완주하지 못한 선수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string solution(vector<string> participant, vector<string> completion) 
{
    int iSize = participant.size();
    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
 
    for(int i = 0 ; i < iSize ; i++)
    {
        //if(participant[i] != completion[i]) //정석 풀이
            //return participant[i];
        if(participant[i] == completion[i]) //내 방식 풀이
            continue;
        else
            return participant[i];
    }
 
    return string();
}
 

algorithm 헤더를 사용해 벡터를 빠르게 정렬할 수 있음.

 

같은 string 비교는 굳이 compare가 아니라 연산자 오버로딩된 것으로도 가능함

 

 

프로그래머스 레벨 2 테스트 문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) 
{
 
    int arr1Row = arr1.size();
    int arr1Col = arr1[0].size();
    int arr2Row = arr2.size();
    int arr2Col = arr2[0].size();
 
    vector<vector<int>> answer(arr1Row, vector<int>(arr2Col));
 
    int temp  = 0;
 
       for(int i = 0; i < arr1Row; ++i) //행 이동
    {
        for(int j = 0; j < arr2Col; ++j) //열 이동
        {
            temp = 0;
 
            for(int k = 0; k < arr1Col; ++k) //행렬 1 열 이동, 행렬 2 행 이동 및 연산
            {
                temp += arr1[i][k] * arr2[k][j];
            }
            //행렬의 곱을 정답 행렬에 입력
            answer[i][j] = temp;
        }
    }
 
    return answer;
}
 
 

의사코드

1. 두 행렬의 사이즈를 구한다.

2. 1행렬의 행과 2행렬의 열로 정답 행렬을 Reserve 한다.

3. 3중 For문으로 계산해서 답을 넣는다.

 - 3for문 행렬 1의 열 이동, 행렬 2의 행 이동과 연산을 처리

 - 2for문 행렬 2의 열 이동

 - 1for문 행렬 1의 행 이동

프로그래머스 2레벨

 

평행한 수평선위의 여러개의 전파탑에서 동시에 왼쪽 방향으로 송신할때 수신받는 전파탑 찾기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    vector<int> heights = { 6,9,5,7,4 };
 
    int iSize = heights.size();
    vector<int> answer;
 
    for (int i = 0; i < iSize - 1++i)
    {
        int iPivot = i + 1;
 
        for (int j = i + 1; j >= 1--j)
        {
            if (answer[iPivot] == 0 && heights[iPivot] < heights[j - 1])
            {
                answer[iPivot] = j;
                break;
            }
 
        }
    }
 

 

+ Recent posts