GitHubSeob
C++ / 프로그래머스 / 햄버거 만들기 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/133502
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제풀이
ingredient를 탐색하면서 [idx] = 1, [idx+1] = 2, [idx+2] = 3, [idx+3] = 1이면 answer에 1을 더하고,
idx-4의 값이 -1보다 작을 수 있으므로 둘 중 큰 값을 idx에 저장한다.
for문에서 탐색할 때마다 ++idx 했으므로, 왼쪽으로 네 칸을 옮겨서 다음 탐색에서 세 칸이 왼쪽으로 가게끔 했다.
코드
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> ingredient) {
int idx(0);
int answer = 0;
for (idx; idx + 3 < ingredient.size(); ++idx) {
if (ingredient[idx] == 1 && ingredient[idx + 1] == 2 && ingredient[idx + 2] == 3 && ingredient[idx + 3] == 1) {
ingredient.erase(ingredient.begin() + idx, ingredient.begin() + idx + 4);
answer++;
idx = max(-1, idx - 4);
}
}
return answer;
}
'Programmers > Level 1' 카테고리의 다른 글
C++ / 프로그래머스 / 대충 만든 자판 (0) | 2023.06.21 |
---|---|
C++ / 프로그래머스 / 둘만의 암호 (0) | 2023.06.21 |
C++ / 프로그래머스 / 성격 유형 검사하기 (0) | 2023.06.21 |
C++ / 프로그래머스 / 바탕화면 정리 (0) | 2023.06.21 |
C++ / 프로그래머스 / 개인정보 수집 유효기간 (0) | 2023.06.21 |