Sort by

recency

|

4095 Discussions

|

  • + 0 comments
      int countingValleys(int steps, string path) {
                int down=0,in_valleys=0,up=0,count=0 ;
                for(int i=0;i<path.size();i++){
                    if(path[i]=='U') up++;
                    if(path[i]=='D') down++;
                    if(down-up>0&&in_valleys==0){
                        count++;
                        in_valleys++;
                    }
                    if(down-up==0&&in_valleys!=0){
                        in_valleys =0;
                    }
                }
                return count;
            }
    
  • + 0 comments
    int countingValleys(int steps, string path) {
    int n_valley = 0, step = 0;
    bool in_valley = false;
    for(char c : path){
        if(c == 'D'){
            step--;
        }
        if (c == 'U') {
            step++;
        }
        if(step == -1 && in_valley == false ){
            in_valley = true;
            n_valley++;
        }
        if (step == 0) {
            in_valley = false;
        }
    }
    return n_valley;
    

    }

  • + 0 comments

    Here is my c++ solution you can find the video here : https://youtu.be/fgJ-i8RJ1Qw

    int countingValleys(int steps, string path) {
        int res = 0, level = 0;
        for(char c : path){
            if(c == 'U'){
                level++;
                if(level == 0) res ++;
            }
            else level--;
        }
        return res;
    }
    
  • + 0 comments

    Python solution I check if i'm level -1 (under the see) and it's D

    def countingValleys(steps, path):
        levelAboutTheSea = 0
        countingIsvalley = 0
        
        for i in range(steps):
            isGoUp = path[i] == "U"
            isGoDown = path[i] == "D"
            
            if isGoUp: levelAboutTheSea += 1
            if isGoDown: levelAboutTheSea -= 1
            
            if (levelAboutTheSea == -1  and path[i] == "D"):
                countingIsvalley += 1 
                
        return countingIsvalley
    
  • + 0 comments
    public static int countingValleys(int steps, String path) {
        // Write your code here
        
            int upCnt=0,downCnt=0,valley=0;
            for(Character c:path.toCharArray()){
                if(c=='U')upCnt++;
                else if(c=='D')downCnt++;
                if(upCnt-downCnt==0 && c=='U'){
                    valley++;
                }
            }
            
            return valley;
            
        }