[프로그래머스 Lv1] 덧칠하기

수민이슈·2023년 4월 21일
0

[C++] 코딩테스트

목록 보기
21/116
post-thumbnail

🖊️ 문제

https://school.programmers.co.kr/learn/courses/30/lessons/161989

🖊️ 풀이

section 벡터를 돌면서, 카운터를 높여나가자는 생각이긴한데
카운터를 기준으로 돌면서 생각하려니까

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int solution(int n, int m, vector<int> section) {
    int answer = 0;
    
    int current = 1;
    
    int max = section[section.size() - 1];
    
    while(current < max) {
        current = section[0] + m - 1;
        cout << current << endl;
        answer += 1;
        for (int i = 0 ; i < section.size() ; i++) {
            if (section[i] <= current) {
                section.erase(section.begin() + i);        
            }
        }
    }
    
    return answer;
}

막..
난리를 쳤는데
아 뒤지겟네 생각이 안나
for문이 안끝나 제발 ㅠㅠ
ㅠㅠ

근데 벡터를 기준으로 for문을 돌려보면
와 너무 쉽잖아

정말..
정말 스스로가 이해되지 않는 .. 문제엿다
Lv2에서 Lv1으로 한 달 전에 강등당했다는데
그럴만하군..

🖊️ 코드

#include <string>
#include <vector>

using namespace std;

int solution(int n, int m, vector<int> section) {
    int answer = 0;
    int cur = 0;
    
    for (int i = 0 ; i < section.size(); i++) {
        if (cur < section[i]) {
            answer += 1;
            cur = section[i] + m - 1;
        }
        
    }
    
    return answer;
}

0개의 댓글