https://school.programmers.co.kr/learn/courses/30/lessons/42586
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
1. 큐에 남은 작업 일수를 담는다.
2. 하나씩 꺼내면서 배포할 수 있는 기능의 개수를 정한다.
뒤에 나오는 작업 일수가 큐의 맨 앞에 나오는 작업일수보다 작다면 같이 배포할 수 있다.
배포후 큐에서 남은 작업 일수를 삭제한다.
#include <string>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
queue<int> leftDay;
int cnt=1;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
int proSize = progresses.size();
//1. 큐에 남은 작업 일수를 담는다.
for(int i = 0 ; i< proSize; i++){
int day = (100-progresses[i])%speeds[i] ? (100-progresses[i])/speeds[i]+1 : (100-progresses[i])/speeds[i];
leftDay.push(day);
}
//2. 하나씩 꺼내면서 배포할 수 있는 기능의 개수를 정한다.
// 뒤에 나오는 작업 일수가 큐의 맨 앞에 나오는 작업일수보다 작다면 같이 배포할 수 있다.
// 배포후 큐에서 남은 작업 일수를 삭제한다.
for(int i = 0 ; i< proSize; i++){
int frontDay = leftDay.front();
leftDay.pop();
cnt=1;
while(true){
if(leftDay.size()==0){
answer.push_back(cnt);
return answer;
}
int nextDay= leftDay.front();
if(nextDay<=frontDay){
cnt++;
leftDay.pop();
}
else{
answer.push_back(cnt);
break;
}
}
}
return answer;
}
다른 사람의 풀이
큐나 스택등 다른 자료구조를 사용하지 않고 answer에 바로 값을 넣어준다.
그 다음 일수를 계산에서 answer의 값을 갱신시키는 방식으로 풀었다.
나는 day를 계산할때 100에서 빼줬는데 99에서 빼주면 나머지에 따라서 1을 더할지 말지 계산안하고 그냥 더하면 되서 더 간편한것 같다.
100에서 빼주면 나머지가 없으면 1을 더하지 않고 있으면 1을 더해야한다.
99에서 빼주면 나머지가 없는 경우까지 1을 더한다. (몫이 1만큼 줄어들어서)
내풀이
int day = (100-progresses[i])%speeds[i] ? (100-progresses[i])/speeds[i]+1 : (100-progresses[i])/speeds[i];
다른 사람 풀이
day = (99 - progresses[i]) / speeds[i] + 1;
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 프로세스 c++ (0) | 2024.01.06 |
---|---|
[프로그래머스] 올바른 괄호 c++ (1) | 2024.01.05 |
[프로그래머스] 베스트 앨범 c++ (1) | 2024.01.03 |
[프로그래머스] 의상 c++ (1) | 2024.01.02 |
[프로그래머스] [3차] n진수 게임 c++ (0) | 2023.12.21 |