PS/프로그래머스
[ 프로그래머스 ] Lv 1. 스택/큐 같은 숫자는 싫어 - (C++/C)
발효홍삼
2022. 8. 31. 23:33
728x90
- 문제
https://school.programmers.co.kr/learn/challenges
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
- 풀이법
연속하는 숫자를 찾는 문제이기 때문에 스택의 top과 비교하며 반복해서 push해주는 방식으로 풀이하였다.
- 풀이
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
vector<int> answer;
stack <int> s;
void push_array(int i) {
answer.push_back(i);
s.push(i);
}
vector<int> solution(vector<int> arr)
{
for(auto i : arr) {
if(!s.empty()) {
if(s.top() == i) continue;
else push_array(i);
}
else push_array(i);
}
return answer;
}
728x90