OSgood의 개발일기

[프로그래머스] LEVEL2 기능개발(C++) 본문

Algorithm/Algorithm 문제 연습

[프로그래머스] LEVEL2 기능개발(C++)

OSgood 2019. 1. 13. 02:00

 오늘은 같이 하는 친구녀석이 바빠서 혼자 문제 풀이를 진행하였다. 그래서 그냥 간단한  LEVEL2로 진행했다. 문제는 아래 URL을 참고해서 읽어보면 될듯 싶다.


https://programmers.co.kr/learn/courses/30/lessons/42586


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
 
vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    vector<int> maxs(progresses.size());
    float temp;
    int max = 0;
    int counting=1;
 
    for (int i=0;i<progresses.size();i++
    {
        temp = speeds[i];
        temp = (100 - progresses[i])/temp;
        max = (int)ceil(temp);
        maxs[i] = max;
    }
 
    max = maxs[0];
    for (int i=1;i<maxs.size();i++
    {
        if(max>=maxs[i]) 
        {
            counting++;
        } 
        else 
        {
            answer.push_back(counting);
            counting =1;
            max =maxs[i];
        }
    }
    answer.push_back(counting);
    return answer;
}
cs


<해설>

-> 문제만 이해하면 정말 간단한 코드이다. 남은 작업량을 계산하고, 그걸을 일의효율로 나눈 후 올림해서 임시 저장을 진행한다. 그 후 max값을 찾아가면서 max값이 넘을 때만 카운티을 해주고 맥스값이 새롭게 바뀔 때는 벡터에 전에 카운팅한 갯수를 넣어주면 된다. 말로 한번에 설명하려다보니 횡설수설(?) 했는데 사실 직접문제를 풀어본다면 이해가 갈 것이다. 아니면 직접 문제를 푸는 것이 지금 내 아래 해설 몇줄을 해석하는 것보다 쉬울 수도 있을 것이다.


 좀 더 난이도가 있는 알고리즘 문제를 포스팅할 때는 주석도 같이 업로드 하도록 하겠다.


출처 - 프로그래머스(https://programmers.co.kr/)


Comments