https://school.programmers.co.kr/learn/courses/30/lessons/42586
해당 문제는 다시 푸는 문제이다.
이게 내가 짠 코드이다.
#include <string>
#include <vector>
#include <deque>
using namespace std;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
int cnt = 0;
deque<pair<int, int>> work;
for (int i = 0; i < speeds.size(); i++) {
work.push_back({ progresses[i], speeds[i]});
}
while (!work.empty())
{
cnt = 0;
for (auto &i : work) {
i.first += i.second;
}
for (auto& i : work) {
if (i.first >= 100) {
work.pop_front();
cnt++;
}
else {
if (cnt >= 1) answer.push_back(cnt);
break;
}
}
}
if (cnt >= 1) answer.push_back(cnt);
return answer;
}
푼 사람들 중에서 효율적인 코드가 뭐가 있나 봤다.
다른 사람의 코드에 주석을 추가한 것 이긴 하지만,
좀 간편하게,
메모리를 많이 안잡아 먹을 수 있는 효율적인 코드를 위해
수학적으로 접근하는 방식을 앞으로 많이 생각해야겠다고 느꼈다.
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer; // 각 배포 시 몇 개의 기능이 함께 배포되는지를 저장할 벡터
int day; // 현재 기능이 완료되기까지 필요한 일 수
int max_day = 0; // 현재 배포 그룹 중 가장 오래 걸리는 기능의 완료 일 수
// 각 기능(progresses[i])에 대해 완료 일 수 계산 및 배포 그룹 생성
for (int i = 0; i < progresses.size(); ++i) {
day = (99 - progresses[i]) / speeds[i] + 1; // i번째 기능이 완료되기까지 걸리는 일 수
// answer가 비어 있거나, 현재 배포 그룹의 최대 일 수보다 더 오래 걸리는 경우 새 그룹으로 시작
if (answer.empty() || max_day < day)
answer.push_back(1); // 새로운 배포 그룹 시작
else
++answer.back(); // 기존 배포 그룹에 기능 추가
// 최대 일 수 갱신
if (max_day < day)
max_day = day;
}
return answer; // 각 배포 시 함께 배포되는 기능의 개수를 반환
}
'콘솔창 & 윈도우창 > 코딩 테스트' 카테고리의 다른 글
프로그래머스 LV.2 오픈채팅방 (카카오 코테) (0) | 2024.11.15 |
---|---|
프로그래머스 LV.2 귤 고르기 (1) | 2024.11.15 |
프로그래머스 LV.2 더 맵게 (0) | 2024.11.13 |
프로그래머스 LV.1 신규 아이디 추천 (1) | 2024.11.13 |
프로그래머스 LV.2 짝지어 제거 (0) | 2024.11.12 |