링크 : https://school.programmers.co.kr/learn/courses/30/lessons/133502?language=cpp
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
[풀이1 스트링으로?]
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int bread = 1;
const int vege = 2;
const int meat = 3;
int solution(vector<int> ingredient) {
int answer = 0;
stringstream ss;
copy(ingredient.begin(), ingredient.end(), ostream_iterator<int>(ss, ""));
string s = ss.str();
const string buger = "1231";
int findidx =0;
do
{
findidx = s.find("1231");
if(findidx == -1)
{
break;
}
s.erase(findidx, 4);
answer++;
} while(findidx != -1);
return answer;
}
개같이 실패~
split 잘 쓰면 시간복잡도 더 줄일 수 있을테넫
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <regex>
#include <queue>
using namespace std;
int solution(vector<int> ingredient) {
int answer = 0;
stringstream ss;
copy(ingredient.begin(), ingredient.end(), ostream_iterator<int>(ss, ""));
string s = ss.str();
const string buger = "1231";
int findidx = 0;
queue<string> bugerQueue;
findidx = s.find(buger);
if (findidx == -1)
{
return 0;
}
bugerQueue.push(s.substr(findidx + 4));//erase(findidx, 4);
string fronts = s.substr(0, findidx);
answer++;
do
{
fronts.append(bugerQueue.front());
findidx = fronts.find(buger);
if (findidx == -1)
{
fronts = bugerQueue.front();
}
else
{
int orginsize = fronts.size();
bugerQueue.pop();
if(findidx > 4) bugerQueue.push(fronts.substr(0, findidx));
if(orginsize - findidx > 4) bugerQueue.push(fronts.substr(findidx + 4));//erase(findidx, 4);
answer++;
continue;
}
bugerQueue.pop();
} while (bugerQueue.empty() == false);
return answer;
}
int main()
{
solution({ 2, 1, 1, 2, 3, 1, 2, 3, 1 });
return 0;
}
오히려 erase보다 점수가 더 안좋아졌다
'책 > 프로그래머스' 카테고리의 다른 글
[ 프로그래머스] #131127 할인행사 (0) | 2022.11.27 |
---|---|
프로그래머스 - 푸드 파이트 대회 (0) | 2022.11.06 |
프로그래머스 - 타겟 넘버(DFS/BFS) C++ (0) | 2022.11.06 |
[프로그래머스] 연속 부분 수열 합의 개수 (0) | 2022.11.06 |