- [ 장르 ] Stack / Queue
- [ Question ]
- My Code
#include <string> #include <vector> using namespace std; vector<int> solution(vector<int> progresses, vector<int> speeds) { vector<int> answer; int CurFrontIndex = 0; // 현재 제일 앞 우선순위 인덱스 int releaseCount = 0; // 현 시점 배포할 작업 수 while(true) { while(progresses[CurFrontIndex] < 100) // 현재 우선순위 인덱스 작업이 100% 될때까지 { for(int i = CurFrontIndex ; i < progresses.size(); i++) // 모든 작업, 스피드만큼 증가 progresses[i] += speeds[i]; } while(progresses[CurFrontIndex] >= 100 ) // 현재 우선순위 인덱스의 작업이 일단 100%끝난 시점에서 다음 작업으로 넘어가면서 다른 100%끝난 작업이 있는지 전진 { releaseCount++; CurFrontIndex++; if(CurFrontIndex >= progresses.size()) break; } if(releaseCount != 0) // 한번 순회하면서 100% 완성된 작업 수 카운트를 정답 벡터에 push ! { answer.push_back(releaseCount); releaseCount = 0; } if(CurFrontIndex >= progresses.size()) // 마지막 작업까지 끝나면 탈출 break; } return answer; }
- Better Code
#include <string> #include <vector> #include <iostream> using namespace std; vector<int> solution(vector<int> progresses, vector<int> speeds) { vector<int> answer; int day; int max_day = 0; for (int i = 0; i < progresses.size(); ++i) // 반복문 하나로 끝 { day = (99 - progresses[i]) / speeds[i] + 1; // 매 작업마다 한 작업당 스피드로 나누면 몇일이 소요할 지 바로 나오겠지 if (answer.empty() || max_day < day) answer.push_back(1); else ++answer.back(); // 이 부분이 포인트라 했다. ++answer.back() .. if (max_day < day) max_day = day; } return answer; }
" 나는 정작 STACK을 잘 구현할려고 한 것인가 ? "
- [ CheckPoint ]
- 이 문제의 작은 포인트는 작업이 100프로가 되면 작업을 중단하고 배포를 해야 한다는 것. 혹시 본인의 테스트 케이스가 대부분 맞는데 1,2개가 틀린다면 이 부분을 체크 해볼 것.
- 나의 로직은 맨 앞 작업부터 끝나기까지 맨 끝 작업까지 한번 씩 speed만큼 작업의 진도를 증가 시키는 것인데, 만약 기존 작업량이 모두 낮고
(10% 미만 ) speed는 각각 전부 1일 경우, 나의 알고리즘은 작업시간이 오래걸릴 것이다.- 좀 더 좋은 방법은 Better Code의 한 작업의 단계에서
(99-현재작업의 량) / 작업의 speed + 1 = 남은 작업 일 수가 측정 됨을 생각하면 연산 한번의 이런 과정이 끝난다.- 아직 ++answer.push_back(1)의 의미를 정확히 모르겠는데 이해를 해봐야 겠다. 혹시 먼저 의견을 줄 수 있다면 조언을 주길 바란다.
728x90
반응형
'Algorithm Practice > [ 프로그래머스 ]' 카테고리의 다른 글
[프로그래머스] [#42584] 주식가격 (0) | 2020.09.16 |
---|---|
[프로그래머스][#42583] 다리를 지나는 트럭 (0) | 2020.09.16 |
[프로그래머스][#42746] 가장 큰수 (0) | 2020.09.12 |
[프로그래머스] [#42587] 프린터 (0) | 2020.09.11 |
[ 프로그래머스 ] [ #12943 ] 콜라츠 추측 (0) | 2020.09.08 |