출처
https://programmers.co.kr/learn/courses/30/lessons/42584
문제 설명
초 단위로 기록된 주식 가격이 담긴 배열 Prices가 매개변수로 주어질 떄, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요.
제한 사항
- prices의 각 가격은 1 이상 10,000 이하인 자연수입니다.
- prices의 길이는 2 이상 100,000 이하입니다.
입출력 예
풀이
이중 포문을 이용하여 시간이 가격이 떨어지기 전까지 count 변수를 통해 시간을 측정하고 가격이 떨어지는 순간 break 문을 이용하여 빠져 나온다. 사실 n이 100,000이기 때문에 이중 포문으로 문제를 풀었을 시 시간 복잡도가 O(n^2)이기 때문에 시간초과가 나야하는데 그런 부분을 고려하지 않고 TestCase를 만든 것 같다. 다시 시간 복잡도가 O(n)이 되도록 코딩할 예정이다.
Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Solution {
public static int[] solution(int[] prices)
{
List<int> answer_lst = new List<int>();
int[] answer = new int[] { };
for(int i = 0; i < prices.Length; i++)
{
int cnt = 0;
for(int j = i + 1; j < prices.Length; j++)
{
cnt++;
if (prices[j] >= prices[i])
continue;
else break;
}
answer_lst.Add(cnt);
}
answer = answer_lst.ToArray();
return answer;
}
}
'알고리즘 > 코딩연습' 카테고리의 다른 글
[프로그래머스] LEVEL2 - 괄호 변환 (0) | 2020.12.22 |
---|---|
[프로그래머스] LEVEL2 - 전화번호 목록 (0) | 2020.12.16 |
[프로그래머스]LEVEL2 - 멀쩡한 사각형 (0) | 2020.12.14 |
[프로그래머스] LEVEL2 - 위장 (0) | 2020.09.09 |