프로그래머스

C# - 주식 가격

ybbro 2023. 2. 8. 18:14

https://school.programmers.co.kr/learn/courses/30/lessons/42584

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] prices)
    {
        List<int> answer = new List<int>();
        // 모든 주가에 대해
        for (int i = 0; i < prices.Length; i++)
        {
            // 새 주가에 대해 전체 비교를 하기 전에 항목 추가
            answer.Add(0);
            // 해당 주가 이후 주가 전체에 대해
            for (int j = i+1; j < prices.Length; j++)
            {
                // 1초 가산
                answer[i]++;
                // 해당 주가보다 떨어졌다면 루프 종료
                if (prices[i] > prices[j])
                    break;
            }
        }
        int[] answer_array = answer.ToArray();
        return answer_array;
    }
}

스텍/큐를 사용하여 푸는 문제라고 주었지만 사용하지 않는 쪽이 시간은 더 짧게 나왔다