Sort by

recency

|

4144 Discussions

|

  • + 0 comments
    def countingValleys(steps, path):
        # Write your code here
        
        valleys = 0
        level = 0
        for i in range(steps):
            if path[i] == "U":
                level += 1
                if level == 0:
                    valleys += 1
            else:
                level -= 1
                  
        return valleys
    
  • + 0 comments

    Great explanation of counting valleys here! The logic—tracking altitude changes and recognizing when you're exiting a valley (i.e. going from below sea level back to zero)—is spot on. It’s really helpful to see the condition if (level == 0 && step == 'U') ++valleys; clearly broken down.

    By the way, if anyone’s working on code challenges while planning overseas travel or residency, you might also be juggling documentation like police clearance. For instance, those applying in the Philippines often need a police clearance to show good conduct. Just a friendly heads-up for anyone balancing coding tasks and bureaucracy!

  • + 0 comments

    My solution c++

    int countingValleys(int steps, string path) {
     int stepsCount=0,valley=0;
     for(int i=0; i<steps; i++){
        if(path[i]=='U'){
          stepsCount++;
          if(stepsCount==0) valley++;
        }
        else if(path[i]=='D'){
          stepsCount--;
        }
     }
     return valley;
    }
    
  • + 0 comments
    public static int countingValleys(int steps, String path) {
    // Write your code here
    int ans = 0,level=0;
    for(char c: path.toCharArray()) {
        if(c=='U') level+=1;
        else level-=1;
        if(level==0 && c=='U') ans++;
    }
    return ans;
    

    } `

  • + 1 comment

    Sorry, I didn't get the question. The mention is "A valley is a sequence of consecutive steps below sea level,"

    Considering String "DUDDDUUDUU", I see only one conecutive substring with D's. So, I am expecting the answer to be 1. How come the original answer is 2. Please help