아래 블로그를 참고해 작성한 코드임을 명시합니다.
https://ongveloper.tistory.com/76
프로그래머스 기능개발 c++ (스택/큐)
문제 출처 : https://programmers.co.kr/learn/courses/30/lessons/42586 코딩테스트 연습 - 기능개발 프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일 때 서비스에 반영할 수 있..
ongveloper.tistory.com
코드1
벡터progresses에 벡터speeds값을 매번 더해 맨앞원소가 100 이상인지 확인하고, 이상이면 벡터의 값을 큐q에 넣어 q.front()가 100이 넘는지 확인
#include <string>
#include <vector>
#include <queue>
using namespace std;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
int pointer = 0;
while(pointer < progresses.size()) { //포인터가 끝에 닿을 때 까지
if(progresses[pointer] >=100) { // 현재 포인터 위치의 원소(맨 앞 원소)가 100 이상이면
queue<int> q;
int count = 0;
for(int i = pointer;i<progresses.size();i++) {
q.push(progresses[i]); // 큐q에 포인터 위치의 원소부터 끝까지 push
}
while(true) {
if(q.front() >= 100 && !q.empty()) { //q가 비어있지 않고 맨 앞 원소가 100이상이면 count증가 후 pop
count++;
q.pop();
}
else { //아니면 answer에 count 삽입 후 반복문 탈출
pointer += count;
answer.push_back(count);
break;
}
}
}
else { //하루씩 작업량 증가
for(int i = 0; i < progresses.size();i++) {
progresses[i] += speeds[i];
}
}
}
return answer;
}
+ 처음 실패한 코드
스택/큐 이용할 생각은 하지 못하고 그냥 벡터를 이용해 풀려고 했으나 시간초과로 제출되지 않았다....
(답은 맞게 나와서 되겠다 싶었는데....)
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
int num = progresses.size(); //작업 개수 받기
int pointer = 0;
//int day = 0;
int commit = 0;
while(pointer < num) { //포인터가 num과 같지 않을 때 까지
for(int i = pointer; i <num ; i++){
progresses[i] += speeds[i];
}
while(progresses[pointer]>=100) {
pointer++;
commit++;
}
if(commit != 0) {
answer.push_back(commit);
pointer = 0;
commit = 0;
}
}
return answer;
}
'CodingTest Practice > Programmers' 카테고리의 다른 글
프로그래머스 : 없는 숫자 더하기 (C++, Lv.1) (0) | 2021.12.30 |
---|---|
프로그래머스 : 신규 아이디 추천 (C++, Lv.1) (0) | 2021.12.30 |
프로그래머스 : 문자열 다루기 기본 (C++) (0) | 2021.12.30 |
프로그래머스 : 모의고사 (C++, Lv.1) (0) | 2021.12.30 |
프로그래머스 : 로또의 최고 순위와 최저 순위 (C++, Lv.1) (0) | 2021.12.30 |