주녘공부일지

[프로그래머스 C#] Lv.2 주식가격 본문

Programmers - C#/CodingTest Lv.2

[프로그래머스 C#] Lv.2 주식가격

주녘 2023. 8. 23. 17:02
728x90

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

 

프로그래머스

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

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

- 스택/큐 문제라고 분류되어 있다고, 스택/큐 로만 해결하려고 하지 않아도 됨

+ List가 편해서 List를 사용했는데, Array가 더 빠름

    using System;
    using System.Collections.Generic;

    public class Solution
    {
        public int[] solution(int[] prices)
        {
            List<int> list = new List<int>();

            for (int i = 0; i < prices.Length; i++)
            {
                int num = 0;

                // 몇 초 뒤에 떨어지는지 체크
                for (int j = i + 1; j < prices.Length; j++)
                {
                    num++;

                    if (prices[i] > prices[j])
                        break;
                }

                list.Add(num);
            }

            return list.ToArray();
        }
    }
728x90