Sort by

recency

|

4106 Discussions

|

  • + 0 comments

    can you help to integrate this algorithem script with my site movies streaming app page? i want to use on it my wordpress website.

  • + 0 comments
    ##python
    def countingValleys(steps, path):
        up = 0
        down = 0
        valley = 0
        for i in range(steps):
            if path[i]=='U':
                up +=1
            elif path[i] =='D':
                down +=1
            if (up == down and path[i]=='U'):
                valley +=1
        return valley
    
  • + 0 comments

    python

    def countingValleys(steps, path): up = 0 down = 0 valley = 0 for i in range(steps): if path[i]=='U': up +=1 elif path[i] =='D': down +=1 if (up == down and path[i]=='U'): valley +=1 return valley

  • + 0 comments

    javascript solution

    function countingValleys(steps, path) { let valleys = 0; let altitude = 0;

    for (let i = 0; i < steps; i++) {
        if (path[i] === 'U') {
            altitude++;
            // Check if we just came up to sea level
            if (altitude === 0) {
                valleys++;
            }
        } else if (path[i] === 'D') {
            altitude--;
        }
    }
    
    return valleys;
    

    }

  • + 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;
    }