본문 바로가기

Algorithm (알고리즘)/프로그래머스 문제

프로그래머스 코딩테스트 연습 -주식 가격 LV2: (C++)

1.문제

초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요.

제한사항

  • prices의 각 가격은 1 이상 10,000 이하인 자연수입니다.
  • prices의 길이는 2 이상 100,000 이하입니다.

2.입력

prices return
[1, 2, 3, 2, 3] [4, 3, 1, 1, 0]

 

소스코드

#include <string>
#include <vector>
#include <iostream>
using namespace std;

vector<int> solution(vector<int> prices)
{
	vector<int> answer;
	int index = 0;

	for (int i = 0; i < prices.size(); i++)
	{
		index = 0;
		for (int j = i+1; j<prices.size(); j++)
		{
			if (prices[i] > prices[j])
			{			
				index++;
				break;
			}
			else
				index++;
		}
		answer.push_back(index);
		
	}
	return answer;
}
int main()
{
	vector<int> v1;
	v1.push_back(1);v1.push_back(2);v1.push_back(3);v1.push_back(2);v1.push_back(3);
	auto answer = solution(v1);
	for (auto x : answer)
	{
		cout << x << " ";
	}
	return 0;
}

 

체점 결과

정확성: 100.0

합계: 100.0 / 100.0

 

출처-  https://programmers.co.kr/learn/courses/30/lessons/42584