Sort by

recency

|

4119 Discussions

|

  • + 0 comments

    can you tell me how i can attach this java script steps to describing the path of my travel geek. Thank you in advance for your help!

  • + 1 comment

    JavaScript solution with early exit condition:

    function countingValleys(steps, path) {
        // Write your code here
        var i = 0;
        var elevation = 0;
        var valleys = 0;
        
        while(i < steps && steps - i > Math.abs(elevation)){
            if(path[i] == "U")
                elevation++;
            else {
                elevation--;
                if(elevation == -1)
                    valleys++;
            }
                
            i++;
        }
        
        return valleys;
    }
    
    • + 0 comments

      I want to use this amazing JavaScript solution on website that this hosted on Wordpress. It will be very hepful for my website stardew valley ios

  • + 0 comments
    public static int countingValleys(int steps, String path) {
    int seaLevel = 0, valleys = 0, currentLevel = 0;
    
    for (char step : path.toCharArray()) {
        if (step == 'U') {
            currentLevel++;
        } else {
            currentLevel--;
        }
    
        // If we just came up to sea level from below, we've exited a valley
        if (currentLevel == 0 && step == 'U') {
            valleys++;
        }
    }
    
    return valleys;
    }
    
  • + 0 comments
    def countingValleys(steps, path):
        level = 0
        valley = False
        path_values = list(map(direction_value, path))
        valleys = 0
        
        for s in path_values:
            level += s
            if level < 0:
                valley = True
            if valley and level >= 0:
                valleys += 1
                valley = False
                
        return valleys
        
    def direction_value(d):
        if d == 'U':
            return 1
        return -1
    
  • + 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;
    }