문제
입출력
문제풀이
#include <string>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
vector<int> solution(vector<int> prices) {
vector <int> answer;
queue <int> time;
int idx = 0;
int count = 0;
int size = prices.size()//이렇게 상수로 표현하고 for문 돌리면 시간 단축
for(int i=0;i<prices.size();i++){
count = 0;
for(int j=i+1;j<prices.size();j++){
// if(prices[i] <= prices[j]){
// count++;
// }
count++;
if(prices[i] > prices[j])
break;
}
answer.push_back(count);
}
return answer;
}
주석 처리 된 코드로 돌렸는데, 시간 초과가 떴다.
if문을 반대로 썼더니, 통과.
prices.size()를 상수 size로 for문돌리면 더 빠르게 단축된다.
'programming language > Structure' 카테고리의 다른 글
[백준 스택문제] 9021번 괄호 (0) | 2021.07.30 |
---|---|
트리, 힙, 그래프 (0) | 2021.07.30 |
[프로그래머스 Level2 스택] 다리를 지나는 트럭 (0) | 2021.07.29 |
std::stack, std::queue, std::priority_queue (0) | 2021.07.29 |
[프로그래머스 Level2 스택] 프린터 (0) | 2021.07.28 |