본문 바로가기
개발공간

C# Counting Valleys [HackerRank]

by -0o0- 2021. 9. 11.

C# Counting Valleys [HackerRank]

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{
    public static int countingValleys(int steps, string path)
    {
        //계곡 들어간 횟수, 현재 레벨 변수 
        //레벨이 0보다 아래면 계곡에 들어감
        int count = 0;
        int level = 0;
        
        for(int i=0; i<steps; i++)
        {
            //이번 경로가 D면서 현재 레벨이 0이면 계곡 입수
            if(path[i]=='D' && level==0)
            {
                count++;
                level--;
                continue;
            }    
            
            //경로가 D이면 레벨 - , U이면 +
            if(path[i]=='D') level--;
            else if(path[i]=='U') level++;
        }
        return count;
    }

}

class Solution
{
    public static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int steps = Convert.ToInt32(Console.ReadLine().Trim());

        string path = Console.ReadLine();

        int result = Result.countingValleys(steps, path);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}

'개발공간' 카테고리의 다른 글

C# Cats and a Mouse [HackerRank]  (0) 2021.09.12
C# Electronics Shop [HackerRank]  (0) 2021.09.11
C# Drawing Book [HackerRank]  (0) 2021.09.11
C# Sales by Match [HackerRank]  (0) 2021.09.10
C# Bill Division [HackerRank]  (0) 2021.09.10

댓글