https://programmers.co.kr/learn/courses/30/lessons/64061

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

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
30
31
32
33
34
35
36
#include <string>
#include <vector>
#include <stack>
 
using namespace std;
 
int solution(vector<vector<int>> board, vector<int> moves) {
    int answer = 0;
    stack<int> st;
    //열마다 제일 위에 있는 값의 높이(행)
    vector<int> top(board.size(), board.size());
 
    //i는 열 j는 행
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board.size(); j++) {
            if (board[j][i] != 0) {
                top[i] = j;
                break;
            }
        }
    }
 
    for (int i = 0; i < moves.size(); i++) {
        int col = moves[i]-1;
        if (top[col] != board.size()) {
            if(st.empty() || st.top() != board[top[col]][col]){ st.push(board[top[col]][col]); }
            else {
                st.pop();
                answer += 2;
            }
            board[top[col]][col] = 0;
            top[col]++;
        }
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

NxN의 배열의 임의의 열을 하나의 stack 구조로 생각하고 문제를 접근

각 열마다 가장 위 값의 위치 인덱스를 top 벡터에 저장 

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

실패율 - 2019 KAKAO BLIND RECRUITMENT  (0) 2020.04.04
예산  (0) 2020.04.04
체육복  (0) 2020.04.04
x만큼 간격이 있는 n개의 숫자  (0) 2020.04.04
핸드폰 번호 가리기  (0) 2020.04.03

https://programmers.co.kr/learn/courses/30/lessons/42889

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

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
30
31
32
33
34
35
36
37
38
39
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
float fail_rate(vector<int> stages, int cmp) {
    float not_yet = 0;
    float arrival = 0;
    
    for (int i = 0; i < stages.size(); i++) {
        if (stages[i] > cmp) {
            arrival++;
        }
        else if (stages[i] == cmp) {
            arrival++;
            not_yet++;
        }
    }
    if (arrival == 0) {
        return 0;
    }
    return not_yet / arrival;
}
 
vector<int> solution(int N, vector<int> stages) {
    vector<int> answer;
    vector<float> fail;
    for (int i = 1; i < N+1; i++) {
        fail.push_back(fail_rate(stages, i));
    }
    for (int i = 1; i < N + 1; i++) {
        vector<float>::iterator max_iter = max_element(fail.begin(), fail.end());
        int max_idx = max_iter - fail.begin();
        answer.push_back(max_idx+1);
        fail[max_idx] = -1;
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

문제에서 실패율의 정의 : 스테이지에 도달했으나 아직 클리어하지 못한플레이어 수 / 스테이지에 도달한 플레이어 수

스테이지의 개수 N, 사용자가 현재 멈춰있는 스테이지 번호의 배열 stages

실패율을 구할 때 도달한 플레이어수가 없어 arrival의 값이 0인 경우 오류이므로
도달한 플레이어 수가 0인 경우 실패율을 0으로 처리해줘야한다

 

반응형

https://programmers.co.kr/learn/courses/30/lessons/12982

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int solution(vector<int> d, int budget) {
    int answer = 0;
    
    sort(d.begin(), d.end());
    
    for(int i = 0; i < d.size(); i++) {
        if(d[i] <= budget){
            budget -= d[i];
            answer++;
        }
        else{break;}
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

반응형

https://programmers.co.kr/learn/courses/30/lessons/42862

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1. 

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
30
31
32
33
34
35
36
37
38
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int solution(int n, vector<int> lost, vector<int> reserve) {
    int answer = 0;
 
    for (int i = 0; i < lost.size(); i++) {
        for (int j = 0; j < reserve.size(); j++) {
            if (lost[i] == reserve[j]) {
                reserve.erase(reserve.begin() + j);
                lost.erase(lost.begin() + i);
                i = 0;
            }
        }
    }
 
    answer = n - lost.size();
 
    for (int i = 0; i < lost.size(); i++) {
        for (int j = 0; j<reserve.size(); j++) {
            if (lost[i] - 1 == reserve[j]) {
                reserve.erase(reserve.begin() + j);
                answer++;
                break;
            }
            else if(lost[i] + 1 == reserve[j]) {
                reserve.erase(reserve.begin() + j);
                answer++;
                break;
            }
        }
    }
 
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

여벌의 체육복을 갖고 있는 학생이 도난당할 수 있는 경우가 있다고 문제에 제시

reserve와 lost에 중복으로 데이터가 들어갈 수 있는 경우가 발생

   중복을 제거하지 않는 경우 체육복이 1벌인 사람이 다른 사람에게 빌려주는 경우 발생

   ex) 5, lost = {2, 3, 4}, reserve = {3, 4, 5} 

        3->2 에게 빌려줌

        하지만 사실상 2벌 중 1벌을 도난당해 빌려줄 수 없음

 

중복을 제거한 후에는 우선적으로 앞사람에게 빌릴 수 있도록 함

 

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
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int solution2(int n, vector<int> lost, vector<int> reserve) {
    int answer = 0;
    vector<int> check(n+11);
    for (int i = 0; i < lost.size(); i++) {check[lost[i]]--;}
    for (int i = 0; i < reserve.size(); i++) { check[reserve[i]]++; }
 
    for (int i = 1; i < check.size(); i++) {
        if (check[i] == 0) {
            if (check[i - 1== 2) {
                check[i - 1]--;
                check[i]++;
            }
            else if (i+1 != check.size() && check[i + 1== 2) {
                check[i + 1]--;
                check[i]++;
            }
        }
    }
    for (int i = 1; i < check.size(); i++) {
        if (check[i] != 0) { answer++; }
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

학생들이 갖고 있는 옷의 수를 벡터로 만들어 계속적으로 업데이트하여 최종적으로 0벌이 아닌 학생 수를 반환

 

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

실패율 - 2019 KAKAO BLIND RECRUITMENT  (0) 2020.04.04
예산  (0) 2020.04.04
x만큼 간격이 있는 n개의 숫자  (0) 2020.04.04
핸드폰 번호 가리기  (0) 2020.04.03
평균 구하기  (0) 2020.04.03

https://programmers.co.kr/learn/courses/30/lessons/12954

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <vector>
 
using namespace std;
 
vector<long long> solution(int x, int n) {
    vector<long long> answer;
    for (int i = 0; i < n; i++) {
        answer.push_back(x*(i + 1));
    }
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

예산  (0) 2020.04.04
체육복  (0) 2020.04.04
핸드폰 번호 가리기  (0) 2020.04.03
평균 구하기  (0) 2020.04.03
최대공약수와 최소공배수  (0) 2020.04.03

https://programmers.co.kr/learn/courses/30/lessons/12948

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string phone_number) {
    string answer = "";
    int phone_number_sz = phone_number.size();
    answer.append(phone_number.end()-4,phone_number.end());
    return answer;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

체육복  (0) 2020.04.04
x만큼 간격이 있는 n개의 숫자  (0) 2020.04.04
평균 구하기  (0) 2020.04.03
최대공약수와 최소공배수  (0) 2020.04.03
제일 작은 수 제거하기  (0) 2020.04.03

https://programmers.co.kr/learn/courses/30/lessons/12944

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제에 제시된 제한 사항
입출력 예시

1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>
 
using namespace std;
 
double solution(vector<int> arr) {
    double answer = 0;
    for (int i = 0; i < arr.size(); i++) {
        answer += arr[i];
    }
    answer /= arr.size();
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

반응형

https://programmers.co.kr/learn/courses/30/lessons/12940

불러오는 중입니다...

문제에 제시된 제한 사항
입출력 예시

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
30
31
32
#include <vector>
 
using namespace std;
 
//최대공약수
int gcd(int a, int b) {
    int gcd = 0;
    int less_than = a < b ? a : b;
    for (int i = 1; i < less_than + 1; i++) {
        if (a%i == 0 && b%i == 0) {
            gcd = i;
        }
    }
    return gcd;
}
 
//최소공배수
int lcm(int a, int b) {
    int g = gcd(a, b);
    if (g == 1) {
        return a * b;
    }
 
    return a*b/g;
}
 
vector<int> solution(int n, int m) {
    vector<int> answer;
    answer.push_back(gcd(n, m));
    answer.push_back(lcm(n, m));
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

반응형

'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글

핸드폰 번호 가리기  (0) 2020.04.03
평균 구하기  (0) 2020.04.03
제일 작은 수 제거하기  (0) 2020.04.03
정수 내림차순으로 배치하기  (0) 2020.04.03
자릿수 더하기  (0) 2020.04.03

+ Recent posts