본문 바로가기

코딩테스트/프로그래머스

(11)
[프로그래머스] 더 맵게 프로그래머스 더 맵게 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42626 #include #include #include #include using namespace std; int solution(vector scoville, int K) { int answer = 0; priority_queue pq(scoville.begin(), scoville.end()); // priority_queue pq와 같다. 내림차순이 default인데 오름차순으로 쓰다 보니까 저렇게 됨 // priority_queue로 오름차순으로 정렬 priority_queue가 vector형태로 담음 greater은 오름 차순으로 내림차순은 less while(pq...
[프로그래머스] 주식가격 프로그래머스 주식가격 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42584 // 버블 소트 방식으로 이용할꺼 백트래킹 안하면 솔직히 시간초과 날듯 // 비교하고 자기보다 작은거 나오면 바로 break // 그리고 마지막에 비교 안된값(마지막값)은 0이니까 push_back해줌 #include #include using namespace std; vector solution(vector prices) { vector answer; for (int i = 0; i ..
[프로그래머스] 쇠막대기 프로그래머스 쇠막대기 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42585 // 눈이 존나 좋아야 된다. 그림보고 처음부터 하나하나 해보면 풀린다. #include #include #include using namespace std; int solution(string arrangement) { int answer = 0; stack num; //글자의 처음부터 마지막까지 순회 for (int i = 0; i < arrangement.length(); i++){ //여는 괄호라면 스택에 추가 if (arrangement[i] == '(') num.push(1); else{ //닫는 괄호라면 스택에서 하나제거 num.pop(); //전 기호가 여..
[프로그래머스] 프린터 프로그래머스 프린터 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42587 // 우선순위 큐 pq에 priorities 넣고 그냥 큐q에 priorities와 index 넣는다. // 그리고 while문에서 q에서 priorities와 index 빼서 pq의 top과 비교해서 같으면 answer++ 하고 이 인덱스가 location이랑 같으면 내가 원하던 위치니까 break하고 아니면 다시 큐에 p와 priorities 넣는다. (그래야 순환되니까) #include #include #include using namespace std; int solution(vector priorities, int location) { int answer = 0;..
[프로그래머스] 기능개발 프로그래머스 기능개발 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42586 // 일단 100에서 progresses[i]를 빼고 이 값을 speeds[i]로 나누자 // 딱 떨어지면 그냥 안떨어지면 + 1 하고 q에 넣는다. // 그리고 while문을 2번 돌린다. // 처음 while문에서 num = q.front하고 cnt = 1하고 그 안의 while문에서 q.front를 또해 이 값이 num보다 작아야 한다. 그래야 기다리는 거니까 // 그리고 안의 while문 빠져 나오면 answer에 push back 해준다. #include #include #include using namespace std; vector solution(vecto..
[프로그래머스] 다리를 지나는 트럭 프로그래머스 다리를 지나는 트럭 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42583 // 너무 어렵다 연습이 필요할듯 // 일단 시간은 흐른다. 현재 시간에서 넣은 시간을 빼서 그게 맞으면 트럭을 넣는 큐에서 빼주고 // 아니면 들어갈 수 있나 확인하고 만약 마지막이면 다리 길이만큼 시간에 더한다. 트럭 큐와 넣은 시간을 시간큐에 넣어준다. // 트럭넣는 큐 q와 시간넣는 큐 t를 두개 만든다. // while문을 돌려서 시간 증가시키고 현재 시간에 큐 처음 들어갔을때 시간 큐를 빼서 그게 다리 길이와 같은지 확인 // 새로 넣을 것과 현재까지 들어간 무게가 weight 이하인지 확인한다. 이 조건문 안에서 만약 트럭이 하나 남았을 경우에는 ..
[프로그래머스] 탑 프로그래머스 탑 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42588 // 일단 answer배열 heights 사이즈만큼 0으로 resize 해주고 // i는 num-1부터 하고 j = 0부터 시작해서 j가 오른쪽으로 점점 가니까 값 갱신해 주게 되니까 결국에는 i와 가장 가까운 쪽의 탑이 됨 #include #include using namespace std; vector solution(vector heights) { vector answer; int num = heights.size(); answer.resize(num, 0); for (int i = num -1; i >= 0; i--){ for (int j = 0; j < i; j++)..
[프로그래머스] 베스트앨범 프로그래머스 베스트앪범 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42579 // map 만들어서 장르(string)과 재생횟수, 인덱스 담는다. // 구조체 만들어서 장르별 재생횟수 큰 순서대로 정렬 // map에서 장르안에서 재생횟수 큰 순서대로 정렬 // 장르별 재생횟수 마다 그 장르 안에서 재생횟수 큰 순서대로 2개 이중 for문 돌려서 answer에 push #include #include #include #include using namespace std; struct tsum{ int sum; string name; }; //장르 정렬 bool comp(tsum a, tsum b){ return a.sum > b.sum; } //곡..
[프로그래머스] 위장 프로그래머스 위장 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42578 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr // 곱의 법칙이다. 해당 옷 카테고리 별로 개수에다가 그 해당 옷을 안입었을때 경우 +1 하고 카테고리 끼리 곱한다. // 그리고 마지막에 옷 하나도 안입었을 경우 -1 해준다. #include #include #include using namespace std; int solution(vector clothes) { int answer = 1; map m; for (int i..
[프로그래머스] 전화번호 목록 프로그래머스 전화번호 목록 C++ 풀이 https://programmers.co.kr/learn/courses/30/lessons/42577 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr // 접두어 찾는 거임 // phone_book[i]에서 phone_book[j]를 찾아서 접두어 (문자의 처음) == 0 (문자의 처음 위치 인덱스) 면 return answer #include #include using namespace std; bool solution(vector phone_book) { bool answer = true; for (int i = ..