'Counting Valley'에 해당되는 글 1건

  1. 2020.06.01 HackerRank - Counting Valley

 

// Complete the countingValleys function below.
    static int countingValleys(int n, String s) {
        int seaLevel = 0;//높이
        int valley = 0;
        boolean isValley = false;
        for(int i = 0; i< n;i++){
            char ch = s.charAt(i);
            if(ch == 'U'){//올라가면 높이증가
                seaLevel++;
            }else if(ch == 'D'){//내려가면 높이 감소
                seaLevel--;
            }
            if(seaLevel == 0){//높이가 0에 도달하면 
                if(isValley){//valley인 경우만 카운팅
                    valley++;    
                }            
            }else if(seaLevel > 0){//높이가 0보다 크면 valley가 아님
                isValley = false;
            }else if(seaLevel < 0){//높이가 0보다 작으면 valley
                isValley = true;
            }
        }
        return valley;
    }

 

'개발 > CodingTest' 카테고리의 다른 글

Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 기능개발  (0) 2020.06.03
프로그래머스 - 탑  (0) 2020.06.03
HackerRank - Anagrams  (0) 2020.06.02
Posted by Lumasca
,