프로그래머스 : 기능개발 (스택/큐)

Chanyang Im·2021년 7월 22일
0

프로그래머스

목록 보기
2/6
post-thumbnail

기능개발 (스택/큐)

문제

풀이

프로그래머스에는 스택/큐 문제로 분류되었지만
굳이 스택/큐를 사용하지 않아도 풀 수 있는 문제입니다.

조건을 만족시키면 쉽게 풀 수 있는 문제입니다.
조건1 : 진도가 100%일 때 배포할 수 있습니다.
조건2 : 뒤에 있는 기능의 진도가 100%라도 앞에 있는 기능의
             진도가 100%가 아니라면 배포할 수 없습니다.
조건3 : 배포할 때 배포하는 기능의 수를 answer에 넣어줍니다.

C++ 코드

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    int size = progresses.size();
    
    // 작업 완료 수
    int count = 0;
    
    // 작업이 모두 완료되면 종료
    while(count < size) {
        
        // 배포 기능 수 초기화
        int n = 0;
        
        // 작업 진도 나가기
        for(int i = count; i < size; i++) {
            progresses[i] += speeds[i];
         }
        
        // 조건에 맞게 완료된 작업이 있으면 배포 준비
        while(count < size && progresses[count] >= 100) {
            n++;
            count++;
        }
        
        // 배포 가능한 기능이 있으면 배포
        if(n != 0) {
            answer.push_back(n);
            continue;
         }
    } 
    return answer;
}

Python 코드

def solution(progresses, speeds):
    answer = []
    size = len(progresses)
    
    # 작업 완료수
    count = 0
    
    # 작업이 모두 완료되면 종료
    while count < size:
        # 배포 기능 수 초기화
        n = 0;
        
        # 작업 진도 나가기
        for i in range(size):
            progresses[i] += speeds[i]
        
        # 조건에 맞게 완료된 작업이 있으면 배포 준비
        while count < size and progresses[count] >= 100:
            n += 1
            count += 1
        
        # 배포 가능한 기능이 있으면 배포
        if n != 0:
            answer.append(n)
    return answer
profile
안녕하세요!! 세상에 관심이 많은 공학자입니다!😆

0개의 댓글