1.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> solution(vector<int> array, vector<vector<int>> commands) 
{
    vector<int> answer;
    
    for(int i = 0; i < commands.size(); i++)
    {
        vector<int> temp;
        
        for(int j = commands[i][0]-1; j < commands[i][1]; j++)
        {
            temp.push_back(array[j]);
        }
        
        sort(temp.begin(), temp.end());
        
        answer.push_back(temp[commands[i][2]-1]);
    }
    
    return answer;
}
cs

 

 

2.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> solution(vector<int> array, vector<vector<int>> commands) 
{
    vector<int> answer;
    
    for(int i = 0; i < commands.size(); i++)
    {
        int num[100];
        int numIdx = 0;
        for(int j = commands[i][0]; j <= commands[i][1]; j++)
        {
            num[numIdx] = array[j-1];
            numIdx++;
        }
        
        sort(num, num+numIdx);
        answer.push_back(num[commands[i][2]-1]);
    }
    
    return answer;
}
cs

1. 

#include <string>
#include <vector>
using namespace std;
 
int solution(int n, vector<int> lost, vector<int> reserve) 
{
    int answer = 0;
    int student[31];
    
    // 모든 학생이 체육복 1개씩 있음
    for(int i = 1; i <= n; i++)
    {
        student[i] = 1;
    }
    
    // 체육복을 도난당한 학생의 체육복 수 -1
    for(int i = 0; i < lost.size(); i++)
    {
        student[lost[i]]--;
    }
    
    // 여벌 체육복이 있는 학생의 체육복 수 +1
    for(int i = 0; i < reserve.size(); i++)
    {
        student[reserve[i]]++;
    }
    
    for(int i = 1; i <= n; i++)
    {
        if(student[i] == 2)
        {
            // 앞 학생
            if(i != 1 && student[i-1== 0)
            {
                student[i]--;
                student[i-1= 1;
            }
            // 뒤 학생
            else if(i != n && student[i+1== 0)
            {
                student[i]--;
                student[i+1= 1;
            }   
        }
    }
    
    for(int i = 1; i <= n; i++)
    {
        if(student[i] >= 1)
        {
            answer++;
        }
    }
    
    return answer;
}
cs

 

 

2.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
int solution(int n, vector<int> lost, vector<int> reserve) 
{
    int answer = 0;
    int visited[31= {0};
    
    sort(lost.begin(), lost.end());
    sort(reserve.begin(), reserve.end());
    
    int lostIdx = 0;
    for(int i = 1; i <= n; i++)
    {
        // 잃어버린 사람 0
        if(lost[lostIdx] == i)
        {
            visited[i] = 0;
            lostIdx++;
        }
        // 잃어버리지 않은 사람 1
        else
        {
            visited[i] = 1;
        }
    }
    
    lostIdx = 0// 여벌 체육복을 가져온 학생이 체육복을 도난당했을 경우, 빌려줄 수 없음을 고려하기 위해 필요
    int reserveIdx = 0;
    for(int i = 1; i <= n; i++)
    {
        int lostflag = 0;
        if(lost[lostIdx] == i)
        {
            lostflag = 1;    
        }   
        
        if(reserve[reserveIdx] == i)
        {
            // 여벌 체육복을 가져온 학생이 체육복을 도난당했을 경우, 빌려줄 수 없음
            if(lost[lostIdx] == i)
            {
                visited[i] = 1;
            }
            else
            {
                if(visited[i-1== 0 && i != 1)
                {
                    visited[i-1= 1;
                }
                else if(visited[i] == 0)
                {
                    visited[i] = 1;
                }
                else if(visited[i+1== 0 && i != n)
                {
                    visited[i+1= 1;
                }    
            }
                
            reserveIdx++;
        }
        
        if(lostflag == 1)
        {
            lostIdx++;
        }
    }
    
    for(int i = 1; i <= n; i++)
    {
        if(visited[i] == 1)
        {
            answer++;
        }
    }
    
    return answer;
}
cs

1.

#include <string>
#include <vector>
using namespace std;
 
vector<int> solution(vector<int> answers) 
{
    vector<int> answer;
    int p1[5= {12345};
    int p2[8= {21232425};
    int p3[10= {3311224455};
    
    int cnt1 = 0;
    int cnt2 = 0;
    int cnt3 = 0;
    for(int i = 0; i < answers.size(); i++)
    {
        int p1Idx = i % 5;
        int p2Idx = i % 8;
        int p3Idx = i % 10;
        
        if(answers[i] == p1[p1Idx])
        {
            cnt1++;
        }
        
        if(answers[i] == p2[p2Idx])
        {
            cnt2++;
        }
        
        if(answers[i] == p3[p3Idx])
        {
            cnt3++;
        }
    }
    
    int Max = max(cnt1, cnt2);
    Max = max(Max, cnt3);
    
    if(Max == cnt1)
    {
        answer.push_back(1);
    }
    
    if(Max == cnt2)
    {
        answer.push_back(2);
    }
    
    if(Max == cnt3)
    {
        answer.push_back(3);
    }
    
    return answer;
}
cs

 

 

2.

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> solution(vector<int> answers) 
{
    vector<int> answer;
    int p1[10001];
    int p2[10001];
    int p3[10001];
    
    int p2Idx = 1;
    for(int i = 1; i < 10001; i++)
    {
        // 1번 수포자
        if(i % 5 == 0)
        {
            p1[i] = 5;
        }
        else
        {
            p1[i] = i % 5;   
        }
        
        // 2번 수포자
        if(i % 2 == 1)
        {
            p2[i] = 2;
        }
        else
        {
            if(p2Idx % 4 == 0)
            {
                p2[i] = 5;
            }
            else if(p2Idx % 4 == 1)
            {
                p2[i] = 1;    
            }
            else if(p2Idx % 4 == 2)
            {
                p2[i] = 3;    
            }
            else if(p2Idx % 4 == 3)
            {
                p2[i] = 4;    
            }
            
            p2Idx++;
        }
        
        // 3번 수포자
        if(i % 10 == 1 || i % 10 == 2)
        {
            p3[i] = 3;
        } 
        else if(i % 10 == 3 || i % 10 == 4)
        {
            p3[i] = 1;
        }
        else if(i % 10 == 5 || i % 10 == 6)
        {
            p3[i] = 2;
        }
        else if(i % 10 == 7 || i % 10 == 8)
        {
            p3[i] = 4;
        }
        else
        {
            p3[i] = 5;
        }
    }
    
    int cnt1 = 0;
    int cnt2 = 0;
    int cnt3 = 0;
    for(int i = 0; i < answers.size(); i++)
    {
        if(answers[i] == p1[i+1])
        {
            cnt1++;
        }
    
        if(answers[i] == p2[i+1])
        {
            cnt2++;
        }
        
        if(answers[i] == p3[i+1])
        {
            cnt3++;
        }
    }
    
    int Max = max(cnt1, cnt2);
    Max = max(Max, cnt3);
    
    if(Max == cnt1)
    {
        answer.push_back(1);
    }
    
    if(Max == cnt2)
    {
        answer.push_back(2);
    }
    
    if(Max == cnt3)
    {
        answer.push_back(3);
    }
    
    return answer;
}
cs
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
 
string solution(vector<string> participant, vector<string> completion) 
{
    string answer = "";
    
    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
    
    for(int i = 0; i < completion.size(); i++)
    {
        if(participant[i] != completion[i])
        {
            answer = participant[i];
            return answer;
        }
    }
    
    // 끝까지 했는데 못찾은 경우, participant의 맨 마지막이 정답
    answer = participant[participant.size()-1];
    return answer;
}
cs

+ Recent posts