[백준] C# : 오르막길 (2846번)

ssu_hyun·2022년 9월 21일
0

Data Structure & Algorithm

목록 보기
63/67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baekjoon
{
    class Program
    {
        static void Main(string[] args)
        {
            // 수열 크기
            int n = int.Parse(Console.ReadLine());
            // n개의 양의 정수
            int[] input = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();

            int pre = 0;
            int min = input[0];
            int max = 0;
            List<int> result = new List<int>();
            bool isAscent = false;
            
            for (int i=0; i < n-1; i++)
            {
                // for문을 돌며 앞 수 < 뒤 수 (오름차순)
                if (input[i+1] > input[i])
                {
                    isAscent = true;
                    // 뒤에 나온 수를 max에 저장
                    max = input[i + 1];
                }
                // 앞 수 >= 뒤 수 (내림차순)
                else
                {
                    isAscent = false;
                    // 이전 오르막길 크기 저장
                    if (i > 0 && input[i] > input[i-1]) result.Add(max - min);
                    // 최소값 업데이트
                    min = input[i + 1];
                }
            }
            // 마지막 오르막길 크기도 정하고 끝낼것
            if (isAscent) result.Add(max - min);

            // 결과 리스트가 없는 경우를 제외하고 가장 큰 값을 구한다.
            if  (result.Count == 0)
            {
                Console.WriteLine(0);
            }
            else
            {
                Console.WriteLine(result.Max());
            }
        }
    }
}

0개의 댓글